
clc,clear

%% 数据（data）
x0 = [0.4, 0.5, 0.6];
y0 = [-0.9163, -0.6931, -0.5108];

% x0 = [0.4, 0.5, 0.6, 0.7 0.8];
% y0 = [-0.9163, -0.6931, -0.5108, -0.3567 -0.2231];

%% 构造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);

%% 计算节点近似值（compute Ln(x^*)）

fprintf('\n Ln=%s \n\n 节点0.54处的近似值为: %f\n\n',simplify(Ln),subs(Ln,0.54));


%% 画图（plot figure）
hold on;
plot(x0,y0,'r*','Linewidth',1);
plot(0.4:0.01:0.6,subs(Ln,0.4:0.01:0.6),'b','Linewidth',1);
plot(0.54,subs(Ln,0.54),'Linewidth',2)