Educational Blog: FALSE POSITION Method in C

Tuesday, 18 November 2014

FALSE POSITION Method in C

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) 3*(x) - 1 - cos(x)
void main()
{
  float x0,x1,x2,f1,f2,f0;
  int count=0;
  clrscr();
  do
  {
  printf("\nEnter the value of x0: ");
  scanf("%f",&x0);
  }while(F(x0) > 0);
  do
  {
  printf("\nEnter the value of x1: ");
  scanf("%f",&x1);
  }while(F(x1) < 0);
  printf("\n__________________________________________________________\n");
  printf("\n    x0\t       x1\t x2\t   f0\t   f1\t   f2");
  printf("\n__________________________________________________________\n");
  do
  {
  f0=F(x0);
  f1=F(x1);
  x2=x0-((f0*(x1-x0))/(f1-f0));
  f2=F(x2);
  printf("\n%f %f %f %f %f %f",x0,x1,x2,f0,f1,f2);
  if(f0*f2<0)
   {
    x1=x2;
   }
   else
   {
    x0 = x2;
   }
  }while(fabs(f2)>ESP);
printf("\n__________________________________________________________\n");
printf("\n\nApp.root = %f",x2);
getch();
}


 -------------------- OUT PUT --------------------


Enter the value of x0: -1

Enter the value of x1: 1

__________________________________________________________

    x0         x1        x2        f0      f1      f2
__________________________________________________________

-1.000000 1.000000 0.513434 -4.540302 1.459698 -0.330761
0.513434  1.000000 0.603320 -0.330761 1.459698 -0.013497
0.603320  1.000000 0.606954 -0.013497 1.459698 -0.000527
0.606954  1.000000 0.607096 -0.000527 1.459698 -0.000021
__________________________________________________________


App.root = 0.607096

No comments:

Post a Comment