本文主要是介绍拉格朗日c语言实验报告,拉格朗日(Lagrange)插值法(C语言),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
拉格朗日插值法流程图
(1)二次插值:
#include
float lagelangri(float x[],float y[],float xx,int n)
{
int i,j;
float *a,yy=0;
a=new float[n];
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];
}
delete a;
return yy;
}
void main()
{
float x[5]={-3.0,-1.0,1.0,2.0,3.0};
float y[5]={1.0,1.5,2.0,2.0,1.0};
float xx1=-2,xx2=0,xx3=2.75,yy1,yy2,yy3;
yy1=lagelangri(x,y,xx1,3);
yy2=lagelangri(x,y,xx2,3);
yy3=lagelangri(x,y,xx3,3);
printf("x1=%-20f,y1=%f\n",xx1,yy1);
printf("x2=%-20f,y2=%f\n",xx2,yy2);
printf("x3=%-20f,y3=%f\n",xx3,yy3);
}
(2)五次插值:
#include
float lagelangri(float x[],float y[],float xx,int n)
{
int i,j;
float *a,yy=0;
a=new float[n];
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];
}
delete a;
return yy;
}
void main()
{
float x[6]={0.30,0.42,0.50,0.58,0.66,0.72};
float y[6]={1.04403,1.08462,1.11803,1.15603,1.19817,1.23223};
float xx1=0.46,xx2=0.55,xx3=0.60,yy1,yy2,yy3;
yy1=lagelangri(x,y,xx1,6);
yy2=lagelangri(x,y,xx2,6);
yy3=lagelangri(x,y,xx3,6);
printf("x1=%-20f,y1=%f\n",xx1,yy1);
printf("x2=%-20f,y2=%f\n",xx2,yy2);
printf("x3=%-20f,y3=%f\n",xx3,yy3);
}
这篇关于拉格朗日c语言实验报告,拉格朗日(Lagrange)插值法(C语言)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!