1st - Yr - Lecture 08 - NEW
1st - Yr - Lecture 08 - NEW
Lecture 8:
Saving your results / Graphical output
Last Lecture
– Relational operators
– Logical operators
– If-else-elseif constructs
– for loops
– while loops
Lecture Outline
• Today, we consider three topics:
– Using MATLAB Data files
– Creating 2-D Graphs of your data
– Creating 3-D Graphs of your data
>>save
>>load
x = linspace(0,2*pi,30);
y = sin(x);
plot(x,y), title('figure 6.1: Sine Wave')
x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x)
plot(x,y,x,z), title('figure 6.2: Sine and cos
Wave')
x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x)
plot(y,x,z,x), title('figure 6.2: Sine and cos Wave')
For example:
plot(x,y,‘r+:',x,z,‘k*-.')
Linestyle symbol
Solid line -
Dotted line :
Dash-dot line -.
Dashed line --
2D Graphics – The plot function
• It is possible to add grid lines to a plot at the tick
marks by using the command ‘grid on’. Further,
the 2-D axes are usually enclosed by a box, this
box can be removed with the command ‘box off’,
for example:
x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x);
plot(x,y,'r+:',x,z,'k*-.')
grid on;
box off;
Multiple Plots
• What happens if you type the following?
x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x);
plot(x,y,'r+:')
plot(x,z,'k*-.')
x = linspace(0,2*pi,30);
y = sin(x);
z = cos(x);
hold on;
plot(x,y,'r+:')
plot(x,z,'k*-.')
To return to MATLABS default,
we may type hold off.
2D Graphics – The plot function
This has only been an introduction to creating 2D
plots. It is all that is needed for this subject,
however MATLAB help provides excellent detailed
examples:
3D Plots – line plots
• The three dimensional equivalent to
the 2D ‘plot’ command is 'plot3'.
• The general format of the function
call is:
plot3(x1,y1,z1,S1,x2,y2,z2,S2,…)
t = linspace(0,10*pi,200);
plot3(sin(t),cos(t),t)
title('Figure 6.3: Helix')
xlabel('sin(t)')
ylabel('cos(t)')
zlabel('t')
grid on
hold on
plot3(sin(t),cos(t),t,'g-')
plot3(sin(t),cos(t),t,'s')
plot3(sin(pi*t./2),cos(pi*t./2),t,'mo')
plot3(sin(pi*t./2),cos(pi*t./2),t,'b-')
title('Figure 6.3: Helix')
xlabel('x')
ylabel('y')
zlabel('t')
3D Plots – mesh plots
• It is also possible to create ‘mesh plots’,
such as the one shown below. These are
plots of three-dimensional function
surfaces.
x=
-3 -2 -1 0 1 2 3
y=
-3 -2 -1 0 1 2 3
>>
X= Z=
-3 -2 -1 0 1 2 3 0 -5 -8 -9 -8 -5 0
-3 -2 -1 0 1 2 3 5 0 -3 -4 -3 0 5
-3 -2 -1 0 1 2 3 8 3 0 -1 0 3 8
-3 -2 -1 0 1 2 3 9 4 1 0 1 4 9
-3 -2 -1 0 1 2 3 8 3 0 -1 0 3 8
-3 -2 -1 0 1 2 3 5 0 -3 -4 -3 0 5
-3 -2 -1 0 1 2 3 0 -5 -8 -9 -8 -5 0
Y= >>
-3 -3 -3 -3 -3 -3 -3
-2 -2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1 -1 -1 Meshgrid creates the matrices X
0 0 0 0 0 0 0 and Y. The matrix Z is made from
1 1 1 1 1 1 1 the second line.
2 2 2 2 2 2 2
3 3 3 3 3 3 3 A 3D plot can be made from the
matrices X, Y and Z.
3D Plots – mesh plots
• Second, we plot the function:
x = -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
mesh(X,Y,Z)
x = -3:0.5:3
y = -3:0.5:5
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
surf(X,Y,Z)
3D Plots – surface plots
• You may not want to see the lines on your
contour, this may be achieved by using the
function ‘shading flat’
x = -3:0.5:3
y = -3:0.5:5
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
surf(X,Y,Z)
shading flat
3D Plots – surface plots
• You may want to see a smooth surface
plot, this may be achieved by using the
function ‘shading interp’, note that the
lines are also removed.
x = -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y)
Z = X.^2 - Y.^2
surf(X,Y,Z)
shading interp
3D Plots – contour plots
• Contour plots show lines of constant
elevation or height. In MATLAB, contour
plots are generated using the command
contour:
x= -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y);
Z = X.^2 - Y.^2;
contour(X,Y,Z,50)
x= -3:0.5:3
y = -3:0.5:3
[X,Y] = meshgrid(x,y);
Z = X.^2 - Y.^2;
contourf(X,Y,Z,20)
x = -5:0.2:5;
y = -5:0.2:5;
[X,Y] = meshgrid(x,y);
Z = (-3*Y)./(X.^2 + Y.^2 + 1)
mesh(X,Y,Z)
x = -5:0.2:5;
y = -5:0.2:5;
[X,Y] = meshgrid(x,y);
Z = (-3*Y)./(X.^2 + Y.^2 + 1)
surf(X,Y,Z)
shading interp
[X,Y] = meshgrid(x,y);
Z = (-3*Y)./(X.^2 + Y.^2 + 1)
contour(X,Y,Z,20)
[X,Y] = meshgrid(x,y);
Z = (-3*Y)./(X.^2 + Y.^2 + 1)
contourf(X,Y,Z,40)