Bisection Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<conio.h> | |
#include<math.h> | |
#include<process.h> | |
using namespace std; | |
int main() | |
{ | |
int n; | |
float x1,x2,xmid,e; | |
float f(float); | |
cout<<"\nenter epsilon value"; | |
cin>>e; | |
cout<<"\nenter x1"; | |
cin>>x1; | |
cout<<"\nenter x2"; | |
cin>>x2; | |
if (f(x1)*f(x2)>0) | |
{ | |
cout<<"\n wrong onitial values"; | |
exit (0); | |
} | |
do | |
{ | |
xmid=(x1+x2)/2; | |
cout<<"\nx1 \t"<<x1; | |
cout<<"\nx2 \t"<<x2; | |
cout<<"\nxmid\t"<<xmid; | |
if(xmid==0) | |
{ | |
cout<<"root"<<xmid; | |
getch(); | |
exit (0); | |
} | |
if( f(x1)*f(xmid)<0 ) | |
{ | |
x2=xmid; | |
} | |
else | |
{ | |
x1=xmid; | |
} | |
n++; | |
} | |
while(fabs(x1-x2)>e); | |
cout<<"\n root is "<<xmid; | |
getch(); | |
return 0; | |
} | |
float f(float x) | |
{ | |
float fx=(x*x*x)-(x)-11; | |
return (fx); | |
} |
Comments
Post a Comment
Thanks for the comment and don't forget to subscribe.