Bisection Method(Numerical Method) C++ Programming
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define f(x) 3*x*x-6*x+2
int main()
{
float x1,x2,x0,f1,f2,f0,e=1;
cout<< "enter the initial guess x1 and x2:";
cin>>x1>>x2;
do
{
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
{
cout<< "the root does not bracket the root";
exit(0);
}
else
{
x0=(x1+x2)/2; //working formula
f0=f(x0);
e=fabs((x2-x1)/x2);
if(f1*f0<0)
{
x2=x0;
}
else
{
x1=x0;
}
}
}while(e>=0.0001&&f0!=0);
cout<<"\nthe root of the equation ="<<x0;
return 0;
}
#include<stdlib.h>
#include<math.h>
using namespace std;
#define f(x) 3*x*x-6*x+2
int main()
{
float x1,x2,x0,f1,f2,f0,e=1;
cout<< "enter the initial guess x1 and x2:";
cin>>x1>>x2;
do
{
f1=f(x1);
f2=f(x2);
if(f1*f2>0)
{
cout<< "the root does not bracket the root";
exit(0);
}
else
{
x0=(x1+x2)/2; //working formula
f0=f(x0);
e=fabs((x2-x1)/x2);
if(f1*f0<0)
{
x2=x0;
}
else
{
x1=x0;
}
}
}while(e>=0.0001&&f0!=0);
cout<<"\nthe root of the equation ="<<x0;
return 0;
}
OUTPUT
here we can find the root of the equation 3x2 -6x+2 by using bisection method.
Post a Comment