forked from yourtion/LearningMasteringAlgorithms-C
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpol.c
75 lines (47 loc) · 1.22 KB
/
interpol.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
//
// interpol.c
// Algorithms - Interpolate
//
// Created by YourtionGuo on 16/05/2017.
// Copyright © 2017 Yourtion. All rights reserved.
//
#include <stdlib.h>
#include <string.h>
#include "nummeths.h"
#pragma mark - Public
int interpol(const double *x, const double *fx, int n, double *z, double *pz, int m)
{
double term, *table, *coeff;
int i, j, k;
/// 为差商以及待确定的系数分配存储空间
if ((table = (double *)malloc(sizeof(double) * n)) == NULL) return -1;
if ((coeff = (double *)malloc(sizeof(double) * n)) == NULL) {
free(table);
return -1;
}
/// 初始化系数
memcpy(table, fx, sizeof(double) * n);
/// 确定插值多项式的系数
coeff[0] = table[0];
for (k = 1; k < n; k++) {
for (i = 0; i < n - k; i++) {
j = i + k;
table[i] = (table[i + 1] - table[i]) / (x[j] - x[i]);
}
coeff[k] = table[0];
}
free(table);
/// 计算插值多项式在特定点的值
for (k = 0; k < m; k++) {
pz[k] = coeff[0];
for (j = 1; j < n; j++) {
term = coeff[j];
for (i = 0; i < j; i++) {
term = term * (z[k] - x[i]);
}
pz[k] = pz[k] + term;
}
}
free(coeff);
return 0;
}