Method of False position

/*
Q1. Write a C program to solve algebraic equations x3-2x-5=0 by using Method of False position. 
*/ 
#include<stdio.h>
#include<math.h>

float f(float);

main()
{
 float x0,x1,x2,err=0.00001; 
 printf("\nEnter the value of x0: ");
 scanf("%f",&x0);
 printf("\nEnter the value of x1: ");
 scanf("%f",&x1);
 if(f(x0)*f(x1)>0)
  printf("\nRoot does not lies between %f and %f",x0,x1);
 else
 {
  do
  {
   x2=x0-((f(x0)*(x1-x0))/(f(x1)-f(x0)));
   printf("\nx0=%f, f(x0)=%f, x1=%f, f(x1)=%f, x2=%f, f(x2)=%f",x0,f(x0),x1,f(x1),x2,f(x2));
   if(f(x0)*f(x2)<0)
   {
    x1=x2;
   }
   else
   {
    x0 = x2;
   }
  }while(fabs(f(x2))>err);
  printf("\n\nApp.root = %f",x2);
 }
}

float f(float x)
{
 return(x*x*x-2*x-5);
}

No comments:

Post a Comment