In: Computer Science
Write the c++ program of Secant Method and Fixed point Method in a one program using switch condition .Like :
cout<<"1.Secant Method \n 2. Fixed point Method\n"<<endl; if press 1 then work Secant Method if press 2 then work Fixed point Method .so please writhe the code in c++ using switch case.and the equation given down consider the equation in the given.Note: Must showt the all the step of output all the iteration step shown in the program .in program must shown all the step of iteration result
for secant method equation :4x3-2x-6=0
for fixed point=x2-x-1=0
Please in every itaration must be shown the output then finally Got the root
Secant Method
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
#define f(x) 4*x*x*x - 2*x - 6
using namespace std;
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
cout<< setprecision(4)<< fixed;
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}
x2 = x1 - (x1 - x0) *
f1/(f1-f0);
f2 = f(x2);
cout<<"Iteration-"<< step<<":\t x2 = "<< setw(10)<< x2<<" and f(x2) = "<< setw(10)<< f(x2)<< endl;
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
step = step + 1;
}while(fabs(f2)>e);
cout<< endl<<"Root is: "<< x2;
return 0;
}
Fixed point Method
#include<stdio.h>
#include<math.h>
#include<iostream>
#define g(x) (x+1)/(x)
using namespace std;
int main()
{
int k=0;
float x1,x0;
float eps = 1e-5;
cout<<"Enter the initial guess x0: ";
cin>>x0;
x1=x0;
do
{
k++;
x0=x1;
x1=g(x0);
cout<<"One root is obtained at:"<<k<<" is
\t"<<x1;
cout<<"\n";
}while(fabs(x1-x0)>eps);
cout<<"Root is:" <<x0;
return 0;
}