Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- #define EPS 1e-7
- using namespace std;
- double func(double x)
- {
- return x*x-3*x+2;
- }
- double fb(double x)
- {
- return 2*x-3;
- }
- double bisection(double l,double r)
- {
- if(func(l)*func(r)>0)
- cout << "Answer is not in [l,r]\n";
- else
- {
- double mid = l;
- while(r-l>=EPS)
- {
- mid = (l+r)/2;
- if(fabs(func(mid))<=EPS)
- return mid;
- else if(func(mid)*func(l)<0)
- r = mid;
- else
- l = mid;
- }
- }
- }
- double newton(double x)
- {
- while(fabs(func(x)) > EPS)
- x = x - func(x)/fb(x);
- return x;
- }
- int main()
- {
- cout << bisection(-3,1);
- //cout << newton(1.0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement