Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab Class No 1
- // Bisection Method using max iteration
- // Shachin shil
- #include<bits/stdc++.h>
- using namespace std;
- double my_fun(double x)
- {
- double fx=(x*x)-x-1;
- return fx;
- }
- double fun(double a,double b,double tolerance,int max_iter)
- {
- double prev,ans,x;
- for(int i=1;i<max_iter;i++)
- {
- x=(a+b)/2.00;
- ans=my_fun(x);
- if(ans<0 && my_fun(a)<0)
- {
- a=x;
- }
- else
- b=x;
- }
- return x;
- }
- int main()
- {
- float a,b,x,tolerance,root;
- int max_iter;
- cin>>a>>b>>tolerance>>max_iter;
- if(my_fun(a)*my_fun(b)>0)
- {
- cout<<"Invalid"<< endl;
- }
- else
- {
- root=fun(a,b,tolerance,max_iter);
- cout<<root<<endl;
- }
- return 0;
- }
- /*
- Input :
- 1
- 2
- .001
- 100
- Output :
- 1.61803
- /*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement