MATLAB Graph Piecewise Functions in MATLAB?

AI Thread Summary
To graph a piecewise function in MATLAB, define the function segments separately and then combine them for plotting. For the given function f(θ), where θ is in radians, the first segment is (80/π²)θ for -π/2 ≤ θ ≤ π/2, and the second segment is (80/π) - (80/π²)θ for π/2 ≤ θ ≤ 3π/2. Use the approach of defining y1 for the first segment and y2 for the second, ensuring to manage overlapping values at the transition point. A simple method involves creating x1 and x2 for the respective ranges and then concatenating them for plotting. Additionally, if a conversion from radians to time is needed, utilize the relationship t = x/v to plot time against the function values.
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.
 

Similar threads

Replies
3
Views
2K
Replies
5
Views
3K
Replies
5
Views
2K
Replies
1
Views
4K
Replies
2
Views
2K
Replies
6
Views
2K
Replies
5
Views
2K
Back
Top