Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define E 0.0001
- using namespace std;
- double func(double x){
- return x*x*x-x-1;
- }
- int n=1;
- double c;
- void bisection(double a,double b){
- if(func(a)*func(b)>0){
- cout<<"roots does not exit"<<endl;
- return;
- }
- while ((b-a)>=E){
- c=(a+b)/2;
- if(func(c)*func(a)<0){
- cout<<"approximation is:"<<n<<" : "<<c<<endl;
- //cout<<"functional value:"<<c*c*c-c-1<<endl;
- b=c;
- }
- else{
- cout<<"approximation is:"<<n<<" : "<<c<<endl;
- //cout<<"functional value:"<<c*c*c-c-1<<endl;
- a=c;}
- n++;
- cout<<endl;
- }}
- int main(){
- cout<<"please enter input:";
- double a,b;
- cin>>a>>b;
- bisection(a,b);
- cout<<"the required root="<<c<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement