已知函数表如下: x: 0.4 0.55 0.8 0.9 1 y: 0.41075 0.57815 0.88811 1.02652 1.17520写出用牛顿插值法求解0.5,0.7,0.85三处点的函数值p,并输出函数值p和差商表的c语言代码、
时间: 2024-03-27 08:35:50 浏览: 156
根据已知数据可以构建差商表如下:
| x | y | f[x, y] | f[x, x-0.4, y] | f[x, x-0.4, x-0.55, y] | f[x, x-0.4, x-0.55, x-0.8, y] | f[x, x-0.4, x-0.55, x-0.8, x-0.9, y] |
|-------|----------|-------------|----------------|------------------------|--------------------------------|------------------------------------|
| 0.4 | 0.41075 | | | | | |
| 0.55 | 0.57815 | 1.235 | | | | |
| 0.8 | 0.88811 | 1.5768 | 2.81125 | | | |
| 0.9 | 1.02652 | 1.72787 | 2.93949 | 3.68766 | | |
| 1 | 1.17520 | 1.88375 | 3.1002 | 4.23882 | 4.92026 | |
根据牛顿插值公式:
$p(x) = y_0 + f[x_0, x_1](x-x_0) + f[x_0, x_1, x_2](x-x_0)(x-x_1) + f[x_0, x_1, x_2, x_3](x-x_0)(x-x_1)(x-x_2) + f[x_0, x_1, x_2, x_3, x_4](x-x_0)(x-x_1)(x-x_2)(x-x_3)$
代入$x=0.5$得:
$p(0.5) = 0.41075 + 1.235 \times (0.5-0.4) + 2.81125 \times (0.5-0.4)(0.5-0.55) + 3.68766 \times (0.5-0.4)(0.5-0.55)(0.5-0.8) + 4.92026 \times (0.5-0.4)(0.5-0.55)(0.5-0.8)(0.5-0.9) = 0.49421$
代入$x=0.7$得:
$p(0.7) = 0.41075 + 1.235 \times (0.7-0.4) + 2.81125 \times (0.7-0.4)(0.7-0.55) + 3.68766 \times (0.7-0.4)(0.7-0.55)(0.7-0.8) + 4.92026 \times (0.7-0.4)(0.7-0.55)(0.7-0.8)(0.7-0.9) = 0.75823$
代入$x=0.85$得:
$p(0.85) = 0.41075 + 1.235 \times (0.85-0.4) + 2.81125 \times (0.85-0.4)(0.85-0.55) + 3.68766 \times (0.85-0.4)(0.85-0.55)(0.85-0.8) + 4.92026 \times (0.85-0.4)(0.85-0.55)(0.85-0.8)(0.85-0.9) = 1.02148$
以下是用C语言实现差商表和插值公式的代码:
```c
#include <stdio.h>
#define N 5
int main()
{
double x[N] = {0.4, 0.55, 0.8, 0.9, 1};
double y[N] = {0.41075, 0.57815, 0.88811, 1.02652, 1.17520};
double f[N][N] = {0};
int i, j, k;
// 构建差商表
for (i = 0; i < N; i++) {
f[i][i] = y[i];
}
for (j = 1; j < N; j++) {
for (i = j; i < N; i++) {
f[i][i-j] = (f[i][i-j+1] - f[i-1][i-j]) / (x[i] - x[i-j]);
}
}
// 输出差商表
printf("差商表:\n");
for (i = 0; i < N; i++) {
printf("%.2f\t", x[i]);
for (j = 0; j <= i; j++) {
printf("%.5f\t", f[i][j]);
}
printf("\n");
}
// 计算插值
double p, t;
double xs[] = {0.5, 0.7, 0.85};
int n = sizeof(xs) / sizeof(xs[0]);
for (k = 0; k < n; k++) {
p = y[0];
for (j = 1; j < N; j++) {
t = f[j][0];
for (i = 0; i < j; i++) {
t *= (xs[k] - x[i]);
}
p += t;
}
printf("p(%.2f) = %.5f\n", xs[k], p);
}
return 0;
}
```
阅读全文