How to solve plot Invalid data argument problem? MATLAB
Show older comments
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
end
end
y = 0;
for i=1:n
y = y + pointy1(i)*L(i,:);
end
y1 = y;
plot(pointx, pointy1, 'r', x, y1, 'c--o');
hold on;
Accepted Answer
More Answers (0)
Categories
Find more on Interpolation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!