Advertisement
Md_hosen_zisad

bisec2lab

Mar 24th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define E 0.0001
  3. using namespace std;
  4.  
  5. double func(double x){
  6.  
  7. return x*x*x-x-1;
  8. }
  9. int n=1;
  10. double c;
  11. void bisection(double a,double b){
  12.  
  13. if(func(a)*func(b)>0){
  14.  
  15.     cout<<"roots does not exit"<<endl;
  16.     return;
  17. }
  18.  
  19. while ((b-a)>=E){
  20.       c=(a+b)/2;
  21.     if(func(c)*func(a)<0){
  22.         cout<<"approximation is:"<<n<<" : "<<c<<endl;
  23.         //cout<<"functional value:"<<c*c*c-c-1<<endl;
  24.         b=c;
  25.     }
  26.     else{
  27.           cout<<"approximation is:"<<n<<" : "<<c<<endl;
  28.         //cout<<"functional value:"<<c*c*c-c-1<<endl;
  29.  
  30.         a=c;}
  31.  
  32. n++;
  33. cout<<endl;
  34. }}
  35.  
  36. int main(){
  37. cout<<"please enter input:";
  38. double a,b;
  39. cin>>a>>b;
  40. bisection(a,b);
  41. cout<<"the required root="<<c<<endl;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement