/* PROGRAM FOR BISECTION METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define f(x) (x*x*x-3*x+1)
void main()
{
int step2,i,maxitr;
float a,x,b,error;
clrscr();
printf("\n BISECTIOM METHOD FOR FINDING ROOTS.");
printf("enter error and max.ittiration\n");
scanf("%f%d",&error,&maxitr);
step2:
printf("enter a and b such that f(a)*f(b)<0\n");
scanf("%f%f",&a,&b);
if (f(a)*f(b)>0)
goto step2;
for(i=0;i<maxitr;i++)
{
x=(a+b)/2;
printf("\n%diteration,root is %0.4f",i+1,x);
if (f(x)*f(b)<0)
a=x;
else
b=x;
if (fabs((b-a)/b)<=error)
{
printf("root is %0.4f\n",x);
getch();
exit(0);
}
}
printf("solution does not converge in%d iteration \n",maxitr);
getch();
}
/*OUTPUT
Enter Error and Max.iteration
.000001
39
Enter a and b such that f(a)*f(b)<0
1 iteration, Root is 5000.5000
2 iteration, Root is 2500.7500
3 iteration, Root is 1250.8750
4 iteration, Root is 625.9375
5 iteration, Root is 313.4688
6 iteration, Root is 157.2344
7 iteration, Root is 79.1179
8 iteration, Root is 40.0589
9 iteration, Root is 20.5293
10 iteration, Root is 10.7646
11 iteration, Root is 5.8823
12 iteration, Root is 3.4412
13 iteration, Root is 2.2206
14 iteration, Root is 1.6103
15 iteration, Root is 1.3051
16 iteration, Root is 1.4577
17 iteration, Root is 1.5340
18 iteration, Root is 1.4959
19 iteration, Root is 1.5149
20 iteration, Root is 1.5245
21 iteration, Root is 1.5292
22 iteration, Root is 1.5316
23 iteration, Root is 1.5328
24 iteration, Root is 1.5322
25 iteration, Root is 1.5319
26 iteration, Root is 1.5321
27 iteration, Root is 1.5321
28 iteration, Root is 1.5321
29 iteration, Root is 1.5321 Root is 1.5321*/
No comments:
Post a Comment