50% found this document useful (2 votes)
235 views

2-D and 3-D Plots - MATLAB & Simulink PDF

This document discusses various plotting functions in MATLAB. It describes how to create 2D line plots using the plot function and customize them by adding titles, labels, and different line styles. It also explains how to create 3D surface and mesh plots by first generating a grid of x-y points using meshgrid and then plotting the corresponding z-values. Finally, it shows how to display multiple plots together using the subplot function to arrange them in a grid layout within the same figure window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
235 views

2-D and 3-D Plots - MATLAB & Simulink PDF

This document discusses various plotting functions in MATLAB. It describes how to create 2D line plots using the plot function and customize them by adding titles, labels, and different line styles. It also explains how to create 3D surface and mesh plots by first generating a grid of x-y points using meshgrid and then plotting the corresponding z-values. Finally, it shows how to display multiple plots together using the subplot function to arrange them in a grid layout within the same figure window.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

14.11.

2016 2-D and 3-D Plots - MATLAB & Simulink

2-D and 3-D Plots

To create two-dimensional line plots, use the plot function. For example,
plot the value of the sine function from 0 to : Open Script

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

You can label the axes and add a title.

xlabel('x')
ylabel('sin(x)')
title('Plot of the Sine Function')

https://www.mathworks.com/help/matlab/learn_matlab/plots.html 1/5
14.11.2016 2-D and 3-D Plots - MATLAB & Simulink

By adding a third input argument to the plot function, you can plot the same variables using a red dashed line.

plot(x,y,'r--')

The 'r--' string is a . Each specification can include characters for the line color, style, and marker.
A marker is a symbol that appears at each plotted data point, such as a +, o, or *. For example, 'g:*' requests a
dotted green line with * markers.

Notice that the titles and labels that you defined for the first plot are no longer in the current window. By default,
MATLAB® clears the figure each time you call a plotting function, resetting the axes and other elements to prepare
the new plot.
https://www.mathworks.com/help/matlab/learn_matlab/plots.html 2/5
14.11.2016 2-D and 3-D Plots - MATLAB & Simulink

To add plots to an existing figure, use hold.

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

hold on

y2 = cos(x);
plot(x,y2,':')
legend('sin','cos')

Until you use hold off or close the window, all plots appear in the current figure window.

Three-dimensional plots typically display a surface defined by a function


in two variables, . Open Script

To evaluate , first create a set of ( ) points over the domain of the


function using meshgrid.

[X,Y] = meshgrid(-2:.2:2);
Z = X .* exp(-X.^2 - Y.^2);

Then, create a surface plot.

surf(X,Y,Z)

https://www.mathworks.com/help/matlab/learn_matlab/plots.html 3/5
14.11.2016 2-D and 3-D Plots - MATLAB & Simulink

Both the surf function and its companion mesh display surfaces in three dimensions. surf displays both the
connecting lines and the faces of the surface in color. mesh produces wireframe surfaces that color only the lines
connecting the defining points.

You can display multiple plots in different subregions of the same window
using the subplot function. Open Script

The first two inputs to subplot indicate the number of plots in each row
and column. The third input specifies which plot is active. For example, create four plots in a 2-by-2 grid within a figure
window.

t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X); title('X');
subplot(2,2,2); mesh(Y); title('Y');
subplot(2,2,3); mesh(Z); title('Z');
subplot(2,2,4); mesh(X,Y,Z); title('X,Y,Z');

https://www.mathworks.com/help/matlab/learn_matlab/plots.html 4/5
14.11.2016 2-D and 3-D Plots - MATLAB & Simulink

Programming and Scripts

https://www.mathworks.com/help/matlab/learn_matlab/plots.html 5/5

You might also like