Graph Piecewise Functions in MATLAB?

Click For Summary
SUMMARY

To graph a piecewise function in MATLAB, use the defined segments of the function with appropriate ranges for θ. The function is defined as f(θ) = (80/π²)θ for -π/2 ≤ θ ≤ π/2 and f(θ) = (80/π) - (80/π²)θ for π/2 ≤ θ ≤ 3π/2. Implement the graph by creating two sets of y-values and corresponding x-values, ensuring to handle any overlap between the segments. Additionally, to convert the function from radians to time, utilize the relationship t = x/v and plot t against y.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of piecewise functions
  • Knowledge of plotting functions in MATLAB
  • Basic understanding of radians and their conversion to time
NEXT STEPS
  • Research how to implement piecewise functions in MATLAB using the 'piecewise' function.
  • Learn about MATLAB's plotting functions, specifically 'plot' and 'hold on'.
  • Explore the conversion of angular measurements to time in MATLAB.
  • Investigate MATLAB's indexing techniques for handling overlapping data points.
USEFUL FOR

MATLAB users, mathematicians, engineers, and anyone interested in graphing piecewise functions and converting angular measurements to time.

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 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K