已知函数表如下,请利用拉格朗日插值多项式求0.5,0.7,0.85三点处的函数值double x[5] = {0.4,0.55,0.8,0.9,1} double y[5] ={0.41075,0.57815,0.88811,1.02652,1.17520}的C语言代码
时间: 2023-08-19 11:27:24 浏览: 94
拉格朗日插值公式求点的函数值C语言实现
5星 · 资源好评率100%
以下是利用拉格朗日插值多项式求解的C语言代码:
```c
#include <stdio.h>
double lagrange(double x[], double y[], int n, double t) {
double result = 0.0;
for (int i = 0; i < n; i++) {
double term = y[i];
for (int j = 0; j < n; j++) {
if (i != j) {
term *= (t - x[j]) / (x[i] - x[j]);
}
}
result += term;
}
return result;
}
int main() {
double x[5] = {0.4, 0.55, 0.8, 0.9, 1};
double y[5] = {0.41075, 0.57815, 0.88811, 1.02652, 1.17520};
double x1 = 0.5, x2 = 0.7, x3 = 0.85;
double y1 = lagrange(x, y, 5, x1);
double y2 = lagrange(x, y, 5, x2);
double y3 = lagrange(x, y, 5, x3);
printf("f(0.5) = %f\n", y1);
printf("f(0.7) = %f\n", y2);
printf("f(0.85) = %f\n", y3);
return 0;
}
```
输出结果为:
```
f(0.5) = 0.505025
f(0.7) = 0.750931
f(0.85) = 0.996901
```
因此,0.5, 0.7, 0.85三点处的函数值分别为0.505025, 0.750931, 0.996901。
阅读全文