Secant Method(Numerical Method) C++ Programming
#include<iostream>
#include<math.h>
using namespace std;
#define f(x) 3*x*x-6*x+2 //equation
int main()
{
float x0,x1,x2,f0,f1,f2=1,e=1;
cout<< "enter the initial guess:";
cin>>x0>>x1;
while(e>0.005 && f2!=0)
{
f0=f(x0);
f1=f(x1);
x2=(x1-(f1*(x1-x0)/(f1-f0))); //working formula
f2=f(x2);
e=fabs((x1-x0)/x1);
x0=x1;
x1=x2;
}
cout<< "\nthe root of the equation="<<x2;
return 0;
}
#include<math.h>
using namespace std;
#define f(x) 3*x*x-6*x+2 //equation
int main()
{
float x0,x1,x2,f0,f1,f2=1,e=1;
cout<< "enter the initial guess:";
cin>>x0>>x1;
while(e>0.005 && f2!=0)
{
f0=f(x0);
f1=f(x1);
x2=(x1-(f1*(x1-x0)/(f1-f0))); //working formula
f2=f(x2);
e=fabs((x1-x0)/x1);
x0=x1;
x1=x2;
}
cout<< "\nthe root of the equation="<<x2;
return 0;
}
OUTPUT
Here we can calculate the root of the equation 3x2 -6x+2 by using secant method.
Post a Comment