Advertisement
Rakibul_Ahasan

Bisection_Method

Feb 20th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // This Function: x^3 + 3x- 5;
  5. double func(double x){
  6.     double f;
  7.     f=pow(x,3)+(3*x)-5;  // Calculate Function
  8.     return f;
  9. }
  10.  
  11. int main()
  12. {
  13.     // E-> Stopping criterion
  14.     double a,b,E;
  15.     cin>>a>>b>>E;
  16.    
  17.     if(func(a)*func(b)>=0) goto Stop;  // jump return 0;
  18.     else {
  19.         Step:
  20.         double Root;
  21.         Root=(a+b)/2.0;
  22.        
  23.         if(func(a)*func(Root)<0) b=Root;
  24.         else{
  25.             a=Root;
  26.         }
  27.        
  28.         if(abs((b-a)/b) >E) {   // relative Error
  29.             cout<<"Iteative:"<<Root<<"\n";
  30.             goto Step; // jump up step;
  31.         }
  32.         else {
  33.             Root=(a+b)/2.0;
  34.             cout<<"Root:"<<Root<<"\n";
  35.             goto Stop; // jump return 0;
  36.         }
  37.     }
  38.    
  39.     Stop:
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement