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;

 採用された回答

Walter Roberson
Walter Roberson 2020 年 5 月 19 日

1 投票

plot(pointx, pointy1(pointx), 'r', x, y1, 'c--o');

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Interpolation についてさらに検索

製品

質問済み:

2020 年 5 月 19 日

回答済み:

2020 年 5 月 19 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by