数值分析实验报告五
一、实验目的
理解Hermite插值方法,掌握Hermite插值算法设计
二、实验内容
使用vc++编程,实现该方法,即Hermite插值法
三、实验步骤
#include
double herm(double x0,double x1,double y0,double y1,double h0,double g0,double g1,double x)
{
d oubl
e alp0,alp1,bta0,bta1,t;double s;
t=h0*h0;
a lp0=(x-x1)*(x-x1)*(h0+2*(x-x0))/t/h0;
a lp1=(x-x0)*(x-x0)*(h0-2*(x-x1))/t/h0;
b ta0=(x-x0)*(x-x1)*(x-x1)/t;
b ta1=(x-x1)*(x-x0)*(x-x0)/t;
s=y0*alp0+y1*alp1+g0*bta0+g1*bta1;
r eturn(s);
}
void main()
{
int n=7;double p0;double pn; double aa[8],bb[8],s=0;
double xx[8]={0.5,0.7,0.9,1.1,1.3,1.5,1.7,1.9};
double yy[8]={0.4794,0.6442,0.7833,0.8912,0.9636,0.9975,0.9917,0.9463};
double g[8];
int i;
double a[8],c[8],h[8];
cout<<"Please input p0 and pn"< cin>>p0;cin>>pn; for(i=0;i<=n-1;i++) { h[i]=xx[i+1]-xx[i]; c out<<"h["< } c[0]=1;g[0]=3*(yy[1]-yy[0])/h[0]-p0*h[0]/2; for( i=1;i<=n-1;i++) { a[i]=h[i]/(h[i]+h[i-1]); c[i]=1-a[i]; } for(i=1;i { c out<<"a["< c out<<"c["< } for( i=1;i<=n-1;i++) { g[i]=3*(c[i]*(yy[i+1]-yy[i])/h[i]+a[i]*(yy[i]-yy[i-1])/h[i-1]); } a[n]=1; g[n]=3*(yy[n]-yy[n-1])/h[n-1]+pn*h[n-1]/2; for(i=0;i<=n;i++) c out<<"g["< a a[0]=2;bb[0]=c[0]/aa[0];g[0]=g[0]/aa[0]; for(i=1;i<=n-1;i++) { a a[i]=2-a[i]*bb[i-1]; b b[i]=c[i]/aa[i]; g[i]=(g[i]-a[i]*g[i-1])/aa[i]; } a a[n]=2-a[n]*bb[n-1]; g[n]=(g[n]-a[n]*g[n-1])/aa[n]; for(i=n-1;i>=0;i--) { g[i]=g[i]-bb[i]*g[i+1]; } c out< for(i=0;i<=n;i++) cout<<"g["< double ss;double c0,c1,d0,d1,g0,g1,h1; double x0;cout<<"Please input interpolation point x0:"< if(x0>=0.5 && x0<0.7) { c0=xx[0];c1=xx[1];d0=yy[0];d1=yy[1];h1=h[0];g0=g[0];g1=g[1]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else if(x0>=0.7 && x0<0.9) { c0=xx[1];c1=xx[2];d0=yy[1];d1=yy[2];h1=h[1];g0=g[1];g1=g[2]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else if(x0>=0.9 && x0<=1.1) {c0=xx[2];c1=xx[3];d0=yy[2];d1=yy[3];h1=h[2];g0=g[2];g1=g[3]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else if(x0>=1.1 && x0<=1.3) {c0=xx[3];c1=xx[4];d0=yy[3];d1=yy[4];h1=h[3];g0=g[3];g1=g[4]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else if(x0>=1.3 && x0<=1.5) { c0=xx[4];c1=xx[5];d0=yy[4];d1=yy[5];h1=h[4];g0=g[4];g1=g[5]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else if(x0>=1.5 && x0<=1.7) { c0=xx[5];c1=xx[6];d0=yy[5];d1=yy[6]; h1=h[5];g0=g[5];g1=g[6]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else if(x0>=1.7 && x0<=1.9) { c0=xx[6];c1=xx[7];d0=yy[6];d1=yy[7];h1=h[6];g0=g[6];g1=g[7]; s s=herm(c0,c1,d0,d1,h1,g0,g1,x0); c out< } else c out<<"The data error,please input again!"< } 四、运行结果