Rakibul_Ahasan

Regular_Falsi_Method

Feb 23rd, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 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.     f=(3*x)-cos(x)-1;
  9.     return f;
  10. }
  11.  
  12. int main()
  13. {
  14.     // E-> Stopping criterion
  15.     double a,b,E;
  16.     cin>>a>>b>>E;
  17.    
  18.     if(func(a)*func(b)>=0) goto Stop;  // jump return 0;
  19.     else {
  20.         Step:
  21.         double Root;
  22.        // Root=(a+b)/2.0;
  23.        Root = (a*func(b) - b*func(a))/ (func(b) - func(a));
  24.        
  25.         if(func(a)*func(Root)<0) b=Root;
  26.         else{
  27.             a=Root;
  28.         }
  29.        
  30.         if(abs((b-a)/b) >E) {   // relative Error
  31.             cout<<"Iteative:"<<Root<<"\n";
  32.             goto Step; // jump up step;
  33.         }
  34.         else {
  35.             Root=(a+b)/2.0;
  36.             cout<<"Root:"<<Root<<"\n";
  37.             goto Stop; // jump return 0;
  38.         }
  39.     }
  40.    
  41.     Stop:
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment