验证一次正确,验证二次不正确,请版主指点
#include#define maxsize 100
int main(void)
{
double x[maxsize],y[maxsize],aa,sum,p,a,b;
int i,k,n;
printf("please input a and b:\n");
scanf("%lf %lf",&a,&b);
printf("please input aa=\n");
scanf("%lf",&aa);
printf("please input n=\n");
scanf("%d",&n);
printf("please input x[k]=\n");
for(k=0;k<=n;k++)
{
printf("x[%d]=",k);
scanf("%lf",&x[k]);
}
printf("please input y[k]=\n");
for(k=0;k<=n;k++)
{
printf("y[%d]=",k);
scanf("%lf",&y[k]);
}
if(x[0]b)
{
printf("输入错误,请重新输入!");
return 0;
}
sum=0;
for(k=0;k<=n;k++)
{
p=1;
for(i=0;i<=n;i++)
{
if(i!=k)
{
p=p*(aa-x[i])/(x[k]-x[i]);
sum+=p*y[k];
}
}
}
printf("sum=%lf\n",sum);
return 0;
}
拉格朗日插值法
x0=0 x1=1 x2=2 x3=3 x4=4
y0=1 y1=0.95 y2=0.82 y3=0.71 y4=0.43
sum(x)=((x-x1)/(x0-x1))*y0+((x-x0)/x1-x0))*y1
sum(1.5)=((1.5-2)/(1-2))*0.95+((1.5-1)/(2-1))*0.82=0.885
二次插值:
sum(x)=((x-x1)*(x-x2))/((x1-x1)*(x0-x2))*y0+((x-x0)*(x-x2))/((x1-x0)*(x1-x2))*y2+((x-x0)*(x-x1))/((x2-x0)*(x2-x1))*y2
sum(1.5)=((1.5-1)*(1.5-2))/((0-1)*(0-2))*1+((1.5-0)*(1.5-2))/((1-0)*(1-2))*0.95+((1.5-0)*(1.5-1))/((2-0)*(2-1))*0.82=0.895
问题:验证一次插值的时候得到了0.885跟计算的一样,可是验证二次插值的时候结果跟0.895不一样,那么我的问题又会出现在那里?