-
Notifications
You must be signed in to change notification settings - Fork 0
/
tp3-1.c
171 lines (131 loc) · 3.97 KB
/
tp3-1.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
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
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
#define START 0
#define END 3*PI
double fact(int);
double CalculateS(double, double, double, size_t);
/* A polynomial structure contains an non-negative degree,
* and a list of coefficients.
*/
typedef struct {
size_t degree;
double *coefficients;
} Polynomial;
/* Allocate a new polynomial of degree n. Because we have
* coefficients from 0 to n inclusive, we allocate a table of
* n+1 coefficients. All coefficients are initialized to 0.
*/
Polynomial* PolynomialNew(size_t degree) {
double *coefficients = calloc(degree+1, sizeof(double));
if (coefficients == NULL) {
fputs("Not enough memory to allocate new polynomial.\n", stderr);
abort();
}
Polynomial *p = malloc(sizeof(Polynomial));
if (p == NULL) {
fputs("Not enough memory to allocate new polynomial.\n", stderr);
abort();
}
p->degree = degree;
p->coefficients = coefficients;
return p;
}
/* Free the space taken by a polynomial. */
void PolynomialFree(Polynomial *p) {
free(p->coefficients);
free(p);
}
/* Given a value for x, evaluate the polynomial. Not used for
* interpolation polynomials.
*/
double PolynomialEvaluate(Polynomial *p, double x) {
double sum = 0.0;
for (size_t i = 0; i <= p->degree; ++i) {
sum += p->coefficients[i] * pow(x, i);
}
return sum;
}
/* Display a polynomial. */
void PolynomialPrint(Polynomial *p) {
for (size_t i = p->degree; i != (size_t)-1; --i) {
printf("%+gx^%d", p->coefficients[i], (int)i);
}
putchar('\n');
}
/* Using Newton-Gregory's method, allocate and return a new collocation
* polynomial of degree `degree` that passes through `degree+1` points
* in the interval [start, end].
*/
Polynomial* InterpolationPolynomialNew(double (*f)(double), size_t degree,
double start, double end) {
Polynomial *p = PolynomialNew(degree);
double table[degree + 1][degree + 1]; /* Containing Delta_y, Delta^2_y, ... */
double h = (end - start) / degree;
/* Initialize table */
for (size_t i = 0; i <= degree; ++i) {
table[0][i] = (*f)(start + i*h);
//printf(" >> f(%.6lf) = %.6lf\n", (start + i*h), table[0][i]);
}
/* Fill up the table */
for (size_t i = 1; i <= degree; ++i) {
for (size_t j = 0; j <= degree - i; ++j) {
table[i][j] = table[i-1][j+1] - table[i-1][j];
}
}
/*
for (size_t i = 0; i <= degree; ++i) {
for (size_t j = 0; j <= degree - i; ++j) {
printf("%+.5lf\t", table[i][j]);
}
putchar('\n');
}
*/
/* Assign coefficients in polynomial. */
for (size_t i = 0; i <= degree; ++i) {
p->coefficients[i] = table[i][0] / (fact(i) * pow(h, i));
}
return p;
}
double InterpolationPolynomialEval(Polynomial *p, double x, double start, double end) {
double y = 0.0;
double h = (end - start) / p->degree;
for (size_t i = 0; i <= p->degree; ++i) {
double t = 1.0;
for (size_t j = 0; j < i; ++j) {
t *= (x - (start + j*h));
}
y += p->coefficients[i] * t;
}
return y;
}
void InterpolationPolynomialPrint(Polynomial *p, double start, double end) {
double h = (end - start) / p->degree;
for (size_t i = p->degree; i != (size_t)-1; --i) {
printf("%+g", p->coefficients[i]);
for (size_t j = 0; j < i; ++j) {
printf("*(x - %g)", start + j*h);
}
}
putchar('\n');
}
double fact(int n) {
double p = 1.0;
for (int i = 1; i <= n; ++i) {
p *= i;
}
return p;
}
double f(double x) {
return sin(x);
}
int main(void) {
for (size_t i = 1; i <= 5; ++i) {
Polynomial *p = InterpolationPolynomialNew(&f, i, START, END);
printf("P%d(x) = ", (int)i);
InterpolationPolynomialPrint(p, START, END);
PolynomialFree(p);
}
return EXIT_SUCCESS;
}