0% found this document useful (0 votes)
10 views2 pages

Question1 Bisection Method

The document provides a C program that implements the Bisection method to find a root of the equation cos(x) - x * exp(x) = 0. The program prompts the user for two initial guesses and a tolerable error, then iteratively calculates the midpoint and evaluates the function until the result is within the specified error. The output includes the steps taken and the final root value, which is approximately 0.518.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Question1 Bisection Method

The document provides a C program that implements the Bisection method to find a root of the equation cos(x) - x * exp(x) = 0. The program prompts the user for two initial guesses and a tolerable error, then iteratively calculates the midpoint and evaluates the function until the result is within the specified error. The output includes the steps taken and the final root value, which is approximately 0.518.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Question1: Write and run the program in any software to find a root of the equation

𝒄𝒐𝒔𝒙 − 𝒙𝒆𝒙 = 𝟎 by Bisection method correct up to 3D places.


C-Code:
/* Bisection Method */
#include<stdio.h>
#include<math.h>
#define f(x) cos(x) - x * exp(x)
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 0;
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
f0 = f(x0);
f1 = f(x1);
if(f0*f1>0)
{
printf("Incorrect Initial Guesses");
}
else
{
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = (x0 + x1)/2;
f2 = f(x2);
printf("%d\t\t%0.4f\t\t%0.4f\t\t%0.4f\t\t%0.4f\n",step, x0, x1, x2, f2);
if( f0 * f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}while(fabs(f2)>e);
printf("\nRoot is: %0.3f", x2);
}

}
Out-Put:
Enter two initial guesses:
0 1
Enter tolerable error:
0.0001

Step x0 x1 x2 f(x2)
0 0.0000 1.0000 0.5000 0.0532
1 0.5000 1.0000 0.7500 -0.8561
2 0.5000 0.7500 0.6250 -0.3567
3 0.5000 0.6250 0.5625 -0.1413
4 0.5000 0.5625 0.5313 -0.0415
5 0.5000 0.5313 0.5156 0.0065
6 0.5156 0.5313 0.5234 -0.0174
7 0.5156 0.5234 0.5195 -0.0054
8 0.5156 0.5195 0.5176 0.0005
9 0.5176 0.5195 0.5186 -0.0024
10 0.5176 0.5186 0.5181 -0.0009
11 0.5176 0.5181 0.5178 -0.0002
12 0.5176 0.5178 0.5177 0.0002
13 0.5177 0.5178 0.5178 -0.0000

Root is: 0.518

You might also like