Lagrange Interpolation related problem (plotting a graph)
Afficher commentaires plus anciens
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!
Réponses (1)
Sulaymon Eshkabilov
le 29 Mai 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 commentaires
Maha Aftab
le 29 Mai 2021
Sulaymon Eshkabilov
le 29 Mai 2021
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
le 30 Mai 2021
Sulaymon Eshkabilov
le 30 Mai 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;
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!