十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
void
成都创新互联主营金城江网站建设的网络公司,主营网站建设方案,成都APP应用开发,金城江h5微信平台小程序开发搭建,金城江网站营销推广欢迎金城江等地区企业咨询
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
的值。
#include
stdio.h
#include
math.h
int
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
2)
{
/*
no
possible
interpolation
*/
*iflag
=
1;
goto
LeaveSpline;
}
ascend
=
1;
for
(i
=
1;
i
n;
++i)
if
(x[i]
=
x[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
nm1;
++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[n-3]
/
(x[n-2]
-
x[n-4]);
c[0]
=
c[0]
*
d[0]
*
d[0]
/
(x[3]
-
x[0]);
c[nm1]
=
-c[nm1]
*
d[n-2]
*
d[n-2]
/
(x[nm1]
-
x[n-4]);
}
/*
Alternative
end
conditions
--
known
slopes
*/
if
(end1
==
1)
{
b[0]
=
2.0
*
(x[1]
-
x[0]);
c[0]
=
(y[1]
-
y[0])
/
(x[1]
-
x[0])
-
slope1;
}
if
(end2
==
1)
{
b[nm1]
=
2.0
*
(x[nm1]
-
x[n-2]);
c[nm1]
=
slope2
-
(y[nm1]
-
y[n-2])
/
(x[nm1]
-
x[n-2]);
}
/*
Forward
elimination
*/
for
(i
=
1;
i
n;
++i)
{
t
=
d[i-1]
/
b[i-1];
b[i]
=
b[i]
-
t
*
d[i-1];
c[i]
=
c[i]
-
t
*
c[i-1];
}
/*
Back
substitution
*/
c[nm1]
=
c[nm1]
/
b[nm1];
for
(ib
=
0;
ib
nm1;
++ib)
{
i
=
n
-
ib
-
2;
c[i]
=
(c[i]
-
d[i]
*
c[i+1])
/
b[i];
}
b[nm1]
=
(y[nm1]
-
y[n-2])
/
d[n-2]
+
d[n-2]
*
(c[n-2]
+
2.0
*
c[nm1]);
for
(i
=
0;
i
nm1;
++i)
{
b[i]
=
(y[i+1]
-
y[i])
/
d[i]
-
d[i]
*
(c[i+1]
+
2.0
*
c[i]);
d[i]
=
(c[i+1]
-
c[i])
/
d[i];
c[i]
=
3.0
*
c[i];
}
c[nm1]
=
3.0
*
c[nm1];
d[nm1]
=
d[n-2];
}
else
{
b[0]
=
(y[1]
-
y[0])
/
(x[1]
-
x[0]);
c[0]
=
0.0;
d[0]
=
0.0;
b[1]
=
b[0];
c[1]
=
0.0;
d[1]
=
0.0;
}
LeaveSpline:
return
0;
}
double
seval
(int
n,
double
u,
double
x[],
double
y[],
double
b[],
double
c[],
double
d[],
int
*last)
{
int
i,
j,
k;
double
w;
i
=
*last;
if
(i
=
n-1)
i
=
0;
if
(i
0)
i
=
0;
if
((x[i]
u)
||
(x[i+1]
u))
{
i
=
0;
j
=
n;
do
{
k
=
(i
+
j)
/
2;
if
(u
x[k])
j
=
k;
if
(u
=
x[k])
i
=
k;
}
while
(j
i+1);
}
*last
=
i;
w
=
u
-
x[i];
w
=
y[i]
+
w
*
(b[i]
+
w
*
(c[i]
+
w
*
d[i]));
return
(w);
}
void
SPL(int
n,
double
*x,
double
*y,
int
ni,
double
*xi,
double
*yi)
{
double
*b,
*c,
*d;
int
iflag,last,i;
b
=
(double
*)
malloc(sizeof(double)
*
n);
c
=
(double
*)malloc(sizeof(double)
*
n);
d
=
(double
*)malloc(sizeof(double)
*
n);
if
(!d)
{
printf("no
enough
memory
for
b,c,d\n");}
else
{
spline
(n,0,0,0,0,x,y,b,c,d,iflag);
if
(iflag==0)
printf("I
got
coef
b,c,d
now\n");
else
printf("x
not
in
order
or
other
error\n");
for
(i=0;ini;i++)
yi[i]
=
seval(ni,xi[i],x,y,b,c,d,last);
free(b);free(c);free(d);
};
}
main(){
double
x[6]={0.,1.,2.,3.,4.,5};
double
y[6]={0.,0.5,2.0,1.6,0.5,0.0};
double
u[8]={0.5,1,1.5,2,2.5,3,3.5,4};
double
s[8];
int
i;
SPL(6,
x,y,
8,
u,
s);
for
(i=0;i8;i++)
printf("%lf
%lf
\n",u[i],s[i]);
return
0;
}
牛顿插值法:
#includestdio.h
#includealloc.h
float Language(float *x,float *y,float xx,int n)
{
int i,j;
float *a,yy=0.0;
a=(float *)malloc(n*sizeof(float));
for(i=0;i=n-1;i++)
{
a[i]=y[i];
for(j=0;j=n-1;j++)
if(j!=i)a[i]*=(xx-x[j])/(x[i]-x[j]);
yy+=a[i];
}
free(a);
return yy;
}
void main()
{
float x[4]={0.56160,0.5628,0.56401,0.56521};
float y[4]={0.82741,0.82659,0.82577,0.82495};
float xx=0.5635,yy;
float Language(float *,float *,float,int);
yy=Language(x,y,xx,4);
printf("x=%f,y=%f\n",xx,yy);
getchar();
}
2.牛顿插值法#includestdio.h
#includemath.h
#define N 4
void Difference(float *x,float *y,int n)
{
float *f;
int k,i;
f=(float *)malloc(n*sizeof(float));
for(k=1;k=n;k++)
{
f[0]=y[k];
for(i=0;ik;i++)
f[i+1]=(f[i]-y[i])/(x[k]-x[i]);
y[k]=f[k];
}
return;
}
main()
{
int i;
float varx=0.895,b;
float x[N+1]={0.4,0.55,0.65,0.8,0.9};
float y[N+1]={0.41075,0.57815,0.69675,0.88811,1.02652};
Difference(x,(float *)y,N);
b=y[N];
for(i=N-1;i=0;i--)b=b*(varx-x[i])+y[i];
printf("Nn(%f)=%f",varx,b);
getchar();
}
留下个邮箱,我发给你:牛顿插值法的程序设计与应用
#include iostream.h
#include math.h
void main()
{
char L;
do
{
double M[100][100];
double x[100],y[100];
double X=1,xx=0,w=1,N=0,P,R=1;
int n;
cout"请输入所求均差阶数:";
cinn;
for(int i=0;i=n;i++)
{
cout"请输入x"i"的值:"endl;
cinx[i];
cout"请输入y"i"的值:"endl;
ciny[i];
M[i][0]=x[i];
M[i][1]=y[i];
}
for( int j=2;j=n+1;j++)
{
for( i=1;i=n;i++)
{
M[i][j]=(M[i][j-1]-M[i-1][j-1])/(M[i][0]-M[i-j+1][0]);
}
}
for(i=1;i=n;i++)
{
cout"其"i"阶均差为:"M[i][i+1]endl;
}
cout"请输入x的值:x=";
cinxx;
for(i=0;in;i++)
{
X*=xx-x[i];
N+=M[i+1][i+2]*X;
P=M[0][1]+N;
}
cout"其函数值:y="Pendl;
for(i=0;in;i++)
{
w*=xx-x[i];
R=fabs(M[n][n+1]*w);
}
cout"其截断误差:R="Rendl;
coutendl"还想算其它插值吗?是请按'y'否则按'n'"endl;
cinL;
}while(L=='y');
}
下面是我所写的拉格朗日C语言实现的程序,经运行正确
#includestdio.h
int main()
{
float x,y;
float a[3]={100,121,144};
float b[3]={10,11,12};
int i,j,k=0;
float t;
y=0;
printf("请输入x的值:");
scanf("%f",x);
for(k=0;k3;k++)
{
t=1;
for(j=0;j=2;j++)
if(j!=k)
{
t=((x-a[j])/(a[k]-a[j]))*t;
}
y=y+t*b[k];
}
printf("y=%f\n",y);
}
#includestdio.h
#includestdlib.h
#includeiostream.h
typedef struct data
{
float x;
float y;
}Data;//变量x和函数值y的结构
Data d[20];//最多二十组数据
float f(int s,int t)//牛顿插值法,用以返回插商
{
if(t==s+1)
return (d[t].y-d[s].y)/(d[t].x-d[s].x);
else
return (f(s+1,t)-f(s,t-1))/(d[t].x-d[s].x);
}
float Newton(float x,int count)
{
int n;
while(1)
{
cout"请输入n值(即n次插值):";//获得插值次数
cinn;
if(n=count-1)// 插值次数不得大于count-1次
break;
else
system("cls");
}
//初始化t,y,yt。
float t=1.0;
float y=d[0].y;
float yt=0.0;
//计算y值
for(int j=1;j=n;j++)
{
t=(x-d[j-1].x)*t;
yt=f(0,j)*t;
//coutf(0,j)endl;
y=y+yt;
}
return y;
}
float lagrange(float x,int count)
{
float y=0.0;
for(int k=0;kcount;k++)//这儿默认为count-1次插值
{
float p=1.0;//初始化p
for(int j=0;jcount;j++)
{//计算p的值
if(k==j)continue;//判断是否为同一个数
p=p*(x-d[j].x)/(d[k].x-d[j].x);
}
y=y+p*d[k].y;//求和
}
return y;//返回y的值
}
void main()
{
float x,y;
int count;
while(1)
{
cout"请输入x[i],y[i]的组数,不得超过20组:";//要求用户输入数据组数
cincount;
if(count=20)
break;//检查输入的是否合法
system("cls");
}
//获得各组数据
for(int i=0;icount;i++)
{
cout"请输入第"i+1"组x的值:";
cind[i].x;
cout"请输入第"i+1"组y的值:";
cind[i].y;
system("cls");
}
cout"请输入x的值:";//获得变量x的值
cinx;
while(1)
{
int choice=3;
cout"请您选择使用哪种插值法计算:"endl;
cout" (0):退出"endl;
cout" (1):Lagrange"endl;
cout" (2):Newton"endl;
cout"输入你的选择:";
cinchoice;//取得用户的选择项
if(choice==2)
{
cout"你选择了牛顿插值计算方法,其结果为:";
y=Newton(x,count);break;//调用相应的处理函数
}
if(choice==1)
{
cout"你选择了拉格朗日插值计算方法,其结果为:";
y=lagrange(x,count);break;//调用相应的处理函数
}
if(choice==0)
break;
system("cls");
cout"输入错误!!!!"endl;
}
coutx" , "yendl;//输出最终结果
}
#include stdio.h
double Lerp(double x0,double y0,double x1,double y1,double x)
{
double dy = y1 - y0;
if(dy == 0){
printf("除0错误!\n");
return 0;
}
return x * (x1 - x0) / dy;
}
int main()
{
double x0,x1,y1,y0,x,y;
printf("Inptu x0 y0 x1 y1 x:");
scanf("%lf %lf %lf %lf %lf",x0,y0,x1,y1,x);
y = Lerp(x0,y0,x1,y1,x);
printf("y = %lf\n",y);
return 0;
}