Plotting Discontinuous Function

Hi, I have a question about plotting a discontinuous function. My function m1(t) = 1 when m(t) greater or equal to 0 and -1 when m(t) is less than 0, where m(t) = cos(t). I have an idea of what the graph needs to look like changing level planes 1 and -1 every [-pi/2, pi/2] but have no idea how to implement it. Please help!

 Accepted Answer

Walter Roberson
Walter Roberson on 28 May 2012
Draw it continuous segment by continuous segment, OR put a NaN between the continuous parts. Or just let it connect the discontinuous parts.
Have you considered a stairs plot?

4 Comments

Yah I was thinking of some sort of stairs plot, like a unit step function. I have tried this code but I keep coming up with an error:
t = 0:0.05:10;
m = (1) .* (x < pi/2) + (-1) .* (pi/2 < x & x < 3*pi/2 ) + (1 ) .* (3*pi/2 < x & x < 5*pi/2) + (-1) .* (x < 5*pi/2 & x< = 7*pi/2);
I am not sure though, I'm fairly new to this.
oh there was a typo all the x should be t as its a function of m(t). But still having error. Any suggestions?
You have < = with a space between the two. You need to fix that.
You have a more subtle bug. In one of your conditions, you have _two_ checks to see if x is less than a particular value.
Okay I got it!! Thanks for the help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!