tampurus

Unit 1.4 Regula Falsi Method

Mar 26th, 2022 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define f(x) ((x*x*x) - (9*x) + 1)
  5. int main()
  6. {
  7.     float x0,x1,e,x2,y0,y1,y2;
  8.     int i=0;
  9.     printf("Enter the value of x0,x1,e\n");
  10.     scanf("%f %f %f",&x0,&x1,&e);
  11.     y0 = f(x0);
  12.     y1 = f(x1);
  13.     if(y1*y0>0){
  14.         printf("\nIntial values are not correct");
  15.         return 0;
  16.     }
  17.     do{
  18.         x2=(x0*y1-x1*y0)/(y1-y0);
  19.         y2=f(x2);
  20.         i++;
  21.        
  22.         if(y2*y0>0){
  23.             x0 = x2;
  24.             y0 = y2;
  25.         }
  26.         else{
  27.             x1 = x2;
  28.             y1 = y2;
  29.         }
  30.         printf("\nx%d = %f",i,x2);
  31.     }while(fabs(y2)>e);
  32.     printf("\nHence the root is %f",x2);
  33.     printf("\nTotal number of interations are %d",i);
  34.     return 0;
  35. }
  36.  
  37. /*
  38. Output
  39. Enter the value of x0,x1,e
  40. 2 3
  41. 0.001
  42. x1 = 2.900000
  43. x2 = 2.941555
  44. x3 = 2.942783
  45. Hence the root is 2.942783
  46. Total number of interations are 3
  47. */
Add Comment
Please, Sign In to add comment