-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiteration.c
More file actions
65 lines (65 loc) · 978 Bytes
/
Copy pathiteration.c
File metadata and controls
65 lines (65 loc) · 978 Bytes
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
#include <stdio.h>
#include <math.h>
float f(float x)
{
return cos(x) - (3 * x) + 1;
}
float f_modify(float x)
{
return (cos(x) + 1) / 3.0;
}
float f_d(float x)
{
return (-sin(x) / 3.0);
}
int main()
{
float a, b;
int flag = 0;
do
{
printf("\nEnter the values of a and b : \n");
scanf("%f%f", &a, &b);
if ((f(a) * f(b)) < 0)
{
flag = 1;
printf("Two roots lie b/w %f and %f \n", a, b);
}
else
{
printf("Invalid Input. Try Again!!! \n");
}
} while (flag == 0);
float x0 = 0;
if (fabs(f(a)) < fabs(f(b)))
{
x0 = a;
}
else
{
x0 = b;
}
if (fabs(f_d(x0)) < 1)
{
printf("Roots found!\n");
}
else
{
printf("Wrong value of x0! \n");
return 0;
}
float err;
printf("\nEnter allowed error : \n");
scanf("%f", &err);
float x1 = x0;
int i = 0;
do
{
x0 = x1;
x1 = f_modify(x0);
i++;
printf("Root at iteration %d = %f\n", i, x1);
} while (fabs(x0 - x1) > err);
printf("Final root after %d iterations = %f\n", i, x1);
return 0;
}