C语言实现三次样条插值的子函数 void SPL(int n,double*x,double*y,int ni,double*xi,double*yi);是你所要。已知 n 个点 x,y;x 必须已按顺序排好。要插值 ni 点,横坐标 xi[],输出 yi[]。程序里用double 型,保证计算精度。SPL调用现成的程序。现成的程序很多。端点处理方法不同,结果会有不同。想同matlab比较,你需 尝试 调用 spline()函数 时,令 end1 为 1,设 slope1 的值,令 end2 为 1 设 slope2 的值。includeincludeint spline(int n,int end1,int end2,double slope1,double slope2,double x[],double y[],double b[],double c[],double d[],int*iflag){int nm1,ib,i,ascend;double t;nm1=n-1;iflag=0;if(n){/*no possible interpolation*/iflag=1;goto LeaveSpline;}ascend=1;for(i=1;i;i)if(x[i][i-1])ascend=0;if。ascend){iflag=2;goto LeaveSpline;}if(n>;=3){d[0]=x[1]-x[0];c[1]=(y[1]-y[0])/d[0];for(i=1;i;i){d[i]=x[i+1]-x[i];b[i]=2.0*(d[i-1]+d[i]);c[i+1]=(y[i+1]-y[i])/d[i];c[i]=c[i+1]-c[i];}Default End conditions*/b[0]=-d[0];b[nm1]=-d[n-2];c[0]=0.0;c[nm1]=0.0;if(n。3){c[0]=c[2]/(x[3]-x[1])-c[1]/(x[2]-x[0]);c[nm1]=c[n-2]/(x[nm1]-x[n-3。
三次样条插值计算步骤
求助:求三次样条插值函数的C++程序 #includeincludeusing namespace std;const int MAX=50;float x[MAX],y[MAX],h[MAX];float c[MAX],a[MAX],fxym[MAX];float f(int x1,int x2,int x3){float a=(y[x3]-y[x2])/(x[x3]-x[x2]);float b=(y[x2]-y[x1])/(x[x2]-x[x1]);return(a-b)/(x[x3]-x[x1]);}/求差分void cal_m(int n){/用追赶法求解出弯矩向量M…float B[MAX];B[0]=c[0]/2;for(int i=1;i;i+)B[i]=c[i]/(2-a[i]*B[i-1]);fxym[0]=fxym[0]/2;for(i=1;i;i+)fxym[i]=(fxym[i]-a[i]*fxym[i-1])/(2-a[i]*B[i-1]);for(i=n-1;i>;=0;i-)fxym[i]=fxym[i]-B[i]*fxym[i+1];}void printout(int n);int main(){int n,i;char ch;do{cout;cin>;>;n;for(i=0;i;i+){cout;cin>;>;x[i];cout;cout;cin>;>;y[i];cout;}for(i=0;i;i+)/求 步长h[i]=x[i+1]-x[i];cout输入边界条件\\n 1:已知两端的一阶导数\\n 2:两端的二阶导数已知\\n 默认:自然边界条件\\n;int t;float f0,f1;cin>;>;t;switch(t){case 1:cout;cin>;>;f0>;>;f1;c[0]=1;a[n]=1;fxym[0]=6*((y[1]-y[0])/(x[1]-x[0])-f0)/h[0];fxym[n]=6*(f1-(y[n]-y[n-1])/(x[n]-x[n-1]))/h[n-1];break;case 2:cout;cin>;>;f0>;>;f1;c[0]=a[n]=0;fxym[0]=2*f0;fxym[n]=2*f1;break;default:。