Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. #include<iomanip>
  4. #define e 0.000001
  5. using namespace std;
  6.  
  7. double f(double x)
  8. { return x*x*x*x*x - 5.0; }
  9.  
  10. double d_f(double x)
  11. {return 5*x*x*x*x; }
  12.  
  13. double newt(double x0)
  14. { double h;
  15. h=f(x0)/d_f(x0);
  16. while(abs(h)>=e)
  17. { x0=x0-h;
  18. h=f(x0)/d_f(x0);
  19. }
  20. return x0;
  21. }
  22.  
  23. double bisection(double a, double b)
  24. {
  25. if(f(a)*f(b)>0) cout<<"No solution";
  26. double c;
  27. c=(a+b)/2.0;
  28. while(abs(b-c)>=e)
  29. { if(f(a)*f(c)<0) b=c;
  30. else a=c;
  31. c=(a+b)/2.0;
  32. }
  33. return c;
  34. }
  35. double secant(double a, double b)
  36. { double x;
  37. x=(a*f(b) - b*f(a))/(f(b) - f(a));
  38. while(abs(x-a)>=e)
  39. { x=(a*f(b) - b*f(a))/(f(b) - f(a));
  40. a=b;
  41. b=x;
  42. }
  43. return x;
  44. }
  45.  
  46.  
  47. int main()
  48. {double x;
  49. double a,b;
  50. cout<<"Enter approximate root: "<<endl;
  51. cin>>x;
  52. cout<<"Enter the interval of possible root range:"<<endl;
  53. cin>>a>>b;
  54. cout<<setprecision(10)<<"Newton Raphson Result: "<<newt(x)<<endl;
  55. cout<<setprecision(10)<<"Bisection Result: "<<bisection(a,b)<<endl;
  56. cout<<setprecision(10)<<"Secant Result: "<<secant(a,b)<<endl;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement