/*
Write a C program to solve algebraic equation x2-x-1=0 by using Method of Bisection
*/
#include<stdio.h>
#include<math.h>
float f(float);
main()
{
float x,a,b,err=0.00001;
printf("\nEnter values of a and b=");
scanf("%f%f",&a,&b);
if(f(a)*f(b)>0)
printf("\nRoot does not lies between %f and %f",a,b);
else
{
do
{
x=(a+b)/2;
if(f(x)==0)
{
break;
}
else if(f(a)*f(x)<0)
b=x;
else
a=x;
}while(fabs(b-a)>=err);
printf("\nRoot of the equation=%f",x);
}
}
float f(float x)
{
return(x*x-x-1);
}
No comments:
Post a Comment