Advertisement
namkongkirat

Bisection Method (Predefined function)

Dec 16th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4.  
  5. float function(float);
  6.  
  7. int main(){
  8.     float a, A,b, B, c, C, tc, m;
  9.     printf("Enter the two intial range:\n");
  10.     scanf("%f%f",&a,&b);
  11.     A=function(a);
  12.     B=function(b);
  13.     if((A*B)>0){
  14.         printf("\n\nPlease enter valid range.");
  15.         main();
  16.     }
  17.     tc=a;
  18.     do{
  19.         c=(a+b)/2;
  20.         C=function(c);
  21.         if((function(a)*function(c))<0){
  22.             b=c;}
  23.         else{
  24.             a=c;}
  25.         m=tc-c;
  26.         tc=c;
  27.     }while(fabs(m)>0.0009);
  28.     printf("\n\nThe root is: %.4f",c);
  29.     getch();
  30. }
  31.  
  32. float function(float x){
  33.     return((x*x)-(4*x)-10);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement