拉格朗日插值编码故障结果
Lagrange's interpolation coding fault result
我尝试在 MATLAB 中为 y=x^2+xe^(x)
计算拉格朗日插值法。我写了下面的代码:
clc
clear
close all
x0=4.7;
n=10;
x=linspace(0,5,n);
y=x.^2+x.*exp(x);
syms t
L=sym(ones(1,n));
P_x=sym(0);
for i=1:n
for j=1:n
L_improcess=(t-x(j))/(x(i)-x(j));
if(i==j)
continue
end
L(i)=L(i)*L_improcess;
P_x=y(i)*L(i)+P_x;
end
end
P=double(subs(P_x,t,x0));
disp(['Lagrange interpolation: P= ',num2str(P)])
disp(['the real value from original function is:' num2str(x0^2+x0*exp(x0))])
所以 x0=4.7
的结果是:
Lagrange interpolation: P= 20195.8626
the real value from original function is:538.8417
我想知道两个结果之间的区别(两者必须几乎相同)
f(x)
的拉格朗日插值法是这样的:
提供了有关拉格朗日插值的更多信息
here .
这一行:
P_x=y(i)*L(i)+P_x;
应该在 i 而不是 j 的循环中。
我尝试在 MATLAB 中为 y=x^2+xe^(x)
计算拉格朗日插值法。我写了下面的代码:
clc
clear
close all
x0=4.7;
n=10;
x=linspace(0,5,n);
y=x.^2+x.*exp(x);
syms t
L=sym(ones(1,n));
P_x=sym(0);
for i=1:n
for j=1:n
L_improcess=(t-x(j))/(x(i)-x(j));
if(i==j)
continue
end
L(i)=L(i)*L_improcess;
P_x=y(i)*L(i)+P_x;
end
end
P=double(subs(P_x,t,x0));
disp(['Lagrange interpolation: P= ',num2str(P)])
disp(['the real value from original function is:' num2str(x0^2+x0*exp(x0))])
所以 x0=4.7
的结果是:
Lagrange interpolation: P= 20195.8626
the real value from original function is:538.8417
我想知道两个结果之间的区别(两者必须几乎相同)
f(x)
的拉格朗日插值法是这样的:
提供了有关拉格朗日插值的更多信息 here .
这一行:
P_x=y(i)*L(i)+P_x;
应该在 i 而不是 j 的循环中。