Lagrange Interpolation related problem (plotting a graph)

x = input('Enter values of x: ');
y = input('Enter values of y: ');
n = length(x);
L = zeros(n,n);
for i = 1:n
V= 1;
for j = 1:n
if i~= j
V = conv(V,poly(x(j)))/(x(i) - x(j));
end
end
L(i,:) = V*y(i);
end
L
P = sum(L)
Now here I want to plot the graph of a certain method, But don't know how to do it!

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 29 日

0 投票

Hi
The answer is simple:
plot(x, L(1,:)) OR plot(x, L(2,:))
plot(x, V) or plot(x, P)
Or within the loop:
...
if i~= j
V = conv(V,poly(x(j)))/(x(i) - x(j));
end
end
L(i,:) = V*y(i);
plot(x, L(i,:)), hold all
end

4 件のコメント

Maha Aftab
Maha Aftab 2021 年 5 月 29 日
Error using gobjects (line 42)
Not enough input arguments.
Error in newplot (line 47)
fig = gobjects(0);
Error in Untitled3 (line 13)
plot(x, L(i,:)), hold all
This error is shown!!
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 29 日
The number of values entered for x and y are equal or not?
Make sure that they have equal number of value entries via input().
Maha Aftab
Maha Aftab 2021 年 5 月 30 日
They are
Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 5 月 30 日
Test again:
x = input('Enter values of x: ');
y = input('Enter values of y: ');
n = length(x);
L = zeros(n,n);
for i = 1:n
V= 1;
for j = 1:n
if i~= j
V = conv(V,poly(x(j)))/(x(i) - x(j));
end
end
L(i,:) = V*y(i);
plot(x, L(i,:)), hold all
end
x = linspace(0, 5);
y = linspace(0, 5)*2+linspace(0, 5).^2;

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2021 年 5 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by