Lagrange Interpolation related problem (plotting a graph)

3 views (last 30 days)
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!

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 29 May 2021
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 Comments
Sulaymon Eshkabilov
Sulaymon Eshkabilov on 30 May 2021
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;

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!