Lagrange 插值
一.功能
给出n个节点及相应的函数值x1,x2,…,xn;y1,y2,…,yn,
对m个插值节点xj,j=1,2,…,m,用Lagrange插值多项式进行成
组插值.
二.算法简介
设已知n个节点,x1,x2,…,xn及相应的函数值yj=f(xj)
j=1,2,…,n,由此构造n-1阶插值多项式Φn-1(x)
当n=3时,有
Φ2(x)=∑YjLj(x),j=1,2,3 (6-10)
其中 L1(x)=[(x-x2)(x-x3)]/[(x1-x2)(x1-x3)]
L2(x)=[(x-x1)(x-x3)]/[(x2-x1)(x2-x3)]
L3(x)=[(x-x1)(x-x2)]/[(x3-x1)(x3-x2)]
L1(x),L2(x),L3(x)分别称为x1,x2,x3的二次插值基函数,将
它们入(6-10)式,即得Lagrange二次插值多项式:
Φ2(x)=∑[∏(x-xj)/(xk-xj)]Yk,
j,k=1,2,3,j≠k (6-11)
推广,当n>3时,可构造n-1次插值基函数,得到n-1次Lagrange
插值多项式:
Φn-1(x)=∑[∏(x-xi)/(xk-xi)]
k=1,2,…,n, I=2,3,…,n (6-12)
三. 程序使用说明
1. 输入参数
n 节点个数
m 插值点个数
X(N),Y(N),一维实数组,分别存放节点及节点处的函数值
C(M) 一维实数组,存放插值点的值
数据排列顺序:x(n),y(n),c(m),分别存放在主程序250-270中
2. 输出参数
n,m分别为节点个数,插值点个数
X(N),Y(N),一维实数组,分别存放节点及节点处的函数值
C(M),D(M),一维实数组,分别存放插值点及值
五.程序例题运行结果说明:
当运行程序后,输入:
6 7
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000
8.0000 27.0000 64.0000 125.0000 216.0000 343.0000
0 1.5 2.5 3.5 4.5 5.5 7
则运行结果为:
x=0.0000000000e+00 y=1.0000000000e+00
x=1.5000000000e+00 y=1.5625000000e+01
x=2.5000000000e+00 y=4.2875000000e+01
x=3.5000000000e+00 y=9.1125000000e+01
x=4.5000000000e+00 y=1.6637500000e+02
x=5.5000000000e+00 y=2.7462500000e+02
x=7.0000000000e+00 y=5.1200000000e+02
basic源程序:
10 '***************
20 '604 Lagrange *
30 '***************
40 INPUT "N,M=";N,M
50 PRINT;TAB(3);"N=";N,"M=";M
60 DIM X(60),Y(60),C(M),D(M)
70 PRINT TAB(3);
80 FOR I=1 TO N
90 READ X(I):PRINT USING" ###.####";X(I);
100 NEXT I
110 PRINT
120 FOR I=1 TO N
130 READ Y(I):PRINT USING" ###.####";Y(I);
140 NEXT I
150 PRINT
160 FOR J=1 TO M
170 READ C(J)
180 NEXT J
190 PRINT
200 GOSUB 400
210 PRINT TAB(3);
220 FOR J=1 TO M
230 PRINT "X=";C(J),"Y=";D(J)
240 NEXT J
250 DATA 1,2,3,4,5,6
260 DATA 8,27,64,125,216,343
270 DATA 0,1.5,2.5,3.5,4.5,5.5,7
280 END
400'子程序
410 FOR J=1 TO M
420 FOR K=1 TO N
430 S=1
440 FOR I=1 TO N
450 IF I=K THEN 470
460 S=S*(C(J)-X(I))/(X(K)-X(I))
470 NEXT I
480 D(J)=D(J)+S*Y(K)
490 NEXT K
500 NEXT J
510 RETURN
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
void result(float *, float *, float *, float *, int, int);
void main()
{
int n, m, i = 0;
float *X, *Y, *C, *D;
printf("Please input node number:");
scanf("%d",&n);
printf("Please input insert-number number:");
scanf("%d",&m);
X = (float*)malloc(sizeof(float)*n);
Y = (float*)malloc(sizeof(float)*n);
C = (float*)malloc(sizeof(float)*m);
D = (float*)malloc(sizeof(float)*m);
for(i = 0; i < n; i++)
{
printf("Please input X[%d]:",i+1);
scanf("%f",X+i);
}
for(i = 0; i < n; i++)
{
printf("Please input Y[%d]:",i+1);
scanf("%f",Y+i);
}
for(i = 0; i < m; i++)
{
printf("Please input C[%d]:",i+1);
scanf("%f",C+i);
}
for(i = 0; i < m; i++)
*(D + i) = 0;
result(X, Y, C, D, n, m);
for(i = 0; i < m; i++)
{
printf("x = %0.10e y = %0.10e\n",*(C+i), *(D+i));
}
free(X);
free(Y);
free(C);
free(D);
system("pause");
}
void result(float *X, float* Y, float *C,float*D, int n, int m)
{
float temp = 0;
int i = 0, k = 0, j = 0;
for(j = 0; j < m; j++)
for(k = 0; k < n; k++)
{
temp = 1;
for(i = 0; i < n; i++)
{
if(i == k)
continue;
temp *= (C[j] - X[i])/(X[k] - X[i]);
}
D[j] += temp * Y[k];
}
}
----------------解决方案--------------------------------------------------------
十分的感谢```
不知道说什么好,这是我们一个实习题目,从来没有学过Basic,去图书馆借书现在也都是VB,很少能看见Basic的书呢,由于时间很紧,就在论坛求助
----------------解决方案--------------------------------------------------------
你是数学系的吗?
----------------解决方案--------------------------------------------------------