拉格朗日插值用c语言怎么编程?各位高手帮帮忙啦

Python018

拉格朗日插值用c语言怎么编程?各位高手帮帮忙啦,第1张

#include<stdio.h>

#include<string.h>

#define N 100

typedef struct tag{

double x

double y

}POINT

void main()

{

int i,j,n

double x,temp,Ln=0

POINT pt[N]

printf("请输入你要输入点的个数,,1<=n<=%d:\n",N)

printf("n=")

scanf("%d",&n)

printf("\n")

printf("\n请输入对应的点数\n")

for(i=0i<ni++)

scanf("%lf,%lf",&pt[i].x,&pt[i].y)

printf("\n")

printf("输入插值点x的值:\n")

scanf("%lf",&x)

printf("\n")

for(i=0i<ni++)

{

for(j=0,temp=1j<nj++)

{

if(j!=i)

temp=temp*(x-pt[j].x)/(pt[i].x-pt[j].x)

}

Ln=Ln+temp*pt[i].y

}

printf("输出:\nLn(%lf)=%lf\n",x,Ln)

}

下面是我所写的拉格朗日C语言实现的程序,经运行正确

#include<stdio.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=0k<3k++)

{

t=1

for(j=0j<=2j++)

if(j!=k)

{

t=((x-a[j])/(a[k]-a[j]))*t

}

y=y+t*b[k]

}

printf("y=%f\n",y)

}