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
2021-5-29
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
2021-5-29
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
2021-5-30
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 Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!