
clc,clear

%% 数据（data）
 n = 12;
x0 = -5:10/n:5;
y0 = 1./(1+x0.^2);


%% 构造Lagrange函数（Get Lagrange polynomial）
syms x

omega_vec = x*ones(size(x0)) - x0;

omega = prod(omega_vec);

omega_der = diff(omega);

Ln_vec = y0.*(omega./(x-x0))./subs(omega_der,x0);

Ln = sum(Ln_vec);


%% 画图（plot figure）
hold on;
set(gca,'FontSize',20);
plot(x0,y0,'bd','Linewidth',2);
plot(-5:0.01:5,subs(Ln,-5:0.01:5),'b','Linewidth',1.5);
plot(-5:0.01:5,1./(1+(-5:0.01:5).^2),'r','Linewidth',1.5);
legend('data','L12(x)','f(x)');legend('boxoff')
axis([-5 5 -4 2]);