Why is plot(x,y=0) not giving a line at y=0?

37 ビュー (過去 30 日間)
Shaona Bose
Shaona Bose 2019 年 4 月 4 日
編集済み: madhan ravi 2019 年 4 月 4 日
I am trying to plot a function with a line through y=0 too.
I wrote this code:
x=0:0.01:10*pi;
plot(x,2*sin(x).*exp(-x/4));
hold on
y=0;
plot(x,y)
This is only giving me a curve and no line through y=0.
I am getting the y=0 line with this code:
x=0:0.01:10*pi;
plot(x,2*sin(x).*exp(-x/4));
hold on
y=zeros(length(x));
plot(x,y)
So what is happening with the pervios code? Doesnt y=0; make any sense here?

採用された回答

madhan ravi
madhan ravi 2019 年 4 月 4 日
編集済み: madhan ravi 2019 年 4 月 4 日
Stephen’s answer is the way I would do it but the explanation for the not satisfactory result of your code:
y=0;
You create a scalar and you try to plot it with x which is vector, it doesn't work like that in MATLAB you should have done
plot(x,zeros(size(x))) % this creates zeros as same size as x.

その他の回答 (1 件)

Stephan
Stephan 2019 年 4 月 4 日
編集済み: Stephan 2019 年 4 月 4 日
Hi,
no need for this - since you are using R2018b you can take advantage of the yline function:
x=0:0.01:10*pi;
plot(x,2*sin(x).*exp(-x/4))
yline(0,'r--');
Best rregards
Stephan

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by