I have a homework. This is my question.
Obtain the Lagrange interpolation to find the 4th order polynomial in x that passes through the given points (x, y)
where x = [-2 -1 0 1 2], y = sin(x) – 2. Then, plot both the curve of interpolation and the data points onto the same graph.
I make an implementation for this but my code gives an error.
Error using plot Invalid data argument.
Error in problem7 (line 23) plot(pointx, pointy1, 'r', x, y1, 'c–o');
Where is my mistake?
pointx = [-2 -1 0 1 2];pointy1 =@(x) (sin(x)-2);x = linspace(-5,5);n = size(pointx, 2);L = ones(n, size(x,2));for i=1:n for j=1:n if (i~=j) L(i,:).*(x-pointx(j))/(pointx(i)-pointx(j)); end endendy = 0;for i=1:n y = y + pointy1(i)*L(i,:);endy1 = y;plot(pointx, pointy1, 'r', x, y1, 'c--o');hold on;
Best Answer