差商差值
程序员文章站
2022-04-16 10:32:28
...
#include<stdio.h>
#include<math.h>
#define N 6
double fun(double *xi, double *f, double x)//前两个参数为定点,第三个参数为估值点
{
double data[N - 1][N - 1] = {0};//差商表
for (int col = 1; col <= N - 1; col++)//初始化差商表
{
for (int row = col; row <= N - 1; row++)
{
if (col == 1)//第一列
{
data[row - 1][col - 1] = (f[row - 1] - f[row]) / (xi[row - 1] - xi[row]);
}
else//非第一列
{
data[row - 1][col - 1]= (data[row - 2][col - 2] - data[row - 1][col - 2]) / (xi[row - col] - xi[row]);
}
}
}
double sum = f[0];//累和
for (int col = 1; col <= N - 1; col++)
{
double temp = 1;//累积
for (int i = 0; i < col; i++)
{
temp *= (x - xi[i]);
}
sum += data[col - 1][col - 1] * temp;
}
return sum;
}
int main()
{
double xi[N] = { 0.40,0.55,0.65,0.80,0.90,1.05 }, f[N] = { 0.41075,0.57815,0.69675,0.88811,1.02652,1.25382 };
printf("%lf ", fun(xi, f, 0.596));
getchar();
}
上一篇: 分组排序纠结者的福音来了