已知函数表如下,请利用拉格朗日插值多项式求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-09-18 10:15:56 浏览: 128
拉格朗日插值公式求点的函数值C语言实现
5星 · 资源好评率100%
以下是利用拉格朗日插值多项式求解的C语言代码:
```
#include <stdio.h>
double lagrange(double x[], double y[], int n, double xi);
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 xi[3] = {0.5, 0.7, 0.85};
int n = 5;
for (int i = 0; i < 3; i++) {
double yi = lagrange(x, y, n, xi[i]);
printf("x = %.2f, y = %.5f\n", xi[i], yi);
}
return 0;
}
double lagrange(double x[], double y[], int n, double xi) {
double yi = 0;
for (int i = 0; i < n; i++) {
double li = 1;
for (int j = 0; j < n; j++) {
if (j != i) {
li *= (xi - x[j]) / (x[i] - x[j]);
}
}
yi += li * y[i];
}
return yi;
}
```
输出结果为:
```
x = 0.50, y = 0.49442
x = 0.70, y = 0.74002
x = 0.85, y = 0.96224
```
阅读全文