Intro M4 PDF
Intro M4 PDF
Intro M4 PDF
MATLAB
MATLAB
Importing
7. MATLAB Graphics
The MATLAB environment supports graphic display of a known
function, data resulting from experimental measurements or from
engineering and/or scientific computation through various built-in
functions for plotting.
It also provides control facility over graphic properties.
X = [1 2 3 4 5 6] and Y = [1 4 9 16 25 36]
>> X = [1 2 3 4 5 6];
>> Y = [1 4 9 16 25 36];
>> plot(X,Y)
Six data points are given to MATLAB and then the function plot connected these
consecutive points with lines, i.e. interpolates the data points
If an analytic formula of a function
Example
Consider the interval, I = [1, 6] of the real line.
>> X=1:6
X=
123456
The colon operator : as used above, generates the discrete representatives of the
given interval with the difference between any two consecutive members 1, called
the step size. This is the default use of the colon operator.
In the general case, one specifies the step size r, a number larger or may be
smaller than 1 depending on the number discrete points he need for his purpose.
>> z = 1:0.5:6
z=
Columns 1 through 7
1.0000 1.5000 2.0000 2.5000 3.0000
3.5000 4.0000
Columns 8 through 11
4.5000 5.0000 5.5000 6.0000
>> x = linspace(1,6,11)
x=
Columns 1 through 7
1.0000 1.5000 2.0000 2.5000
3.0000 3.5000 4.0000
Columns 8 through 11
4.5000 5.0000 5.5000 6.0000
In the argument of the MATLAB function linspace, 11 represents
the number n of discrete points needed, while 1 and 6 are the left and
right end points of the interval. The default value of n is 100.
>> x=linspace(-4,5);
>> y=x.^3-2*x.^2-x+2;
>> plot(x,y)
>> grid
>> title('The Graph of y = x^3-2x^2-x+2')
The function grid forces MATLAB to turn on the current axes' grid lines while
the function title outputs the preferred string at the top and in the center of the
current axes. The Syntax is, title('string').
Multiple graphics
We can visualize the plot of two or more graphs on the same set of axes.
These can be done either by plotting them both at the same time, the syntax is
plot(x, f, x, g) or through the use of the command hold on/off. In what
follows, we try to visualize the graphs of
in one and the same set of axes, by plotting them at the same time.
>> x=linspace(-4,5);
>> f = x.^3 - 2.*x.^2 - x + 2;
>> g= 2.*x.^3+2;
>> plot(x,f,x,g,'.-')
>> legend('y = x^3-2x^2-x+2','y=2x^3+2')
>> grid
Next we shall use the command hold on/off to generate the same set of
graphics with MATLAB as the preceding one. In this case, we call the function
plot twice between the commands hold on and hold off as indicated bellow.
>>x=linspace(-4,5);
>>f = x.^3 - 2.*x.^2 - x
+ 2;
>>g = 2.*x.^3+2;
>>hold on
>>plot(x,f)
>>plot(x,g,'.-g')
>>hold off
>>legend('y = x^3-2x^2-x+2','y=2x^3+2')
>>grid
One can use a single window to visualize several plots one after the other without
overlay. The command clf ensures an empty figure window. In case a graphics window
is hidden under other active windows on the desktop, the command shg helps bring
graphics window to the front.
Plotting Options
When visualizing several plots on the same set of axes one has to do something
to distinguish between different plots. MATLAB has a facility that enable us do
this.
Each plot can be given its own distinctive look by modifying, either the way the
points are plotted i.e. as a solid line, as points, as point-line etc, or by using
different colors green blue, red, etc for different curves.
These graphic features are specified by introducing a 3rd argument in the function
plot, The syntax is plot(x,y,s) where s is a character string composed of a letter
for color and others for line type and plot symbol.
>>grid
>>subplot(222)
>>plot(x,g,'g')
>>legend('y=-2x^3+2')
>>grid
>>subplot(223)
>>plot(x,h,'c')
>>grid
>>legend('y=sinx')
>>axis([-6 8 -2 2])
>>subplot(224)
>>plot(x,k,'m')
>>axis([-6 8 -2 2])
>>legend('y=cosx')
>>grid
The above figure window is subdivided into 2 x 2 matrix of separate axes and is
generated with the script in previous slid
where a x b and c y d.
For implicitly defined function f(x,y) ezplot(f, [a,b,c,d]) plots f(x,y) = 0 over
the domainD = {(x, y) : a x b,c y d}.
Example
>> ezplot('cos(x^2 - y^2)',[-3,3,-4,4])
B) Parametric Plot
Example
>> ezplot('sin(t)','cos(t)')
>> grid
The graph of a function, y =
f(x) of a single-variable x
can be displayed in
MATLAB using the function
ezplot , the corresponding
syntax is ezplot(f,[a,b]).
Example
>> ezplot('sinh(x)',[-3,3])
>> grid
C) Field Plot
In MATLAB, we can display a vector quantity, r = u i + vj e.g. velocity
of a wind, the velocity of flow of a fluid etc, measured at uniformly
spaced points in a two dimensional domain D.
The plotter function used to display a vector field would exhibit an
arrow at each data point (x,y), whose direction and magnitude
corresponded to the value (u(x,y),v(x,y)). For two dimensional case,
such a plot can be achieved with the built-in function quiver.
Example
Suppose f(x, y) = -yi + xj defined over the
rectangle [2, 2] [-2,2]. To display the plot
of the vector field, we write the following
>> u=inline(-y,x,y)
>> v=inline(x,x,y)
>> x=linspace(-2,2,11);
>> y=linspace(-2,2,11);
>> [X,Y]=meshgrid(x,y);
If you do not care for the crowded appearance of the vectors, you can
display/try the following lines of commands command,
[x,y]=meshgrid(2:0.2:2);
U = -y;
V=x;
quiver(x,y,u,v)
title('r = -y i + x j')
to get the following
graphics with dense,
i.e. larger number of
arrows
D) Contour Plot
In MATLAB, we can plot contour curves of a function, Z = f(x,y) of two
variables.
A contour plot is the level curves of Z corresponding to a given value.
The first step is specifying the domain, i.e. range of values for x & y
and generating grid of points, which can be done by using the meshgrid
command on the given domain.
We then, compute the function value at each point and finally use
MATLAB's contour command to draw the level curves.
The following lines of commands should produce the level curves of
If you want to display exactly a given number of contours, you can specify
the number, i.e. how many you want. All you need to do is, introduce a
fourth argument to the function contour
>> [x,y] = meshgrid(0:.05:2, -2:.05:2);
>> z = x .* exp(-x.^2 - y.^2);
>> contour(x,y,z,3)
Often, an equation such as this one is difficult (or impossible) to solve for
y in terms of x.
However, one can use MATLAB function contour as an implicit function
plotter, thereby ignoring the need to solve the equation explicitly for y in
terms of x before plotting.
We start by collecting all terms to one side of the equation so as to make
the above equal to zero.
Now, we set
Evidently, the graph of the implicit function is the level curve of f(x; y) = ,
where =0 i.e. a single level curve that can be displayed using the
MATLAB function contour. To render comparison, we shall do this for the
implicit function, cos(x2 y2) = 0 .
<< [x,y] = meshgrid(-3:.05:3, -4:.05:4);
<< z = cos(x.^2 - y.^2);
<< contour(x,y,z,[0,0])
Example
>> t=0:0.01:37;
>> plot3(sin(t),cos(t),t)
>> xlabel('x'),ylabel('y'),zlabel('z')
>> title('Helix')
As with the built-in function for 2D case, plot the function for 3d, plot3
also has options to specify how a line/point should be plotted.
This preference handled by introducing a fourth argument to the function,
for instance we can change the color of the above helix to one of our
choice, say magenta as follows
>> plot3(sin(t),cos(t),t,'m' )
Line style can also be specified for the plot3 function while plotting a
parametric curve over a domain D R^3 .
B) Parametric curves in 3d
The command, ezplot3(x,y,z) plots the space curve over the default
domain 0 < t < 2*pi, provided that t is declared a symbolic variable.
Example
>> syms t
>> ezplot3(t*sin(t),t*cos(t),t,[0,50],animate)
Graphics in 3d, oftentimes represent real world data and hence contour
plots usually permit physical interpretations.
Thus, if a 3d surface is taken to be a topographic map of a mountain then
the corresponding contours represent points of equal elevation or height,
isobars or in case the surface represents a temperature distribution the
contours represent isotherms, points of equal temperature.
MATLAB can be used to visualize not only lines of identical value i.e.
contours but also regions of similar value.
The later is done by using the built-in function contourf, that gives
colorings
Example
>> [x,y] = meshgrid(-2:0.2:2, -2:0.2:2);
>> z = x.*exp(-x.^2 - y.^2);
>> subplot(211)
>> surfc(x,y,z)
>> subplot(223)
>> contour(x,y,z)
>> subplot(224)
>> contourf(x,y,z)