Graph Piecewise Functions in MATLAB?

In summary, the conversation discusses how to graph a piecewise function in MATLAB that is in radians. The function has two parts, with different equations for different ranges of the input variable. One way to graph it is to set up two separate functions and plot them together. Another approach is to use one function with an index to select the correct parts of the equation. Additionally, the conversation mentions the possibility of converting the function from radians to time in MATLAB.
  • #1
jean28
85
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
  • #2
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.
 

Related to Graph Piecewise Functions in MATLAB?

1. How do I graph a piecewise function in MATLAB?

To graph a piecewise function in MATLAB, you can use the "piecewise" function. This function allows you to define different equations for different intervals, and then plot them on a single graph.

2. How can I add labels and titles to my piecewise function graph?

You can add labels and titles to your piecewise function graph using the "xlabel", "ylabel", and "title" functions in MATLAB. These functions allow you to specify the text for each label or title, as well as the font size and style.

3. Can I plot multiple piecewise functions on the same graph?

Yes, you can plot multiple piecewise functions on the same graph in MATLAB. Simply use the "hold on" function before plotting each function, and they will all appear on the same graph.

4. How do I change the color or style of my piecewise function graph?

You can change the color or style of your piecewise function graph by using the "color" and "linestyle" parameters in the "plot" function. You can also use the "plot" function multiple times with different color and style parameters to create a more customized graph.

5. Is it possible to add a legend to my piecewise function graph?

Yes, you can add a legend to your piecewise function graph by using the "legend" function in MATLAB. This function allows you to specify the labels for each function and their corresponding colors or styles.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
476
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
Back
Top