lagrange.c

来自「拉格朗日插值逼近:在离散数据基础上补插除连续函数是计算数学中最基本最常用的手段是」· C语言 代码 · 共 37 行

C
37
字号
#include<stdio.h>
#define N 1000
void main()
{
	int i,j,k,p;
	float x[N],l[N],f[N],m[N];
	float y,L;
	printf("Please put in the number of the sum of the crunodes:");

	scanf("%d",&k);

	printf("Please put in the values of the crunodes and the function values in order:\n");

	for(i=0;i<k;i++)
		scanf("%f%f",&x[i],&f[i]);

	printf("Please put in the value that you want to get:");
	scanf("%f",&y);

	for(i=0;i<k;i++)
	{
		for(j=0,l[i]=1;j<k;j++)
		{
			if(j==i)
				m[j]=1;
			else
				m[j]=(y-x[j])/(x[i]-x[j]);

			l[i]=l[i]*m[j];
		}
	}
	for(i=0,L=0;i<k;i++)
		L=L+l[i]*f[i];

	printf("The function value you want to get is:%f\n",L);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?