Friday, 24 August 2012

Numerical Technique -Bisection Method

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 8
float f(float);
void main()
{
 float a,b,c,d,t;
 int i,j,n;
 printf("Enter the number of iterations:");
 scanf("%d",&n);
 for(i=-max;i<max;i++)
 {
  a=i;
  b=i+1;
  if(f(a)*f(b)<=0)
  {
    if(f(a)>f(b))
    {
     t=a;
     a=b;
     b=t;
    }
    printf("\nThe initial limits are:");
    printf("\na=%f\tb=%f",a,b);
    printf("\n\n      a     \tb     \tc    \tf(c)");
    for(j=0;j<n;j++)
    {
     c=(a+b)/2;
     printf("\n%10.6f%10.6f%10.6f%10.6f",a,b,c,f(c));
     if(f(c)<0)
     a=c;
     else
     b=c;
    }
    getch();
  }
 }
}
float f(float x)
{
 return(cos(x)-3*x+1);
}

No comments:

Post a Comment