-
Notifications
You must be signed in to change notification settings - Fork 0
/
Divided_diff.c
92 lines (77 loc) · 2.33 KB
/
Divided_diff.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
int factorial(int n) {
if (n <= 1)
return 1;
else
return n * factorial(n - 1);
}
void dividedDifferences(double x[], double y[], int n, double dd[][n]) {
int i, j;
for (i = 0; i < n; i++) {
dd[i][0] = y[i];
}
for (j = 1; j < n; j++) {
for (i = 0; i < n - j; i++) {
dd[i][j] = (dd[i + 1][j - 1] - dd[i][j - 1]) / (x[i + j] - x[i]);
}
}
}
double computeFX(double x[], double dd[][4], int n, double value) {
double result = 0;
double term = 1;
for (int i = 0; i < n; i++) {
double product = 1;
for (int j = 0; j < i; j++) {
product *= (value - x[j]);
}
result += dd[0][i] * product;
}
return result;
}
double computeDerivative(double x[], double dd[][4], int n, double value) {
double result = 0;
for (int i = 0; i < n - 1; i++) {
double term = dd[0][i];
for (int j = 0; j < i; j++) {
term *= (value - x[j]);
}
result += term * (i + 1) / factorial(i + 1);
}
return result;
}
double computeSecondDerivative(double x[], double dd[][4], int n, double value) {
double result = 0;
for (int i = 0; i < n - 2; i++) {
double term = dd[0][i];
for (int j = 0; j < i; j++) {
term *= (value - x[j]);
}
result += term * (i + 1) * (i + 2) / factorial(i + 1);
}
return result;
}
int main() {
int n;
printf("Enter the number of data points: ");
scanf("%d", &n);
double x[n], y[n];
printf("Enter the data points (x, y):\n");
for (int i = 0; i < n; i++) {
printf("x[%d] = ", i);
scanf("%lf", &x[i]);
printf("y[%d] = ", i);
scanf("%lf", &y[i]);
}
double dd[n][n];
dividedDifferences(x, y, n, dd);
double value;
printf("\nEnter the value at which you want to find f(x): ");
scanf("%lf", &value);
double fx = computeFX(x, dd, n, value);
double derivative = computeDerivative(x, dd, n, value);
double secondDerivative = computeSecondDerivative(x, dd, n, value);
printf("\nf(%lf) = %lf\n", value, fx);
printf("f'(%lf) = %lf\n", value, derivative);
printf("f''(%lf) = %lf\n", value, secondDerivative);
return 0;
}