upsidedown

secant method

Aug 12th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. #define E 0.0001
  4. #define f(x) x*x*x*x+2*x*x-16*x+5
  5.  
  6. void main()
  7. {
  8.     int i=0,n;
  9.     float x1,x2,x3,f1,f2,f3;
  10.  
  11.     printf("enter number of iterations and initial values x1 and x2\n");
  12.     scanf("%d%f%f",&n,&x1,&x2);
  13.     printf("ITRATION\t  x1\t  x2\t  x3\n");
  14.  
  15.     while(i<n)
  16.     {
  17.  
  18.             f1=f(x1);
  19.             f2=f(x2);
  20.             x3=x2-((f2*(x2-x1))/(f2-f1));
  21.             f3=f(x3);
  22.  
  23.             printf("\n%d\t %.4f\t %.4f\t %.4f ",i+1,x1,x2,x3);
  24.  
  25.             if(fabs((x2-x1)/x2)<=E)
  26.             {
  27.                 printf("\nthe root is %f\n",x3);
  28.                 break;
  29.             }
  30.  
  31.             else
  32.             {
  33.                 x1=x2;
  34.                 f1=f2;
  35.                 x2=x3;
  36.                 f2=f3;
  37.            
  38.             }
  39.             i++;
  40.  
  41.     }
  42. }
  43.  
  44.  
  45. OUTPUT:
  46. enter number of iterations and initial values x1 and x2
  47. 100
  48. 0
  49. 1
  50. ITRATION          x1      x2      x3
  51.  
  52. 1        0.0000  1.0000  0.3846
  53. 2        1.0000  0.3846  0.3128
  54. 3        0.3846  0.3128  0.3267
  55. 4        0.3128  0.3267  0.3265
  56. 5        0.3267  0.3265  0.3265
  57. 6        0.3265  0.3265  0.3265
  58. the root is 0.326539
  59. Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment