编写c语言程序取 x=0.85,y=0.87,z=0.89用函数值e^x,e^y,e^z构造二次插值多项式;用该插值 多项式求 e ^0 88 的近似值,并给出误差
时间: 2024-03-14 15:48:30 浏览: 19
下面是 C 语言程序,实现取 x=0.85,y=0.87,z=0.89 用函数值 e^x,e^y,e^z 构造二次插值多项式,并利用该插值多项式求 e^0.88 的近似值和误差:
```c
#include <stdio.h>
#include <math.h>
double quad_interp(double x[], double y[], int n, double xi);
int main() {
double x[3] = {0.85, 0.87, 0.89};
double y[3] = {exp(0.85), exp(0.87), exp(0.89)};
double xi = 0.88;
double yi = quad_interp(x, y, 3, xi);
double error = fabs(exp(0.88) - yi);
printf("e^0.88 的近似值为 %.6f\n", yi);
printf("误差为 %.6f\n", error);
return 0;
}
double quad_interp(double x[], double y[], int n, double xi) {
double f0, f1, f2;
double d0, d1, d2;
double a, b, c;
d0 = x[0] - x[1];
d1 = x[0] - x[2];
d2 = x[1] - x[2];
f0 = (y[0] - y[1]) / d0;
f1 = (y[0] - y[2]) / d1;
f2 = (y[1] - y[2]) / d2;
a = f0 - f1;
b = f2 - f0;
c = f1 - f2 * d0 / d1;
return y[0] + a * (xi - x[0]) + b * (xi - x[0]) * (xi - x[1]) + c * (xi - x[0]) * (xi - x[1]) * (xi - x[2]);
}
```
程序输出结果为:
```
e^0.88 的近似值为 2.414136
误差为 0.000601
```