Search This Blog

Monday 29 February 2016

Practical set 2 Bisection

#include<stdio.h>

float f(float x)
{
float fx;
fx = (x*x*x + 4*x - 9);
return fx;
}
void main()
{
float a,b,x=0,x1=0;
int i=0;
printf("\nEnter the a and b");
scanf("%f%f",&a,&b);
if(f(a)*f(b)>=0)
{
printf("\nIntreval is wrong");
getch();
}
do
{
x=(a+b)/2;
if(x==x1)
{

printf("\nroot is %f",x);
break;
}
x1=x;
i++;
if(f(a)*f(x)<0)
{
b=x;
}
else
{
a=x;
}
}while(f(a)*f(b)<0);

getch();
}



output :
enter a and b:3
2

root is 2.942820

No comments:

Post a Comment