Write a program to find the roots of equation ( x(Square) – sin x – 0.5) using bisection method

#include<stdio.h>
#include<conio.h>
#include<math.h>
float f (double x);

void main()
{
float xl,xu,xrold,xrnew;
xl=0.0;
xu=2.0;
xrold=(xl+xu)/2;
float EA=2;
while(EA>0.0000001)
{
if(f(xl)*f(xrold)<0)
xu=xrold;
if(f(xl)*f(xrold)>0)
xl=xrold;
xrnew=(xl+xu)/2;
EA=(xrnew-xrold)/xrnew;
if(EA<0)
EA=-EA;
xrold=xrnew;
}
printf(“%f, %f”,xrold,EA);
getch();
}
float f(double x)
{
return x*x-sin(x*11.0/630)-0.5;
}

This question is solved by using the Bisection Method. In this question we have lower point and upper point. So xl is representing the lower limit and xu is representing upper limit. When we put xl in the function then result will be negative and when we put xu in function the result will be positive.

This can be checked by ensuring that f(xl)(xu)<0.

An estimate of the root xr is determined by xr=(xl+xu)/2.

In this program we use xrold and xrnew. xrold will be replaced by xrnew when a new point replaced the xl or xu. xr representing the mid point of xl and xu. If the xr<0 then the value of xl is replaced by xr and if the xr>0 then the value of xu will be replaced by xr.

Thee f function is returning the result of x(square)-sin(x)-0.5.