拉格朗日插值求解近似值

题目如下

在这里插入图片描述

代码

#include<stdio.h>
void print(float x[],float y[],float m)
{
	float s,a;
	int i,j;
	s=0;
	for(i=0;i<5;i++)
	{
		a=1;
		for(j=0;j<5;j++)
		if(i!=j)
			a*=(m-x[j])/(x[i]-x[j]);
		s=s+a*y[i];
	}
	printf("%.6f\n",s);
}
int main()
{
	float x[5]={0.4,0.55,0.8,0.9,1},y[5]={0.41075,0.57815,0.88811,1.02652,1.17520};
	float m1=0.5,m2=0.7,m3=0.85;
	print(x,y,m1);
	print(x,y,m2);
	print(x,y,m3);
	return 0;
 } 

运行截图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42837885/article/details/84945607
今日推荐