UNKNOWN //************************************** // Name: root finder // Description:finds roots to an equation using methods of bisection, false position, secant, iteration and newton's method // By: Sophonias Berhanu // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //This code is copyrighted and has limited warranties. //Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.13806/lngWId.3/qx/vb/scripts/ShowCode.htm //for details. //************************************** #include<iostream.h> #include<conio.h> #include<math.h> #include<iomanip.h> long double funct(long double x) { long double y; y=(sin(x)-x+(0.5)); return y; } long double functI(long double x) //funct(x)=0 => x=sin(x)+0.5// {//fixed point iteration method// long double y; y=(sin(x)+(0.5)); return y; } long double functN_D(long double x) //derivative of funct(x)// { //newton-rapson method// long double y; y=(cos(x)-1); return y; } long double newton(long double x0,int m,long double e,long double sigma) { int i; long double x1; for (i=0;i<m;i++) { if (fabs(functN_D(x0))>sigma) { x1=x0-((funct(x0))/(functN_D(x0))); cout<<setw(20)<<x0<<setw(20)<<x1<<setw(25)<<setw(25)<<fabs(funct(x1))<<endl; x0=x1; } else if (!(fabs(functN_D(x0))>sigma)) break; } if ((fabs(funct(x0))<e) && (fabs(functN_D(x0))>sigma)) cout<<"\n\n\n"<<setw(25)<<x0<<" Is the approxmate root:"; else if (!(fabs((funct(x0)))<e) && (fabs(functN_D(x0))>sigma)) cout<<"\n\n\n"<<setw(25)<<"no convergence in "<<m<<" interations."; else if (!(fabs(functN_D(x0))>sigma)) cout<<"\n\n\n"<<setw(25)<<"Division by zero."; getch(); return 0; } long double Iteration(long double x,int m,long double e) { long double y; int i; for (i=0;i<m;i++) { y=functI(x); cout<<setw(20)<<x<<setw(20)<<y<<setw(25)<<setw(25)<<fabs(x-y)<<endl; x=y; } if (abs(funct(x))<e) cout<<"\n\n\n"<<setw(25)<<x<<" Is the approxmate root:"; else if(!abs((funct(x)<e))) cout<<"\n\n\n"<<setw(35)<<"No convergence in "<<m<<" iterations:"; getch(); return 0; } long double Secant(long double x0,long double x1,int m,long double e) { int i; long double x2; for (i=0;i<m;i++) { x2=x1-funct(x1)*((x1-x0)/(funct(x1)-funct(x0))); cout<<setw(20)<<x0<<setw(20)<<x1<<setw(25)<<setw(25)<<fabs(funct(x2))<<endl; x0=x1; x1=x2; } if(fabs(funct(x2))<e) cout<<"\n\n\n"<<setw(25)<<x2<<" Is the approxmate root:"; else cout<<"\n\n\n"<<setw(30)<<"Doesn't converge in "<<m <<" iterations."; getch(); return 0; } long double False_position(long double a,long double b,int m,long double e) { int i; long double ans; for(i=0;i<m;i++) { ans=a-funct(a)*(b-a)/(funct(b)-funct(a)); cout<<setw(20)<<a<<setw(20)<<b<<setw(25)<<setw(25)<<funct(ans)<<endl; if((funct(ans)*funct(b))<0) a=ans; else if ((funct(ans)*funct(a))<0) b=ans; } if (e>fabs(funct(ans))) cout<<"\n\n\n"<<setw(25)<<ans<<" Is the approxmate root:"; else if (e<fabs(funct(ans))) cout<<"\n\n\n"<<setw(30)<<"Doesn't converge in "<<m<<" iterations"; getch(); return 0; } long double Bisection(long double a,long double b,long double e) { long double c; c=(a+b)/2; cout<<setw(20)<<a<<setw(20)<<b<<setw(25)<<setw(25)<<fabs(c-a)<<endl; if (e>fabs(c-a)) { cout<<"\n\n\n"<<setw(25)<<c<<" Is the approxmate root:"; getch(); } else if ((funct(c)*funct(a))<0) return Bisection(a,c,e); else if ((funct(c)*funct(b))<0) return Bisection(c,b,e); } void newton_entry() { // x0=1 m=4 e=0.00005 sigma=0.0000000000005 clrscr(); long double x0,e,sigma; int m; cout<<"\n"<<setw(70)<<"Enter the starting value x0, maximum number of iterations m,\n"; cout<<setw(71)<<"the tolerance e and sigma for the denominator's tolerance.\n\n\n"; cin>>x0>>m>>e>>sigma; cout<<setw(20)<<"x0"<<setw(20)<<"x1"<<setw(25)<<setw(25)<<"Absolut Error:"<<endl; newton(x0,m,e,sigma); getch(); } void Iteration_entry() { //x0=0m=8e=.00005 clrscr(); long double x0,e; int m; cout<<"\n"<<setw(65)<<"Enter the the starting value x0, maximum number\n"; cout<<setw(60)<<"of iterations M and the tolerance e.\n\n\n"; cin>>x0>>m>>e; cout<<setw(20)<<"x0"<<setw(20)<<"g(x0)"<<setw(25)<<setw(25)<<"Absolut Error:"<<endl; Iteration(x0,m,e); getch(); } void Secant_entry() { //a=1.49b=1.48 m=4e=.00005 inputs clrscr(); long double a,b,m,e; cout<<"\n"<<setw(70)<<"Enter the interval (a,b), maximum number of iterations M\n"; cout<<setw(50)<<"and the tolerance e:"; cout<<".\n\n\n"; cin>>a>>b>>m>>e; cout<<setw(20)<<"f("<<a<<")="<<funct(a)<<endl; cout<<setw(20)<<"f("<<b<<")="<<funct(b)<<"\n\n\n\n"; cout<<setw(20)<<"a"<<setw(20)<<"b"<<setw(25)<<setw(25)<<"Absolut Error:"<<endl; Secant(a,b,m,e); getch(); } void False_position_entry() { //a=1.49b=1.50 m=4e=.00005 inputs clrscr(); long double a,b,m,e; cout<<"\n\n\n\n"<<setw(60)<<"Enter the interval (a,b), maximum number of \n"; cout<<setw(55)<<"iterations M and the tolerance e.\n\n\n"; cin>>a>>b>>m>>e; if (funct(a)*funct(b)<0) { cout<<setw(20)<<"f("<<a<<")="<<funct(a)<<endl; cout<<setw(20)<<"f("<<b<<")="<<funct(b)<<"\n\n\n\n"; cout<<setw(20)<<"a"<<setw(20)<<"b"<<setw(25)<<setw(25)<<"Absolut Error:"<<endl; False_position(a,b,m,e); } else cout<<setw(30)<<"Wrong interval\n\n\n"; getch(); } void Bisection_entry() { //a=1.49b=1.50 e=.00005 inputs clrscr(); long double a,b,e; cout<<"\n\n\n\n"<<setw(60)<<"Enter the interval (a,b) and the tolerance e.\n\n\n"; cin>>a>>b>>e; if (funct(a)*funct(b)<0) { cout<<setw(20)<<"f("<<a<<")="<<funct(a)<<endl; cout<<setw(20)<<"f("<<b<<")="<<funct(b)<<"\n\n\n\n"; cout<<setw(20)<<"a"<<setw(20)<<"b"<<setw(25)<<setw(25)<<"Absolut Error:"<<endl; cout<<"\n"; Bisection(a,b,e); } else cout<<setw(30)<<"Wrong interval\n\n\n"; getch(); } void main() { long double a,b,x0,x1,e,sigma; int m,choice; clrscr(); do{ cout<<setw(65)<<"Enter the choice you want to solve the problem with:"; cout<<"\n\n\n"; cout<<setw(35)<<"Problem definition:"; cout<<setw(15)<<"y=sin(x)-x+0.5"; cout<<"\n\n\n"; cout<<setw(28)<<"1- Bisection Method:"; cout<<"\n\n\n"; cout<<setw(33)<<"2- False position Method:"; cout<<"\n\n\n"; cout<<setw(25)<<"3- secant Method:"; cout<<"\n\n\n"; cout<<setw(40)<<"4- Fixed Point Iteration Method:"; cout<<"\n\n\n"; cout<<setw(32)<<"5- Newton-rapson Method:"; cout<<"\n\n\n"; cout<<setw(19)<<"6- To Exit:"; cout<<"\n\n\n"; cin>>choice; switch (choice) { case 1: Bisection_entry(); Bisection(a,b,e); break; case 2: False_position_entry(); False_position(a,b,m,e); break; case 3: Secant_entry(); Secant(x0,x1,m,e); break; case 4: Iteration_entry(); Iteration(x0,m,e); break; case 5: newton_entry(); newton(x0,m,e,sigma); break; getch(); } }while (choice!=6); }