-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterativeMethods.c
More file actions
162 lines (123 loc) · 3.8 KB
/
IterativeMethods.c
File metadata and controls
162 lines (123 loc) · 3.8 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Iterative methods for finding roots of f(x) = x³ - 3x + 1.06
#include <stdio.h>
#include <math.h>
float f(float x) {
return x*x*x - 3*x + 1.06;
}
float df(float x) {
return 3*x*x - 3;
}
void bisectionMethod(float a, float b, float tolerance) {
float error = 1.0;
float c, prev_c = 0.0;
int iteration = 0;
int max_iterations = 13;
printf("\nBisection Method\n");
printf("Iteration\tRoot\n");
printf("-------------------\n");
while (error > tolerance && iteration < max_iterations) {
iteration++;
c = (a + b) / 2.0;
if (iteration > 1) {
error = fabs(c - prev_c);
}
printf("%d\t\t%.3f\n", iteration, c);
if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
prev_c = c;
}
printf("\nRoot: %.3f\n", c);
}
void secantMethod(float x0, float x1, float tolerance) {
float error = 1.0;
float x2;
int iteration = 0;
int max_iterations = 6;
printf("\n=== Secant Method ===\n");
printf("Iteration\tRoot\n");
printf("-------------------\n");
while (error > tolerance && iteration < max_iterations) {
iteration++;
x2 = x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0));
error = fabs(x2 - x1);
printf("%d\t\t%.3f\n", iteration, x2);
x0 = x1;
x1 = x2;
}
printf("\nRoot: %.3f\n", x2);
}
void regulaFalsiMethod(float a, float b, float tolerance) {
float error = 1.0;
float c, prev_c = 0.0;
int iteration = 0;
int max_iterations = 4;
printf("\n=== Regula Falsi (False Position) Method ===\n");
printf("Iteration\tRoot\n");
printf("-------------------\n");
while (error > tolerance && iteration < max_iterations) {
iteration++;
c = (a * f(b) - b * f(a)) / (f(b) - f(a));
if (iteration > 1) {
error = fabs(c - prev_c);
}
printf("%d\t\t%.3f\n", iteration, c);
if (f(a) * f(c) < 0) {
b = c;
} else {
a = c;
}
prev_c = c;
}
printf("\nRoot: %.3f\n", c);
}
void newtonRaphsonMethod(float x, float tolerance) {
float error = 1.0;
float x_new;
int iteration = 0;
int max_iterations = 4;
printf("\n=== Newton-Raphson Method ===\n");
printf("Iteration\tRoot\n");
printf("-------------------\n");
while (error > tolerance && iteration < max_iterations) {
iteration++;
x_new = x - f(x) / df(x);
error = fabs(x_new - x);
printf("%d\t\t%.3f\n", iteration, x_new);
x = x_new;
}
printf("\nPositive Root: %.3f\n", x);
}
int main() {
float tolerance = 1e-4;
int choice;
printf("\n f(x) = x^3 - 3x + 1.06\n");
printf(" Interval: [0, 1]\n");
printf("Select a method:\n");
printf("1. Bisection Method\n");
printf("2. Secant Method\n");
printf("3. Regula Falsi (False Position)\n");
printf("4. Newton-Raphson Method\n");
printf("\nEnter your choice (1-4): ");
scanf("%d", &choice);
switch(choice) {
case 1:
bisectionMethod(0.0, 1.0, tolerance);
break;
case 2:
secantMethod(0.0, 1.0, tolerance);
break;
case 3:
regulaFalsiMethod(0.0, 1.0, tolerance);
break;
case 4:
newtonRaphsonMethod(0.5, tolerance);
break;
default:
printf("Invalid choice!\n");
return 1;
}
return 0;
}