lagrange.c

来自「拉格朗日插值的源程序」· C语言 代码 · 共 49 行

C
49
字号
/*  HELLO.C -- Hello, world */

#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "stdlib.h"
#ifndef EPSILON
#define EPSILON 0.0000000001
#endif
#define N 3
double lagrange(double x,double xi[N],double yi[N])
{
int i,j;
double y,lix;
y=0.0;
for(i=0;i<N;i++)
{lix=1.0;
for(j=0;j<N;j++)
{
if(j!=i)
{
    if(fabs(xi[i]-xi[j])<EPSILON)
    {printf("the interpolation base poits overlaping!\n");
     getch();
     printf("strike any key to exit!\n");
     getch();
     exit(1);
    }
else lix=lix*(x-xi[j])/(xi[i]-xi[j]);
}
}
y=y+yi[i]*lix;
}
return(y);

}
main()
{
double x,y;
static double xi[N]={4.00,6.25,9.00};                                            /* 一个固定的例子进行说明 */
static double yi[N]={2.00,2.50,3.00};

printf("lagrange interpolation :\n");
getch();
scanf("%lf",&x);
y=lagrange(x,xi,yi);
printf("x=%12.6f  p(x)=%15.8f\n",x,y);
getch(); }

⌨️ 快捷键说明

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