rootUser

Bisection (c)

May 24th, 2016
49
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 ESP 0.001
  4. #define F(x) (x)*(x)*(x) + (x)*(x) + (x) + 7
  5.  
  6. void main()
  7. {
  8.     int i = 1;
  9.     float x0,x1,x2;
  10.     double f1,f2,f0,t;
  11.  
  12.     printf("\nEnter the value of x0: ");
  13.     scanf("%f",&x0);
  14.  
  15.     printf("\nEnter the value of x1: ");
  16.     scanf("%f",&x1);
  17.     printf("\n__________________________________________________________________\n");
  18.     printf("\niteration\t x0\t       x1\t x2\t   f0\t   f1\t   f2");
  19.     printf("\n___________________________________________________________________\n");
  20.     do
  21.     {
  22.         x2=(x0+x1)/2;
  23.         f0=F(x0);
  24.         f1=F(x1);
  25.         f2=F(x2);
  26.         printf("\n%d %f %f %f %lf %lf %lf", i, x0,x1,x2,f0,f1,f2);
  27.         if(f0*f2<0)
  28.         {
  29.             x1=x2;
  30.         }
  31.         else
  32.         {
  33.             x0=x2;
  34.         }
  35.         i++;
  36.     }
  37.     while(fabs(f2)>ESP);
  38.     printf("\n__________________________________________________________\n");
  39.     printf("\n\nApp.root = %f",x2);
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment