Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- #include<iomanip>
- #define e 0.000001
- using namespace std;
- double f(double x)
- { return x*x*x*x*x - 5.0; }
- double d_f(double x)
- {return 5*x*x*x*x; }
- double newt(double x0)
- { double h;
- h=f(x0)/d_f(x0);
- while(abs(h)>=e)
- { x0=x0-h;
- h=f(x0)/d_f(x0);
- }
- return x0;
- }
- double bisection(double a, double b)
- {
- if(f(a)*f(b)>0) cout<<"No solution";
- double c;
- c=(a+b)/2.0;
- while(abs(b-c)>=e)
- { if(f(a)*f(c)<0) b=c;
- else a=c;
- c=(a+b)/2.0;
- }
- return c;
- }
- double secant(double a, double b)
- { double x;
- x=(a*f(b) - b*f(a))/(f(b) - f(a));
- while(abs(x-a)>=e)
- { x=(a*f(b) - b*f(a))/(f(b) - f(a));
- a=b;
- b=x;
- }
- return x;
- }
- int main()
- {double x;
- double a,b;
- cout<<"Enter approximate root: "<<endl;
- cin>>x;
- cout<<"Enter the interval of possible root range:"<<endl;
- cin>>a>>b;
- cout<<setprecision(10)<<"Newton Raphson Result: "<<newt(x)<<endl;
- cout<<setprecision(10)<<"Bisection Result: "<<bisection(a,b)<<endl;
- cout<<setprecision(10)<<"Secant Result: "<<secant(a,b)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement