Bisection Assignment
Bisection Assignment
: 01
STATEMENT : FINDING THE ROOT OF A POLYNOMIAL EQUATION USING
BISECTION METHOD.
ALGORITHM :
Input: Taking an equation cos(x) – x*exp(x)+1 as the input and a and b from the user.
Output: Approximate value of the root with the minimum tolerable error put by the user.
Declaring a macro f(x) for the equation cos(x) – x*exp(x)+1.
Steps:
1. START.
2. Declaring all the float( x0, x1, x2, f0, f1, f2, e ) and integer(step = 1[initialized]) variables in the
main() function.
3. Ask the user to put the values of a and b and storing them in x0 and x1 respectively.
4. Set f0 = f(x0).
5. Set f1 = f(x1).
6. Check if f0*f1 is less than 0.0
6.1. Show a suitable message.
6.2. Exit program.
[End of If]
7. Ask the user to enter the tolerable error difference.
8. Store it in the variable e.
9. Print the column names in a table format.
10. Repeat he steps while fabs(f2) is greater than e.
10.1. Set x2 = (x0 + x1)/2.
10.2. Set f2=f(f2).
10.3. Print the values of step , x0, x1, x2, and f2 one after another respectively.
10.4. Check if f0*f2 is less that 0
a. Set x1 = x2 and f1=f2.
[End of if]
10.5. Else
a. Set x0 = x2 and f0=f2.
[End of else]
10.6. Set step = step + 1 .
[End of loop]
11. Print the root/final value of x2.
12. END.
__________________________________________________________________________________
SOURCE CODE :
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define f(x) cos(x) - x * exp(x)+1
void main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
up:
printf("\nEnter the value of a: ");
scanf("%f", &x0);
printf("\nEnter the value of b: ");
scanf("%f", &x1);
f0=f(x0);
f1=f(x1);
if( f0 * f1 > 0.0)
{
printf("\nIncorrect Initial Guesses.\n");
exit(0);
}
printf("\nEnter tolerable error: ");
scanf("%f", &e); printf("\
n___________________________________________________________________________\n");
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
printf("\
n___________________________________________________________________________\n");
do
{
x2 = (x0 + x1)/2;
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\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: %f", x2);
exit(0);
}
__________________________________________________________________________________
OUTPUT :
Output set : 1
Enter the value of a: 0
Enter the value of b: 1
Enter tolerable error: 0.0001
___________________________________________________
Step x0 x1 x2 f(x2)
___________________________________________________
1 0.000000 1.000000 0.500000 0.053222
2 0.500000 1.000000 0.750000 -0.856061
3 0.500000 0.750000 0.625000 -0.356691
4 0.500000 0.625000 0.562500 -0.141294
5 0.500000 0.562500 0.531250 -0.041512
6 0.500000 0.531250 0.515625 0.006475
7 0.515625 0.531250 0.523438 -0.017362
8 0.515625 0.523438 0.519531 -0.005404
9 0.515625 0.519531 0.517578 0.000545
10 0.517578 0.519531 0.518555 -0.002427
11 0.517578 0.518555 0.518066 -0.000940
12 0.517578 0.518066 0.517822 -0.000197
13 0.517578 0.517822 0.517700 0.000174
14 0.517700 0.517822 0.517761 -0.000012
Root is: 0.517761.
_________________________________
Signature of teacher
27.9.23