forked from DilipKumarGangwar/Numerical-Methods-CBNST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Iteration_Method.c
73 lines (64 loc) · 1.57 KB
/
Iteration_Method.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//C Program to Implement Iteration Method
#include<stdio.h>
#include<math.h>
#define EPSILON 0.001
float f(float x)
{
return x*x*x + x*x -1;
}
float findValueAt(float x)
{
return 1/sqrt(1+x);
}
float differentiate(float x)
{
return 1/2.0 * (pow(1,pow((1+x),3/2.0)));
}
int main()
{
int maxIteration,i;
float a,b,x,x0;
printf("Enter Maximum no of Iterations\n");
scanf("%d",&maxIteration);
//......Compute a and b.............
do
{
printf("Enter the value of a and b(starting boundary)");
scanf("%f%f",&a,&b);
if(f(a)*f(b)>0)
{
printf("Boundary Values are Invalid\n");
continue;
}
else
{
printf("Roots Lie between %f and %f\n",a,b);
break;
}
} while(1);
//find x0
x0= (a+b)/2;
//check if the function form used is valid or invalid
if(fabs(differentiate(x0))<1)
printf("Function Form is Correct. Iteration Method can be applied\n");
else
{
printf("Function Form is NOT Correct. Iteration Method CAN'T be applied\n");
return 0;
}
//Apply Successive approximation to find the root b/w a and b
//..........Find root............
for(i=1;i<=maxIteration;i++)
{
x = findValueAt(x0); //find the mid point
if(fabs(x-x0)<EPSILON)
{
printf("Iterations=%d Final Root=%f\n",i,x);
return 0;
}
printf("Iterations=%d Roots=%f\n",i,x);
x0=x;
}
printf("Root=%f Total Iterations=%d",x,--i);
return 0;
}