Sunteți pe pagina 1din 2

Solution to Problem Set 4 Question No.

1(ii) :

Program:

/* Program to calculate a real root of the equation sinx = 5*x - 2 using bisection method */

#include<stdio.h>
#include<math.h>

void main()
{
float f(float);
float a,b,c;

clrscr();

do
{
printf("\nEnter the lower bound of the interval:");
scanf("%f",&a);
printf("\nEnter the upper bound of the interval:");
scanf("%f",&b);
if( f(a) * f(b) >= 0)
{
printf("\nINVALID INTERVAL");
}
}
while( f(a) * f(b) >= 0 );

c=(a+b)/2;

while(fabs(f(c)) > 0.00001)


{
if( f(c)*f(a) < 0 )
{
b=c;
}
if( f(c)*f(b) < 0 )
{
a=c;
}
c= (a+b)/2;
}

printf("\nThe approximate value of the root is %f",c);


getch();
}

float f( float y)
{
return sin(y) - 5*y + 2;
}

S-ar putea să vă placă și