Professional Documents
Culture Documents
Matlab For Beginner (Chap 4)
Matlab For Beginner (Chap 4)
Tan Do
Vietnamese-German University
Lecture 4
In this lecture
Plotting terminology
plot function
1 Label each axis with an appropriate name together with its unit.
2 tick-marks should be easy to read (e.g., 1, 0.1 or 0.2 rather than 0.13, 1.35).
3 When plotting more than one curve or data set, use dierent line types, data
symbols and legend labels to distinguish them.
5 Use data symbols for discrete data sets. Do not use data symbols for smooth
curves.
fplot functions
A very powerful method for plotting function is to use fplot:
or
Code 2
x = 1:0.01:2;
f = cos(tan(x)) - tan(sin(x));
Tan Do (VGU)
plot(x,f) Introduction to MATLAB Lecture 4 7 / 25
2D plotting
Subplots
We put plots next to each other to compare data. subplot function is
used to do so:
subplot(m,n,p)
This generates m × n panels with m rows and n columns, enumerated rst
from left to right and then top-down.
1 2 3
4 5 6
g = sin(10*x);
subplot(1,2,2)
plot(x,g), xlabel(’x’), ylabel(’g’), title(’Plot of g(x) = sin(10x)’)
Output
Saving gures
You can also save a gure with other extensions too (e.g., .png or .jpg,
etc.) for other purposes.
The saved gure will then be available in the Current Folder window. To
open the gure , double-click on it.
Examples
The position p (in meters) of a projectile is observed at dierent times t (in seconds).
h i
t= 0 1 2 3 4 5 6 7 8
and h i
p = 100 120.41 131.64 133.69 126.56 110.25 84.76 50.09 6.24 .
To connect data points with line segments, you have to plot the data twice.
plot(t,p,t,p,’ro’)
This produces
x = 0:0.01:2*pi;
y = sin(x);
z = cos(x);
plot(x,y,’r’,x,z,’b--’), xlabel(’x’), ylabel(’y’),
legend(’y = sin(x)’,’y = cos(x)’)
Tan Do (VGU) Introduction to MATLAB Lecture 4 15 / 25
2D plotting
You can also put labels next to the curves to distinguish them.
hold on command
Back to the example on page 15, the following code produces the same
graph.
plot(x,y,’r’)
hold on
plot(x,z,’b-’)
3D line plots
t = 0:pi/50:10*pi;
plot3(exp(-0.05*t).*sin(t), exp(-0.05*t).*cos(t), t)
xlabel(’x’), ylabel(’y’), zlabel(’z’)
grid
Note that the grid and label functions work with plot3.
zlabel is used to label the z-axis.
We can also add title, specify line type and color to enhance the plot:
t = 0:pi/50:10*pi;
plot3(exp(-0.05*t).*sin(t), exp(-0.05*t).*cos(t), t, ’ro’)
xlabel(’x’), ylabel(’y’), zlabel(’z’)
grid
title(’A spiral curve in 3D’)
which gives the coordinates of a rectangle grid with one corner at (xmin, ymin) and
the opposite corner at (xmax, ymax).
Each rectangular panel in the grid will have width xspacing and depth yspacing.
Example
>> x = 1:4;
>> y = 2:2:6;
>> [X, Y] = meshgrid(x,y)
X =
1 2 3 4 (4, 6)
1 2 3 4
1 2 3 4
Y =
(1, 4)
2 2 2 2
4 4 4 4
6 6 6 6
−2 ≤ x ≤ 2 and −2 ≤ y ≤ 2.
The code is
x = -2:0.1:2;
y = -2:0.1:2;
[X,Y] = meshgrid(x,y);
Z = X.*exp(-(X-Y.^2).^2 - Y.^2);
mesh(X,Y,Z)
xlabel(’x’), ylabel(’y’), zlabel(’z’)
which gives
Note that in the code above, x and y refers to the same vector.
In this case, we could have made the code more concise using:
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-(X-Y.^2).^2 - Y.^2);
mesh(X,Y,Z)
xlabel(’x’), ylabel(’y’), zlabel(’z’)
Remember that the spacing should not be too small for x and y values because:
it will make the surface dicult to visualize, and
the vectors X and Y will be too large.