X
|
Y
|
0.40
|
0.41075
|
0.55
|
0.57815
|
0.65
|
0.69675
|
0.80
|
0.88811
|
0.90
|
1.02652
|
1.05
|
1.25382
|
#include
using namespace
std;
double
average_deviation(double* function_value,double*
independent_variable,int
end);
//声明均差函数
int main()
{
double
x[5]={0.40,0.55,0.65,0.80,0.90};
//用来存储已知x的值
double
y[5]={0.41075,0.57815,0.69675,0.88811,1.02652};
//用来存储已知y的值
double
result=0;
//用来保存最后的插值结果
double
input_x;
//插值的x值
double
polynomial;
//保存多项式的值
cout<<"通过插值得到的函数为:"<<endl;
for (int i=0;i<5;i++)
{
cout<<average_deviation(y,x,i);
for (int j=0;j
cout<<"*"<<"("<<"x-"<<x[j]<<")";
if (i<4)
cout<<"+";
}
cout<<endl;
cout<<"请输入插值的x值:";
cin>>input_x;
for (int i=0;i<5;i++)
{
polynomial=1;
for (int j=0;j
{
polynomial*=(input_x-x[j]);
}
result+=(average_deviation(y,x,i)*polynomial);
}
cout<<"插值得到的函数值为:"<<result<</span><</span>endl;
return 0;
}
double
average_deviation(double* function_value,double*
independent_variable,int end)
{
double*
function_value_f;
//function_value_f用来保存0到k-1的k阶均差
double*
function_value_b;
//function_value_f用来保存1到k的k阶均差
double*
independent_variable_f;
//用来保存前k-1个值
double*
independent_variable_b;
//用来保存后k-1个值
double value_f;
double value_b;
if (0==end)
{
return function_value[end];
}
else
{
function_value_f=new double[end];
independent_variable_f=new double[end];
function_value_b=new double[end];
independent_variable_b=new double[end];
for (int i=1;i<=end;i++)
{
function_value_f[i-1]=function_value[i];
independent_variable_f[i-1]=independent_variable[i];
}
for (int i=0;i<=end-1;i++)
{
function_value_b[i]=function_value[i];
independent_variable_b[i]=independent_variable[i];
}
value_f = average_deviation(function_value_f,
independent_variable_f, end - 1);
value_b = average_deviation(function_value_b,
independent_variable_b, end - 1);
delete[] function_value_f;
delete[] function_value_b;
delete[] independent_variable_b;
delete[] independent_variable_f;
return
(value_f-value_b)/(independent_variable[end]-independent_variable[0]);
}
}