plotting tan(x) - basic student's question

85 ビュー (過去 30 日間)
Will
Will 2014 年 2 月 27 日
回答済み: pulkit verma 2021 年 8 月 28 日
I am new to matlab and have been trying to plot basic things.
I have sucessfully managed to plot sin(x) by doing the following:
x = -2*pi:pi/100:2*pi; y = sin(x); plot(x,y)
However, when I replace the sin(x) by tan(x), the plot does not come out right. I can't figure out why this is. On the other hand I have managed to plot tan(x) using ezplot...

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 27 日
編集済み: Mischa Kim 2014 年 2 月 27 日
Will, tan(x) has singularities (function goes to infinity) at pi/2 +/- n*pi. So when you plot over the range from -2*pi to +2*pi the data points just before and right after such a singularity will also be connected (by the default line), unless you tell MATLAB to ploint data points only (markers, essentially). For example,
plot(x,y,'*')
Plus, you would also want to put a limit on your y-axis, something like
ylim([-10 10])

その他の回答 (3 件)

Wayne King
Wayne King 2014 年 2 月 27 日
編集済み: Wayne King 2014 年 2 月 27 日
tan(x) is periodic with period pi and "blows up" at odd multiples of pi/2 so why not just plot one period?
dx = 0.01;
x = -pi/2+dx:pi/100:pi/2-dx;
y = tan(x);
plot(x,y)
by increasing dx, you'll get a better visual because the value near +/- pi/2 grows in magnitude so quickly, the plot becomes dominated by the large negative and positive values,
dx = 0.1;
x = -pi/2+dx:pi/100:pi/2-dx;
y = tan(x);
plot(x,y)

hai yang
hai yang 2018 年 6 月 10 日
ezplot('tan(x)')
does the work very well
  1 件のコメント
Adil Rashid
Adil Rashid 2020 年 3 月 26 日
Greatest answer

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


pulkit verma
pulkit verma 2021 年 8 月 28 日
ezplot("tan(x)")

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by