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

回答 (2 件)

John D'Errico
John D'Errico 2019 年 2 月 22 日
編集済み: John D'Errico 2019 年 2 月 22 日

1 投票

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 件のコメント

Khristopher Macedo
Khristopher Macedo 2019 年 2 月 22 日
Thank you, it worked.

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

Yukun
Yukun 2019 年 2 月 22 日

0 投票

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.

タグ

質問済み:

2019 年 2 月 22 日

コメント済み:

2019 年 2 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by