function y = LI(x,n) %lagrange interpolation of exp on [0,l] z=linspace(0,1,n+1); %z(1), ... , z(n+1) corresponds to the interpolation points %x_0, x_1, . . ., x_n from the book y = zeros(1,length(x)); for i = 1:n+1 %This loop adds the n terms of the lagrange polynomial L = ones(1,length(x)); for j = 1:n+1 %This loop multiplies th n terms of the L_i,n polynomial if (j ~= i) L = L.*(x-z(j))./(z(i) - z(j)); end end y = y + exp(z(i))*L; end