Powered By Blogger

Tuesday, April 26, 2011

Numerical Analysis - Bisection Method


#include<stdio.h>
#include <math.h>
#include<conio.h>
#define ESP 0.001
float F(float);
void main()
{
  int i = 1;
  float x0,x1,x2;
  double f1,f2,f0,t;
//  clrscr();
  printf("\t\tBISECTION METHOD\n");
  printf("\nEnter the lower value: ");
  scanf("%f",&x0);

  printf("\nEnter the upper value: ");
  scanf("%f",&x1);
  printf("\n__________________________________________________________________________\n");
  printf("\nn\t x1\t   x2\t      f(x1)\t   f(x2)\t  xn\t  f2");
  printf("\n__________________________________________________________________________\n");
  do
  {
  x2=(x0+x1)/2;
  f0=F(x0);
  f1=F(x1);
  f2=F(x2);
  printf("\n%d     %f   %f    %f    %lf   %lf   %lf",i,x0,x1,f0,f1,x2,f2);
  if(f0*f2<0)
   {
    x1=x2;
   }
   else
   {
    x0=x2;
   }
   i++;
  }while(fabs(f2)>ESP);
printf("\n___________________________________________________________________________\n");
printf("\n\nThe Approximate Root= %f",x2);
getch();
}
float F(float x)
{
float exp;
exp=pow(x,3)-3*x+1.06;
return exp;
}

No comments:

Post a Comment