Plotting square wave in matlab

10 ビュー (過去 30 日間)
Aksh Chordia
Aksh Chordia 2021 年 1 月 7 日
編集済み: Aksh Chordia 2021 年 1 月 8 日
I dont know why but MATLAB is adding a rise time while plotting the square wave.
How to stop it from doing so?
I want perfect square wave, how can I achieve the same?
The code and png of figure is attached below.
Thanks in advance
Aksh
t = linspace(0,10e-9);
x = square(t*2*pi/(10e-9));
Y_max=x*370.0;
Y_min=x*323.4;
hold on
plot(t,Y_max,'LineWidth',3,'color',[203/255;1/255;98/255]);
plot(t,Y_min,'LineWidth',3,'color',[203/255;1/255;98/255]);
hold off

採用された回答

Cris LaPierre
Cris LaPierre 2021 年 1 月 7 日
編集済み: Cris LaPierre 2021 年 1 月 7 日
This is because your x value is different for the point on the bottom compared to the point on top. Your x axis range is so small, that you are able to see this difference (=1.0101e-10).
I guess technically, it has more to do with the ratio of to the range of x. You could try adding more data points to t to make it less apparent.
t = linspace(0,10e-9,1000);
x = square(t*2*pi/(10e-9));
Y_max=x*370.0;
Y_min=x*323.4;
hold on
plot(t,Y_max);
plot(t,Y_min);
  1 件のコメント
Cris LaPierre
Cris LaPierre 2021 年 1 月 7 日
You'll notice this same thing happens in the examples given in the doc, too.

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

その他の回答 (1 件)

William
William 2021 年 1 月 7 日
Yes, the function square(x) returns a unique value, either +1 or -1, for each vallue of x. There is no value of x for which it returns both +1 and -1, so when you plot sin(x) vs (x), there is no value of x for which the plot will produce a line that is exactly vertical. In order to produce such a graph, you need to generate a data set in which there are two identical x-values at each transition, with one of them having a corresponding y-value of -1 and the other having a y-value of +1.
  1 件のコメント
Aksh Chordia
Aksh Chordia 2021 年 1 月 8 日
編集済み: Aksh Chordia 2021 年 1 月 8 日
Got your point, thanks for that insight.

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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by