Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4. #define EPS 1e-7
  5. using namespace std;
  6. double func(double x)
  7. {
  8. return x*x-3*x+2;
  9. }
  10. double fb(double x)
  11. {
  12. return 2*x-3;
  13. }
  14. double bisection(double l,double r)
  15. {
  16. if(func(l)*func(r)>0)
  17. cout << "Answer is not in [l,r]\n";
  18. else
  19. {
  20. double mid = l;
  21. while(r-l>=EPS)
  22. {
  23. mid = (l+r)/2;
  24. if(fabs(func(mid))<=EPS)
  25. return mid;
  26. else if(func(mid)*func(l)<0)
  27. r = mid;
  28. else
  29. l = mid;
  30. }
  31. }
  32. }
  33. double newton(double x)
  34. {
  35. while(fabs(func(x)) > EPS)
  36. x = x - func(x)/fb(x);
  37. return x;
  38. }
  39. int main()
  40. {
  41. cout << bisection(-3,1);
  42. //cout << newton(1.0);
  43. return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement