How to Plot Piecewise Functions in MATLAB?

In summary, to plot a function on MATLAB, you must first define the function using the "function" keyword and use the "plot" function to specify the range of values for the input variable. You can plot multiple functions on the same graph by using the "hold on" command between each "plot" function. To change the color or style of your plot, you can specify additional arguments in the "plot" function. To add a legend to your plot, use the "legend" function and specify the labels for each of your plots. You can also save your plot as an image file by using the "saveas" function and specifying the file format and desired file name.
  • #1
sofy
1
0
Hello!

I want to plot this function on MATLAB:

f(t)=1 si 0<=t<=1
f(t)=0 otherwise

Please help me! How to do it?
I tried so much things it doesn't work!
 
Physics news on Phys.org
  • #2
sofy said:
Hello!

I want to plot this function on MATLAB:

f(t)=1 si 0<=t<=1
f(t)=0 otherwise

Please help me! How to do it?
I tried so much things it doesn't work!

Look up the Heaviside function in the Matlab documentation.
 

Related to How to Plot Piecewise Functions in MATLAB?

1. How do I plot a function on MATLAB?

To plot a function on MATLAB, first define the function using the "function" keyword and specifying the input and output variables. Then use the "plot" function and specify the range of values for the input variable. Finally, use the "xlabel", "ylabel", and "title" functions to label the axes and give the plot a title.

2. Can I plot multiple functions on the same graph?

Yes, you can plot multiple functions on the same graph by using the "hold on" command between each "plot" function. This will prevent the previous plot from being cleared and allow you to add more plots to the same graph.

3. How do I change the color or style of my plot?

To change the color or style of your plot, you can specify additional arguments in the "plot" function. For example, you can use the "Color" argument to specify a specific color and the "LineStyle" argument to specify a specific line style.

4. How do I add a legend to my plot?

To add a legend to your plot, use the "legend" function and specify the labels for each of your plots. You can also use the "Location" argument to specify where you want the legend to appear on the plot.

5. Can I save my plot as an image file?

Yes, you can save your plot as an image file by using the "saveas" function and specifying the file format (e.g. .png, .jpg) and the desired file name. This will save the plot as an image in your current working directory.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
92
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
41
Back
Top