-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewton_Raphson.c
73 lines (56 loc) · 2.03 KB
/
Newton_Raphson.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EPSILON 1e-6
double evaluatePolynomial(double x, int degree, double coefficients[]) {
double result = 0;
for (int i = 0; i <= degree; i++) {
result += coefficients[i] * pow(x, degree - i);
}
return result;
}
double evaluatePolynomialDerivative(double x, int degree, double coefficients[]) {
double result = 0;
for (int i = 0; i < degree; i++) {
result += (degree - i) * coefficients[i] * pow(x, degree - i - 1);
}
return result;
}
double newtonRaphson(int degree, double coefficients[], double x0, int max_iterations) {
double x = x0;
int iterations = 0;
while (iterations < max_iterations) {
double fx = evaluatePolynomial(x, degree, coefficients);
double dfx = evaluatePolynomialDerivative(x, degree, coefficients);
double x_new = x - fx / dfx;
printf("Iteration %d:\n", iterations + 1);
printf("x%d = %.6f\n", iterations, x);
printf("f(x%d) = %.6f\n", iterations, fx);
printf("f'(x%d) = %.6f\n", iterations, dfx);
if (fabs(x_new - x) < EPSILON) {
return x_new;
}
x = x_new;
iterations++;
}
return x;
}
int main() {
int degree, max_iterations;
printf("Enter the highest degree of the polynomial: ");
scanf("%d", °ree);
double coefficients[degree + 1];
printf("Enter the coefficients of the polynomial (starting from highest degree to constant term):\n");
for (int i = 0; i <= degree; i++) {
printf("Coefficient for x^%d: ", degree - i);
scanf("%lf", &coefficients[i]);
}
double initial_guess;
printf("Enter your initial guess (x0): ");
scanf("%lf", &initial_guess);
printf("Enter the number of iterations: ");
scanf("%d", &max_iterations);
double root = newtonRaphson(degree, coefficients, initial_guess, max_iterations);
printf("Root found at: %.6f\n", root);
return 0;
}