Need help with bisection method

Joined
Aug 12, 2022
Messages
6
Reaction score
0
Write a program to find the roots of / − () using the bisection method. The program should ask the user for initial estimates of and . The program should then print out the value of the root, accurate to 10e-9 . Additionally, make the program print out how many iterations (bisections) it takes to achieve the required accuracy.
 
Joined
Aug 12, 2022
Messages
6
Reaction score
0
Write a program to find the roots of x/5 − cos(x) using the bisection method. The program should ask the user for initial estimates of and . The program should then print out the value of the root, accurate to 10e-9 . Additionally, make the program print out how many iterations (bisections) it takes to achieve the required accuracy.

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
// Let the Function is : x^3-4x-4.224
double function(double x)
{
return (x/5 - cos(x));
}
int main()
{
double a,b,c;
double xmin=0,xmax=0;
printf("Enter a number for Xmin: ", xmin);
scanf("%lf", &xmin);

printf("\nEnter a number for Xmax: ", xmax);
scanf("%lf", &xmax);

while(function(xmin)function(xmax)>0)
{
xmin--;
xmax++;
}
if(function(xmax)function(xmax-1)<=0)
{
a=xmax-1;
b=xmax+1;
}
else
{
a=xmin-1;
b=xmin+1;
}
while(fabs(a-b)>=0.0001)
{
c=(a+b)/2;
printf("[%f %f %f]\n",a,b,c);
if(function(c)==0)
break;
else if(function(a)function(c)<0)
b=c;
else
a=c;
}
printf("The root is %.9f\n",c);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top