Bisection Method
Write C program of the equation x = cos x, using the bisection method.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float fun(float x)
{
return(cos(x)-x);
}
void main()
{
float a,b,eps,x;
int i=0;
clrscr();
printf("\n Enter the Lower and Upper limit:");
scanf("%f%f",&a,&b);
printf("\n Enter the epsilon value:");
scanf("%f",&eps);
if((fun(a)*fun(b))>0)
printf("\n starting value is unsuitable");
else
while(fabs((b-a)/b)>eps)
{
x=(a+b)/2;
i++;
if((fun(x)*fun(a))>0)
a=x;
else
b=x;
}
printf("\n\t Soultion Converges to a root.\n");
printf("\n Number of intration:%d\n",i);
printf("%f\n\t",x);
getch();
}
No comments:
Post a Comment