Graph Piecewise Functions in MATLAB?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 5K views
jean28
Messages
83
Reaction score
0
Hey guys. I need to graph a piecewise function in MATLAB and I don't know how to do it. On top of that, it is also in radians:

f(θ)
=
(80/∏2) θ, -∏/2 ≤ θ ≤ ∏/2
(80/∏) - (80/∏2) θ, ∏/2 ≤ θ ≤ 3∏/2

How do I graph it in MATLAB? And other than that, is there a way in MATLAB that I can take that function and turn it into time instead of radians? Thanks a lot.
 
Physics news on Phys.org
If you have:

y=f(x) for a<x<b and y=g(x) for b<x<c ...

There are several ways to approach this - the simplest would be to set up y1=f(x1) and y2=g(x2) then make y=[y1,y2]; x=[x1,x2]; plot(x,y).

You may have to be careful about the overlap between x1 and x2.
eg. if (for N steps in each region) x1=a:(b-a/N):b; x2=b:(c-b)/N:c; they will have point b in common... which can matter.

You can also use x=a:(c-a)/N:c; and select s1=find(x=<b); s2=find(x>b); and use s1 and s2 to index the correct parts of y.

If you know the relationship between x and t, say: t=x./v; then you can just plot(t,y) instead.