Lecture 7
Lecture 7
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
For example, using the graph from the previous example, add an x- and y-axis
labels. Now label the axis and add a title. The character \pi creates the symbol π.
x=0:pi/100:2*pi; sine function
1
y= sin(x);
0.8
plot(x,y) 0.6
xlabel('x=0:2\pi') 0.2
y=sin(x)
0
ylabel('y=sin(x)')
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
x=0:2
y=sin(x); 0.8
0.6
y2=sin(x-0.25);
0.4
y3=sin(x-0.5); 0.2
plot(x,y,x,y2,x,y3) 0
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
The legend command provides an easy way to identify the individual plots.
legend('sin(x)','sin(x-0.25)','sin(x-0.5)')
1
sin(x)
0.8 sin(x-0.25)
sin(x-0.5)
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
By default, MATLAB uses line style and color to distinguish the data sets
plotted in the graph. However, you can change the appearance of these graphic
components or add annotations to the graph to help explain your data for presentation.
Programming II 2nd Year Rasha Hassan
6
Lecture 7: MATLAB Graphics
Example 2: Write a MATLAB program to plot three functions of x in one plot, where
the first function is (2cos(x)), the second function is (cos(x)), and the third function is
(0.5 cos(x)), with the interval of 𝟎 ≤ 𝐱 ≤ 𝟐𝛑 in step of π/100. Use a dashed, a solid,
and a dotted line styles to plot these functions Then, add a title, x-axis label, y-axis
label, and legend to this plot. Then after, set the minimum and maximum values of the
axes.
Solution:
x = 0:pi/100:2*pi;
y1 = 2*cos(x);
y2 = cos(x);
y3 = 0.5*cos(x);
plot (x, y1, '--', x, y2, '-', x, y3, ':')
xlabel('0 \leq x \leq 2\pi')
ylabel('Cosine Functions')
legend('2*cos(x)','cos(x)','0.5*cos(x)')
title('Three Functions Plot')
axis([0 2*pi -3 3])
Three Functions Plot
3
2*cos(x)
cos(x)
2 0.5*cos(x)
1
Cosine Functions
-1
-2
-3
0 1 2 3 4 5 6
0 x 2
MATLAB does not replace the existing graph when you issue another plotting
command; it adds the new data to the current graph, rescaling the axes if necessary.
For example, these statements first create a contour plot of the peaks function, then
superimpose a pseudocolor plot of the same function.
x = 0:pi/100:2*pi;
y = 0:pi/10:2*pi;
plot(x,sin(x))
hold on
plot(y,cos(y))
hold off
The hold on command causes the sin(x) plot to be combined with the cos(y) plot
in one figure.
0.8
0.6
0.4
0.2
-0.2
-0.4
-0.6
-0.8
-1
0 1 2 3 4 5 6 7
Example 3: The position 𝑥 as a function of time of a particle that moves along a straight
line is given y:
𝒙(𝒕) = 𝟎. 𝟒𝟏 𝒕𝟒 − 𝟏𝟎. 𝟖 𝒕𝟑 + 𝟔𝟒 𝒕𝟐 − 𝟖. 𝟐 𝒕 + 𝟒. 𝟒 𝒇𝒕
The velocity 𝑣(𝑡) of the particle is determined by the derivative of 𝑥(𝑡) with respect
to 𝑡, and the acceleration 𝑎(𝑡) is determined by the derivative of 𝑣(𝑡) with respect to
𝑡.
Derive the expression for the velocity ad acceleration of the particle, and make plots of
the position, velocity, and acceleration as function of time for 0 ≤ 𝑡 ≤ 8 𝑠. Use the
hold command to make the three plots in one plot. Label the axes appropriately with
the correct units.
Solution:
𝒗(𝒕) = 𝟏. 𝟔𝟒 𝒕𝟑 − 𝟑𝟐. 𝟒𝒕𝟐 + 𝟏𝟐𝟖 𝒕 − 𝟖. 𝟐 𝒇𝒕/𝒔
𝒂(𝒕) = 𝟒. 𝟗𝟐 𝒕𝟐 − 𝟔𝟒. 𝟖 𝒕 + 𝟏𝟐𝟖 𝒇𝒕/𝒔𝟐
MATLAB Code:
t=linspace(0,8,500);
x=0.41*t.^4-10.8*t.^3+64*t.^2-8.2*t+4.4;
v=1.64*t.^3-32.4*t.^2+128*t-8.2;
a=4.92*t.^2-64.8*t+128;
plot(t,x)
hold on
xlabel('t(s)')
ylabel('x(ft)')
plot(t,v)
hold on
xlabel('t(s)')
ylabel('v(ft.s)')
plot(t,a)
hold off
xlabel('t(s)')
ylabel('a(ft.s.^2)')
500
400
300
200
a(ft.s.2)
100
-100
-200
-300
0 1 2 3 4 5 6 7 8
t(s)
Example 4: Write a program to plot two functions of x in separate plots, where the
first function is (𝒆−𝟏.𝟓𝒙 𝒔𝒊𝒏(𝟏𝟎𝒙)), and the second function is (𝒆−𝟐𝒙 𝒔𝒊𝒏(𝟏𝟎𝒙)), with
the interval of t from 0 to 5 in step of 0.01. Use a square marker, a red color, a dash-
dotted line style to the first plot, and an asterisk marker, a blue color, a dotted line style
to the second plot. Then, add a title, x-axis label, and y-axis label to each plot. Then
after, turn the grid on.
Solution:
x=0:0.01:5;
y1=exp(-1.5*x).*sin(10*x);
y2=exp(-2*x).*sin(10*x);
subplot(1,2,1)
plot(x,y1,'s r -.')
title('e(-1.5x)sin(10x)')
xlabel('x')
ylabel('y1')
grid on
subplot(1,2,2)
plot(x,y2,'* b :')
title('e(-
2x)sin(10x)')
xlabel('x')
ylabel('y2')
grid on
If you want to clear one of the plots in a subplot without affecting the others,
you can use the cla (clear axes) command. Continuing the previous example:
subplot(1,2,1)
cla
e(-1.5x)sin(10x) e(-2x)sin(10x)
1 1
0.9
0.8
0.8
0.7 0.6
0.6
0.4
y1
y2
0.5
0.2
0.4
0.3 0
0.2
-0.2
0.1
0 -0.4
0 0.5 1 0 2 4 6
x x
❖ The three vectors with the coordinates of the data points must have the same
number of elements.
❖ The line specifiers, properties, and property values are the same as in 2-D plots
(see Section 5.1).
For example, if the coordinates x, y, and z are given as a function of the
parameter t by:
𝑥 = √𝑡 sin 2𝑡
𝑦 = √𝑡 cos 2𝑡
𝑧 = 0.5 𝑡
a plot of the points for 0 ≤ 𝑡 ≤ 6𝜋 can be produced by the following script file:
t=0:0.1:6*pi;
x=sqrt(t).*sin(2*t);
y=sqrt(t).*cos(2*t);
z=0.5*t;
plot3(x,y,z,'k','linewidth',1)
grid on
xlabel('x'); ylabel('y'); zlabel('z')
The plot shown in Figure below is created when the script is executed.
10
6
z
0
5
5
0
0
y -5 -5
x
In this grid, the distance between the points is one unit. The points of the grid
can be defined by two matrices, 𝑋 and 𝑌. Matrix 𝑋 has the 𝑥 coordinates of all the
points, and matrix 𝑌 has the 𝑦 coordinates of all the points:
−1 0 1 2 3 4 4 4 4 4
−1 0 1 2 3 3 3 3 3 3
𝑋=[ ] and 𝑌 = [ ]
−1 0 1 2 3 2 2 2 2 2
−1 0 1 2 3 1 1 1 1 1
The X matrix is made of identical rows since in each row of the grid the points
have the same x coordinate. In the same way the Y matrix is made of identical columns
since in each column of the grid the y coordinate of the points is the same.
MATLAB has a built-in function, called meshgrid, that can be used for creating
the X and Y matrices. The form of the meshgrid function is:
In the vectors 𝑥 and 𝑦 the first and last elements are the respective boundaries of
the domain. The density of the grid is determined by the number of elements in the
vectors. For example, the mesh matrices 𝑋 and 𝑌 that correspond to the grid in Figure
above can be created with the meshgrid command by:
>> x=-1:3;
>> y=1:4;
>> [X,Y]=meshgrid(x,y)
OR >> [X,Y]=meshgrid(-1:3,1:4)
X=
-1 0 1 2 3
-1 0 1 2 3
-1 0 1 2 3
-1 0 1 2 3
Y=
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
Once the grid matrices exist, they can be used for calculating the value of 𝑧 at
each grid point.
B. Calculating the Value of 𝒛 at Each Point of the Grid:
The value of 𝑧 at each point is calculated by using element-by-element
calculations in the same way it is used with vectors. When the independent variables 𝑥
and 𝑦 are matrices (they must be of the same size), the calculated dependent variable
is also a matrix of the same size. The value of 𝑧 at each address is calculated from the
corresponding values of 𝑥 and 𝑦. For example, if 𝑧 is given by:
𝑥𝑦 2
𝑧= 2
𝑥 + 𝑦2
the value of 𝑧 at each point of the grid above is calculated by:
>> Z = X.*Y.^2./(X.^2 + Y.^2)
Z=
-0.5000 0 0.5000 0.4000 0.3000
-0.8000 0 0.8000 1.0000 0.9231
-0.9000 0 0.9000 1.3846 1.5000
-0.9412 0 0.9412 1.6000 1.9200
Once the three matrices have been created, they can be used to plot mesh or
surface plots.
where 𝑋 and 𝑌 are matrices with the coordinates of the grid and 𝑍 is a matrix
with the value of 𝑧 at the grid points. The mesh plot is made of lines that connect the
points. In the surface plot, areas within the mesh lines are colored.
As an example, the following script file contains a complete program that creates
𝑥𝑦 2
the grid and then makes a mesh (or surface) plot of the function 𝑧 = over the
𝑥 2 +𝑦 2
domain −1 ≤ 𝑥 ≤ 3 and 1 ≤ 𝑦 ≤ 4.
x=-1:0.1:3;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=X.*Y.^2./(X.^2+Y.^2);
mesh(X,Y,Z)
xlabel('x'); ylabel('y'); zlabel('z')
1.5
0.5
z
-0.5
-1
4
3
3
2
2 1
0
y 1 -1
x
x=-1:0.1:3;
y=1:0.1:4;
[X,Y]=meshgrid(x,y);
Z=X.*Y.^2./(X.^2+Y.^2);
surf(X,Y,Z)
xlabel('x'); ylabel('y'); zlabel('z')
1.5
0.5
z
-0.5
-1
4
3
3
2
2 1
0
y 1 -1
x
15
10
0
2
1 2
0 1
0
-1 -1
-2 -2
Example 6: Write a program to plot a 3D sinc function where x and y are in the interval
from -8 to 8 in step of 0.5.
Solution: MATLAB Code:
[x,y]=meshgrid(-8:0.5:8);
R=sqrt(x.^2+ y.^2)+eps;
z=sin(R)./R;
mesh(x , y , z )
Output:
Homework:
1. Make two separate plots of the function 𝑓(𝑡) = (𝑥 + 1)(𝑥 − 2)(2𝑥 − 0.25) −
𝑒 𝑥 , one plot for 0 ≤ 𝑥 ≤ 3 and one for −3 ≤ 𝑥 ≤ 6.
2. The position 𝑥 as a function of time of a particle that moves along a straight line
is given y:
𝒙(𝒕) = 𝟎. 𝟒𝟏 𝒕𝟒 − 𝟏𝟎. 𝟖 𝒕𝟑 + 𝟔𝟒 𝒕𝟐 − 𝟖. 𝟐 𝒕 + 𝟒. 𝟒 𝒇𝒕
The velocity 𝑣(𝑡) of the particle is determined by the derivative of 𝑥(𝑡) with respect
to 𝑡, and the acceleration 𝑎(𝑡) is determined by the derivative of 𝑣(𝑡) with respect to
𝑡.
Derive the expression for the velocity ad acceleration of the particle, and make plots of
the position, velocity, and acceleration as function of time for 0 ≤ 𝑡 ≤ 8 𝑠. Use the
subplot command to make the three plots on the same page with the plot of the
position on the top, the velocity in the middle, and the acceleration at the bottom. Label
the axes appropriately with the correct units.
3. Write a MATLAB program to plot four curves of isotherms in one plot for one
mole of an ideal gas for volume ranging from 1 to 10 𝑚3 , at temperatures of
𝑇 == 100, 200, 300, 𝑎𝑑 400 𝐾, respectively. Label the axes and display a
legend. The units for pressure are Pa. If the ideal gas law relates the pressure 𝑃,
volume 𝑉, and temperature 𝑇 of an ideal gas:
𝑃𝑉 =nRT
where 𝑛 is the number of moles and 𝑅 = 8.3145 𝐽(𝐾 𝑚𝑜𝑙).
Hint: plot of pressure versus volume at constant temperature are called isotherms.