My plot does not show anything
Mostra commenti meno recenti
I'm trying to plot a function but I don't get lines in it.
delta = pi / 100;
x1 = -pi + delta : delta : 0 - delta;
x2 = 0 + delta : delta : pi - delta;
x3 = -pi : delta : pi;
hold on
plot(x1, delta, '-b', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x2, delta, '-r', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x3, delta, '-y', 'LineWidth', 2), xlabel('x'), ylabel('y')
hold off
legend('-pi + delta : delta : 0 - delta', '0 + delta : delta : pi - delta', '-pi : delta : pi')
box on
grid on
Risposte (2)
John D'Errico
il 22 Feb 2019
Modificato: John D'Errico
il 22 Feb 2019
delta is a scalar.
plot does not work well when one of the arguments is a scalar. I suppose some might call if a feature that it works partially at all. For example
This works:
plot(1:5,1,'o')
but this gives you just white.
plot(1:5,1,'-')
And this plots the symbols, but no connecting line.
plot(1:5,1,'-o')
As I said, the ability of plot to do anything at all when x and y are different sizes is a feature. It should arguably return an error in my opinion. My guess is this has been in MATLAB for so long that the capability has been left untouched, a grandfathered feature. There are probably some people who use that capability in legacy code.
Anyway, an easy fix is:
delta = pi / 100;
x1 = -pi + delta : delta : 0 - delta;
x2 = 0 + delta : delta : pi - delta;
x3 = -pi : delta : pi;
hold on
plot(x1, delta*ones(size(x1)), '-b', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x2, delta*ones(size(x2)), '-r', 'LineWidth', 2), xlabel('x'), ylabel('y')
plot(x3, delta*ones(size(x3)), '-y', 'LineWidth', 2), xlabel('x'), ylabel('y')
hold off
legend('-pi + delta : delta : 0 - delta', '0 + delta : delta : pi - delta', '-pi : delta : pi')
box on
grid on
1 Commento
Khristopher Macedo
il 22 Feb 2019
Yukun
il 22 Feb 2019
0 voti
So, firstly, can you describe your purpose of writing the code? What do you want to achieve?
And as you can see, "delta" is a scalar but x1,x2,x3 are vectors. I don't think it is proper.
Categorie
Scopri di più su Exploration and Visualization in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!