Advertisement
rodan0818

Secant Method (Numerical Method)

May 15th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4. #define e 0.00001
  5. float func(float x){
  6.  return (x*x*x-2*x-5);
  7. }
  8. int main(){
  9. int i =0;
  10. float x1,x2,x3,fx1,fx2,fx3;
  11. printf("Enter You Guess for x1 and x2 \n");
  12. scanf("%f%f",&x1,&x2);
  13. do{
  14. fx1 = func(x1);
  15. fx2 = func(x2);
  16. x3 = ((x1*fx2)-(x2*fx1))/(fx2-fx1);
  17. fx3 = func(x3);
  18. if (i==0){
  19.  printf("x1 x2  x3  f(x1)     f(x2)     f(x3) \n ");
  20. }
  21. printf("%f  %f  %f  %f    %f        %f \n",x1,x2,x3,fx1,fx2,fx3);
  22. x1=x2;
  23. x2=x3;
  24. ++i;
  25. }while ((fabs(fx3))>e);
  26. printf("The Root Using Second Method %f \n",x3);
  27. return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement