2016-11-15 54 views
1

我试图在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)内插方法是这样的:

enter image description here

更多资料,请有关拉格朗日插值的离子可用 here

回答

1

这条线:

P_x=y(i)*L(i)+P_x; 

应该在遍历我,不学家