to set y equal to x, while MATLAB skips the command between the else and end
statements. On the other hand, if x is negative, then MATLAB skips to the else
statement and executes the succeeding command, setting y equal to -x. As with a
for loop, the inden
else
y = -1;
end
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
(e)
x2
e
dx.
4. Compute the following integrals numerically using quadl.
(a)
(b)
(c)
sin x
dx.
0 e
1 3
x + 1 dx.
0
x2
dx. In
e
this case, also nd the error in the numerical answer, by
comparing it with the exact answer found in Problem 3.
5. Evaluate
(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
Symbolic
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
> 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
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
> image(indpic)
> colormap(map)
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.
Images
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.
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
2pi
pi
0
pi
2pi
Figure 5.10. Two Periods of sin x and cos x.
Inci
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
mo
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
plotted
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
Tools:Edit Plot
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
Surfaces in Three-Dimensional Space
There are two basic commands for plotting surfaces in 3-space: mesh and surf. The
former produces a transparent mesh surface, the latter an opaque shaded one. There
are two different ways of using each command, one for
x=
>
>
>
>
1 z 2 cos , y =
[Z, Theta]
X = sqrt(1
Y = sqrt(1
surf(X, Y,
1 z 2 sin . Thus we can produce our plot with
= meshgrid(-1:0.1:1, (0:0.1:2)*pi);
- Z.2).*cos(Theta);
- Z.2).*sin(Theta);
Z); axis square
1
0.5
0
0.5
1
1
1
0.5
0
0
0.5
1 1
Figure 5.7.
3
2
1
0
1
2
3
3
2
1
0
1
2
3
Figure 5.2. Contour Plot of x2 + y 2 .
single level set, you must specify the same level twice. This is quite useful for implicit
plotting of a curve given by an equation in x and y. For example, to plot the circle of
radius 1
Figure 5.4. A Vector-Field Plot of (x, y).
y-coordinates, and one for the z-coordinates. For example, we can plot a helix with
> T = -2:0.01:2;
> plot3(cos(2*pi*T), sin(2*pi*T), T)
2
1
0
1
2
1
1
0.5
0.5
0
0
0.5
0.5
1
1
Figure 5.5. The Helix x = cos(2z), y
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
1
0.5
0
0.5
1
Figure 5.1. The Unit Circle x2 + y 2 = 1.
that looks smooth. Here also axis square forces the same scale on both axes;
without it the circle would look like an ellipse.
Parametric plotting is also possib
Another familiar example is the sum of the innite geometric series:
> syms a k; symsum(ak, 0, Inf)
ans =
-1/(a-1)
Note, however, that the answer is valid only for 1 < a < 1.
The command symsum uses the variable k as its default variable. See the
section b
> syms x; limit(sin(x)/x, x, 0)
ans =
1
To compute one-sided limits, use the right and left options. For example:
> limit(abs(x)/x, x, 0, left)
ans =
-1
Limits at innity can be computed using the symbol Inf.
> limit(x4 + x2 - 3)/(3*x4 - log(x), x, Inf)
an