-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregula_falsi.c
54 lines (45 loc) · 1.6 KB
/
regula_falsi.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
#include <stdio.h>
#include <math.h>
// Function to evaluate the polynomial
double f(double coefficients[], int degree, double x) {
double result = 0.0;
for (int i = 0; i <= degree; i++) {
result += coefficients[i] * pow(x, degree - i);
}
return result;
}
// Regula Falsi method to find the root
double regulaFalsi(double coefficients[], int degree, double a, double b, int maxIterations) {
double c;
for (int i = 0; i < maxIterations; i++) {
// Calculate c using Regula Falsi method
c = (a * f(coefficients, degree, b) - b * f(coefficients, degree, a)) / (f(coefficients, degree, b) - f(coefficients, degree, a));
// Update the interval [a, b]
if (f(coefficients, degree, c) * f(coefficients, degree, a) < 0) {
b = c;
} else {
a = c;
}
}
return c;
}
int main() {
int degree;
printf("Enter the degree of the polynomial: ");
scanf("%d", °ree);
double coefficients[degree + 1];
printf("Enter the coefficients of the polynomial starting from the highest degree term:\n");
for (int i = degree; i >= 0; i--) {
printf("Coefficient of x^%d: ", i);
scanf("%lf", &coefficients[i]);
}
double a, b;
printf("Enter the initial interval [a, b]: ");
scanf("%lf %lf", &a, &b);
int maxIterations;
printf("Enter the maximum number of iterations: ");
scanf("%d", &maxIterations);
double root = regulaFalsi(coefficients, degree, a, b, maxIterations);
printf("Approximate root: %lf\n", root);
return 0;
}