Any other ways to program tan(x) between [-3*pi/2 to 3*pi/2])?

Hello!
I write down the code for the tan(x) between [-3*pi/2 to 3*pi/2]), but I want to reduce it, or doing it in another way. Any ideas?
x1=-(3*pi/2)+0.01:0.01:-(pi/2)-0.01;
plot(x1,tan(x1),'r','linewidth',2.5)
hold on
x2=-(pi/2)+0.01:0.01:(pi/2)-0.01;
plot(x2,tan(x2),'r','linewidth',2.5)
x3=(pi/2)+0.01:0.01:(3*pi/2)-0.01;
plot(x3,tan(x3),'r','linewidth',2.5)
hold off

 採用された回答

Image Analyst
Image Analyst 2013 年 7 月 13 日

1 投票

x = -3*pi/2 + .1 : 0.1 : 3*pi/2 - 0.1;
y = tan(x);
plot(x, tan(x), 'r','linewidth',2);
grid on;

5 件のコメント

Mohammed Sayan
Mohammed Sayan 2013 年 7 月 13 日
Thanks but I know that! The problem is tan(x) doesn't converge to [-3*pi/2 -pi/2 pi/2 3*pi/2]...That was my reason to program it like that!
Image Analyst
Image Analyst 2013 年 7 月 13 日
編集済み: Image Analyst 2013 年 7 月 14 日
It works. I don't know what you want to do. Do you just want to display it but not have the axes go from -inf to +inf? If so, just use ylim() to set it to whatever you want. MATLAB does handle infinity so you just need to define exactly what you want to plot.
x = -3*pi/2 : 0.1 : 3*pi/2;
y = tan(x);
plot(x, y, 'r','linewidth',2);
ylim([-10, 10]);
grid on;
Mohammed Sayan
Mohammed Sayan 2013 年 7 月 14 日
Now it is correct, What about if I want to remove the vertical lines??
Image Analyst
Image Analyst 2013 年 7 月 14 日
編集済み: Image Analyst 2013 年 7 月 14 日
You can cover them up with black lines using the line() function, or you can set values more than 10 or 1000 or whatever to nan:
x = -3*pi/2 : 0.01 : 3*pi/2;
y = tan(x);
maxYtoPlot = 10;
minYtoPlot = -10;
y(y > maxYtoPlot) = nan;
y(y < minYtoPlot) = nan;
plot(x, y, 'r','linewidth',2);
ylim([minYtoPlot, maxYtoPlot])
grid on;
Mohammed Sayan
Mohammed Sayan 2013 年 7 月 14 日
Good JOB!

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 13 日

1 投票

x=-3*pi/2+0.01:0.01:-(pi/2)-0.01
y=tan(x)
y=[y nan y nan y]
x=linspace(x(1),pi/2-0.01,numel(y))
plot(x,y)

3 件のコメント

Mohammed Sayan
Mohammed Sayan 2013 年 7 月 13 日
I am sorry to say it is not correct because the tan(x) must pass through [-pi 0 and pi] which yours doesn't!!!
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 7 月 13 日
Look at x, you will see that x takes values near 0, if you want to be more closer to 0, you have to choose a very small sample time
Mohammed Sayan
Mohammed Sayan 2013 年 7 月 14 日
Thanks, I need to try it to see!

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

カテゴリ

ヘルプ センター および File ExchangePerformance and Memory についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by