Many commands allow more than one class of input, though sometimes only one
data class is mentioned in the online help. This exibility can be a convenience in
some cases and a pitfall in others. For example, the integration command, int, accepts strings a
The command colormap changes the colormap of the current axes or, with no input
arguments, outputs the current colormap.
You can edit an image by changing the values in the image array. Notice that
when you display an image
a media le in a format that MATLAB supports by double-clicking on it in the Current
Directory Browser or by selecting File:Import Data.
MATLAB can read, write, and edit images, such as those created by a digital camera,
found on the World Wide Web,
Color = [0.8 0.8 0.8]
CurrentAxes = [151.001]
Children = [151.001]
Here Color gives the background color of the plot in red-green-blue (RGB) coordinates, where [0 0 0] is black and [1 1 1] is white; [0.8 0.8 0.8] is light gray. Notice
that CurrentAxes and
(rather than in decimal notation, which is ugly here). It also increases the size of the
labels to a 16-point font. The result is shown in Figure 5.10.
Figure 5.10. Two Periods of sin x and cos x.
Notice that many symbols (such as the arrow pointing to the left in Figure 5.9) can
be inserted into a text string by calling them with names starting with \. (If youve
used the scientic typesetting program TEX, youll recognize the convention here.) In
clicking on or near the curve. Right-clicking in the gure window gives several options; changing Selection Style to Mouse Position will allow you to click on an arbitrary point on the curve rather than just a data point. (Remember that the curves
can then adjust the color as desired; for instance, typing rgbpic(end, 1, 2) =
180 will increase the green intensity, making the color lighter and more yellow.
Of course, changing a single pixel will not change the appearance of the gure
much, but you can
> T = (0:0.01:2)*pi;
> figure, axis equal, axis([-1 1 -1 1]), hold on
> comet(cos(T), sin(T)
displays uniform circular motion.
We used hold on here not to save a previous graph, but to preserve the axis
properties we had just set. Without hold on, MATLAB
y = -1;
Here if the input x is positive, then the output y is set to 1 and all commands from
the elseif statement to the end statement are skipped. (In particular, the test in the
elseif statement is not performed.) If x is not positive, then MAT
Now suppose that one has a population in which mating occurs only with ones
identical genotype. (Thats not far-fetched if we are considering a controlled
plant or vegetable population.) Next suppose that x0 , y0 , and z0 denote the
percentages of the popu
(b) An n n matrix is non-singular if its rank is n. Which of the four you
computed in part (a) are non-singular?
(c) Another measure of non-singularity is given by the determinant a fundamental result in linear algebra is that a matrix is non-singular pre
Every time you create an M-le, you are writing a computer program using the MATLAB programming language. You can do quite a lot in MATLAB using no more than
the most basic programming techniques that we have already introduced. In particular, we discussed
4. Compute the following integrals numerically using quadl.
x + 1 dx.
this case, also nd the error in the numerical answer, by
comparing it with the exact answer found in Problem 3.
(f) f (x) = erf(x), n = 8, c = 0.
8. Plot the following surfaces:
(a) z = sin x sin y for 3 x 3 and 3 y 3,
(b) z = (x2 + y 2 ) cos(x2 + y 2 ) for 1 x 1 and 1 y 1.
9. Create a 17-frame movie, whose frames show lled red circles of radius 1/2
centered at the
Problems 2, 3, 57, and parts of 1012 require the Symbolic Math Toolbox.
The others do not.
1. Use contour to do the following.
(a) Plot the level curves of the function f (x, y) = 3y + y 3 x3 in the region where x and y are between 1 and 1 (to ge
sound, and plays it. A sinusoidal vector corresponds to a pure tone, and the
frequency of the sinusoidal signal determines the pitch. Thus the following example
plays the motto from Beethovens Fifth Symphony:
> x = (0:0.1:250)*pi; y = zeros(1,200); z = (0
Edit:Current Object Properties. In MATLAB 5.3, select File:Property Editor.
To modify objects in the gure window with the mouse, editing must be enabled in
that window. In MATLAB 6 and later, you can enable or disable editing by selecting
Multiple Figure Windows
When you execute the rst plotting command in a given MATLAB session, the graph
appears in a new window labeled Figure 1. Subsequent graphics commands either
modify or replace the graph in this window. You have seen that hold on dir
Doing Calculus with MATLAB
MATLAB has in its Symbolic Math Toolbox built-in commands for most of
the computations of basic calculus.
You can use diff to differentiate symbolic expressions, and also to approximate the
not y. If you want to solve for y in this example, you need to enter solve(x
+ y = 3, y). MATLABs default variable for solve is x. If there is no
x in the equation(s) to solve, MATLAB looks for the nearest letter to x (where y
takes precedence over w, but
number i is represented as i in MATLAB. Although you may never have occasion to
enter a complex number in a MATLAB session, MATLAB often produces an answer
involving complex numbers. For example, many polynomials with real coefcients
have complex roots.
by itself denotes an entire row or column. For example, A(:,2) denotes the second
column of A, and A(3,:) yields the third row of A.
MATLAB has several commands that generate special matrices. The commands
zeros(n,m) and ones(n,m) produce n m matrices of
function M-le, called sq.m:
function z = sq(x)
% sq(x) returns the square of x.
z = x.2;
Typing sq(3) produces the answer 9, whether or not x or z is already dened in your
Workspace. Running the M-le neither denes them, nor changes their denitions if
left hand side of equation = right hand side of equation
Notice that string output, like symbolic output, is not indented.
Symbolic and Floating-Point Numbers
We mentioned above that you can convert between symbolic numbers and oatingpoint numbers w
> syms t, int(h(t), t)
then the previous denition of h is substituted into the symbolic expression h(t)
before the integration is performed.
In Chapter 2 we described how to create an anonymous or inline function from an
In MATLAB, an expression can belong to either the string or the symbolic class
of data. Consider the following example.
> f = x3 - 1;
This result may be puzzling if you are expecting f to act like a function. Since f is a
string, f(7) denot
> syms x, int(asin(x), 0, 1)
You are undoubtedly aware that not every function that appears in calculus can be
symbolically integrated, and so numerical integration is sometimes necessary. MATLAB has two commands for numerical integration o