Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Graph Piecewise Functions in MATLAB?

  1. Dec 4, 2012 #1
    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:

    (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.
  2. jcsd
  3. Dec 4, 2012 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook