拉格朗日插值的一个自编程序
特点:对插值节点数没有约束。#include <stdio.h>
#include <malloc.h>
#include <conio.h>
void main()
{
//................//
//lagrange插值举例//
//................//
double *xi,*yi,x,y;//xi,yi为插值节点,x为插值点;
int n;//n为插值区间数量;
int i,j,k;
double P;
y=0.0;
printf("请输入插值区间数量(n=):\n");
scanf("%d",&n);
xi=(double*)malloc((n+1)*sizeof(double));
yi=(double*)malloc((n+1)*sizeof(double));
printf("请输入节点xi值:\n");
for(i=0;i<=n;i++)
{
printf("xi[%d]: ",i);
scanf("%lf",(xi+i));
printf("yi[%d]: ",i);
scanf("%lf",(yi+i));
printf("\n");
}
printf("请输入插值点值:\n");
printf("x=");
scanf("%lf",&x);
printf("\n");
//下面实现插值功能
for(k=0;k<=n;k++)
{
P=1.0;
for(j=0;j<=n;j++)
{
if(k!=j) P*=(x-*(xi+j))/(*(xi+k)-*(xi+j));
}
y+=P*(*(yi+k));
}
printf("插值点x上的值为(y=):%lf",y);
free(xi);
free(yi);
getche();