How to draw a discontinue function

Hi all, I have x(t) = cos(t) and y(t) = 0.5 if abs(x(t)) larger than 0.5, other wise y(t) = x (t). I have no idea how to plot y(t). Please help me!!!

 採用された回答

Wayne King
Wayne King 2012 年 6 月 3 日

0 投票

t = 0:0.01:2*pi;
x = cos(t);
y = x;
y(abs(x)>0.5) = 0.5;
% to see the discontinuity
stem(t(1:400),y(1:400))
If you really want to see the discontinuity most clearly, it's probably a good idea to delete the baseline in the stem plot.
h = stem(t(150:250),y(150:250),'markerfacecolor',[0 0 1]);
axis tight;
delete(get(h,'baseline'))

2 件のコメント

UCLA
UCLA 2012 年 6 月 3 日
Thank you for your answer. But I am not sure about your notation y(abs(x)>0.5) does that mean y is a function and abs(x)>0.5 is the argument? Can I say if abs(x)>0.5 y = 0.5 instead? Thanks!
Wayne King
Wayne King 2012 年 6 月 3 日
that is logical indexing, it says for all the elements where the absolute value of x is greater than 0.5, set the corresponding y element to 0.5. That is the definition you gave. I think logical indexing is the easiest way to do it.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by