Sunday, 29 April 2012

Bisection method

#include <math.h>
#include<conio.h>
#define ESP 0.000001
#define F(x) tan(x)+1.45*(sin(x))*(sin(x))-1
void main()
{
  int i = 1;
  float x0,x1,x2;
  double f1,f2,f0,t;
  clrscr();
  printf("\nEnter the value of x0: ");
  scanf("%f",&x0);

  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  printf("\n__________________________________________________________________\n");
  printf("\niteration\t x0\t       x1\t x2\t   f0\t   f1\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,x2,f0,f1,f2);
  if(f0*f2<0)
    {
     x1=x2;
    }
    else
    {
     x0=x2;
    }
    i++;
  }while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %.6f",x2);
getch();
}

1 comment:

  1. **********Newton-Raphson Method***********

    #include
    #include
    #include
    float f(float x)
    {
    return(pow(x,3)+R*pow(x,2) -(R+1));
    }
    float df(float x)
    {
    return (3*pow(x,2) + 2*R*x );
    }
    void main()
    {
    float x0=0.1, x1, x2;
    clrscr();
    int count = 0;
    while(1)
    {
    x1 = x0 - f(x0)/df(x0);
    count++;
    if(x0==x1){
    break;
    }
    x0 = x1;
    }
    printf("The root after %d iteration is %0.6f",count, x0);
    getch();;
    }

    ReplyDelete