Plotting the Lagrange interpolated curve on top of the equidistant points

51 Views Asked by At

I have made a simple code for Lagrange interpolation of vectors of data x and y and , but I need some help with plotting and displaying errors.

I wish to plot the equidistant points together with the Lagrange Interpolated curve in the same plot, and display the error of the approximation. Plotting the points is trivial, but I struggle with plotting the interpolated polynomial curve on top of the (x,y) plot.

clear
clc

x = -1:0.25:1;
y = 1./(1+25.*x.^2);

N = 1:length(y);
M = zeros(length(y),length(x));
for n = 1:length(x)
    for i = 1:length(y)
        l = 1;
        index = find(N ~= i);
        for jj = 1:length(index)
            l = l.*(x(n)-y(index(jj)))./(y(i)-y(index(jj)));
        end
        M(i,n) = l;
    end
end

plot(x,y)

All help is appreciated

0

There are 0 best solutions below