A Guide to MATLAB for Beginners and Experienced Users phần 8 - Pdf 21

Common Problems
209
SOLUTION: When you encounter a syntax error, review your input line carefully for mistakes
in typing.
EXAMPLE: If the user, intending to compute 2 times −4, inadvertently switches the symbols,
the result is
>>2-*4
???2-*4
|
Error: Missing variable or function.
Here the vertical bar highlights the place where MATLAB believes the error is lo-
cated. In this case, the actual error is earlier in the input line.
Spelling Error
CAUSE: Using uppercase instead of lowercase letters in MATLAB commands, or mis-
spelling the command.
SOLUTION: Fix the spelling.
Consider the following accidental misspellings.
EXAMPLE:
>> Sin(pi/2)
??? Undefined command/function ’Sin’.
>> ffzero(@(x) xˆ2 - 3, 1)
??? Undefined command/function ’ffzero’.
>> fzero(@(x) xˆ2 - 3, 1)
ans =
1.7321
Error or Warning Messages When Plotting
CAUSE: There are several possible explanations, but usually the problem is the wrong
type of input for the plotting command chosen.
SOLUTION: Carefully follow the examples in the help lines of the plotting command, and
pay attention to the error and warning messages.
EXAMPLE:

as meaning [(1 +

3i)/2]|x|
1/3
. In order to fix the problem, we must specify the
function in MATLAB more carefully. The correct graph in Figure 11.2 results from
>> plot(X, sign(X).*abs(X).ˆ(1/3))

3
−2 −1
0
1 2
3
−1.5
−1
−0.5
0
0.5
1
1.5
Figure 11.2. Correct Graph of y = x
1/3
.
A Previously Saved M-File Evaluates Differently
One of the most frustrating problems you may encounter occurs when a previously
saved M-file, which you are sure is in good shape, won’t evaluate or evaluates incor-
The Most Common Mistakes
211
rectly when opened in a new session.
CAUSE: Change in the sequence of evaluation, or failure to initialize variables.

• Mismatched delimiters
• Using the wrong delimiters
212
Chapter 11. Troubleshooting
• Plotting the wrong kind of object
• Using uppercase instead of lowercase letters in MATLAB commands, or mis-
spelling commands.
Debugging Techniques
Now that we have discussed the most common mistakes, it’s time to discuss how to
debug your M-files, and how to locate and fix those pesky problems that don’t fit into
the neat categories above.
If one of your M-files is not working the way you expected, perhaps the easiest
thing you can do to debug it is to insert the command keyboard somewhere in the
middle. This temporarily suspends (but does not stop) execution and returns com-
mand to the keyboard, where you are given a special prompt with a K in it. You can
execute whatever commands you want at this point (for instance, to examine some of
the variables). To return to execution of the M-file, type return or dbcont, short
for “debug continue.”
A more systematic way to debug M-files is to use the debugging features of the
MATLAB M-file Editor/Debugger. Start by going to the menu item Tools:Check
Code with M-Lint. This checks the code of the M-file for common mistakes and
syntax errors and prints out a report specifying where potential problems are located.
(You can do the same thing from the Command Window with the command mlint
followed by a space and the name of the file.) Next, use the debugger to insert “break-
points” in the file. Usually you would do this with the Breakpoints menu or with the
“Set/clear breakpoint” icon at the top of the Editor/Debugger window, but you can
also do it from the Command Window with the command dbstop. (See its online
help.) Once a breakpoint has been inserted in the M-file, you will see a little red dot
next to the appropriate line in the Editor/Debugger. (An example is illustrated in Fig-
ure 11.8 below.) Then, when you call the M-file, execution will stop at the breakpoint,

5
−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
Figure 11.3. A First Attempt at Shading the Region between sin x and −sin x.
The graph in Figure 11.3 was obtained by executing
>> shadecurves(’sin(x)’, ’-sin(x)’, 0, pi)
or
>> syms x; shadecurves(sin(x), -sin(x), 0, pi)
This is not really what we wanted; the figure we seek is shown in Figure 11.4.
0 0
.
5
1 1.
5
2 2.
5 3 3
.
5
−1
−0.8
−0.6

0.6
0.7
0.8
0.9
1
Figure 11.5. A First Attempt at Shading the Region between x
2
and

x.
It’s not too hard to figure out why our regions aren’t shaded; that’s because we used
plot (which plots curves) instead of patch (which plots filled patches). So that
suggests we should try changing the last line of the M-file to:
patch([xvals, xvals], [ffun(xvals), gfun(xvals)])
That gives the error message:
??? Error using ==> patch
Not enough input arguments.
Error in ==> shadecurves at 9
patch([xvals, xvals], [ffun(xvals), gfun(xvals)])
so we go back and try
>> help patch
to see whether we can get the syntax right. The help lines indicate that patch re-
quires a third argument, the color (in RGB coordinates) with which our patch is to be
filled. So we change our final line to, for instance,
patch([xvals, xvals], [ffun(xvals), gfun(xvals)], [.5,0,.7])
That gives us now as output to
>> syms x; shadecurves(xˆ2, sqrt(x), 0, 1); axis square
the picture shown in Figure 11.6.
That’s better, but still not quite right, because we can see a mysterious diagonal
line down the middle. Not only that, but if we try

and

x.
−1.
5
−1 −
0
.
5 0 0
.
5
1 1.
5
0
1
2
3
4
5
6
Figure 11.7. A First Attempt at Shading the Region between x
2
and x
4
.
Y, which are its first two inputs. A way to see how this is working is to change
the “50” in line 9 of the M-file to something much smaller, say 5, and then insert a
breakpoint in the M-file before line 9. At this point, our M-file in the Editor/Debugger
window now looks like Figure 11.8. Note the large dot to the left of the last line,
indicating the breakpoint. When we run the M-file with the same input, we now

1 1.
5
0
1
2
3
4
5
6
Figure 11.9. A Second Attempt at Shading the Region between x
2
and x
4
.
Finally we can understand what is going on; MATLAB has “connected the dots”
using the points whose coordinates were just printed out. In particular, MATLAB has
drawn a line from the point (1.5, 2.25) to the point (−1.5, 5.0625). This is not what
we wanted; we wanted MATLAB to join the point (1.5, 2.25) on the curve y = x
2
to
the point (1.5, 5.0625) on the curve y = x
4
. We can fix this by reversing the order
of the x-coordinates at which we evaluate the second function g. So, letting slavx
Debugging Techniques
217
denote xvals reversed, we correct our M-file to read:
function shadecurves(f, g, a, b)
%SHADECURVES Draws the region between two curves
% SHADECURVES(f, g, a, b) takes strings or expressions f

2.64575131106459
The third, that is 2024/765, is the best approximation.
219
220
Solutions to the Practice Sets
2.
(a)
cosh(0.1)
ans =
1.00500416805580
(b)
log(2)
ans =
0.69314718055995
(c)
atan(1/2)
ans =
0.46364760900081
format short
3.
[x, y, z] = solve(’3*x + 4*y + 5*z = 2’,
’2*x - 3*y + 7*z = -1’, ’x - 6*y + z = 3’,
’x’, ’y’, ’z’)
x=
241/92
y=
-21/92
Solutions to Practice Set A: Algebra and Arithmetic
221
z=

5.
syms x y; factor(xˆ4 - yˆ4)
ans =
(x-y)*(x+y)*(xˆ2+yˆ2)
6.
(a)
simplify(1/(1 + 1/(1 + 1/x)))
ans =
(x+1)/(2*x+1)
(b)
simplify(cos(x)ˆ2 - sin(x)ˆ2)
ans =
2*cos(x)ˆ2-1
Let’s try simple instead.
[better, how] = simple(cos(x)ˆ2 - sin(x)ˆ2)
Solutions to Practice Set A: Algebra and Arithmetic
223
better =
cos(2*x)
how =
combine(trig)
7.
3ˆ301
pretty(sym(’3’)ˆ301)
ans =
4.1067e+143
4106744371757651279739780821462649478993910868760123094144405702351069915\
3249722978140061846706682416475145332179398212844053819829708732369\
8003
But note the following:

[%1 \%1/]
3 2 1/2
%1 := -108 q + 12 (12 p + 81 q )
Note how pretty separates out the subexpression
−108q +12

12p
3
+81q
2
,
which occurs in the formulas for all three of the roots, and gives it a name. This
expression is closely related to the discriminant of the cubic equation.
(d)
ezplot(’exp(x)’); hold on; ezplot(’8*x - 4’)
title(’eˆx and 8x - 4’)
Solutions to Practice Set A: Algebra and Arithmetic
225
−6 −4 −2 0 2 4 6
−60
−40
−20
0
20
40
x
e
x
and 8x − 4
fzero(’exp(x) - 8*x + 4’, 1)

x
sin(1/x
2
)
X = -2:0.1:2; plot(X, sin(1./X.ˆ2))
Warning: Divide by zero.
−2 −1 0 1 2
−1
−0.5
0
0.5
1
Both pictures are incomplete. Let’s see what happens if we refine the mesh.
X = -2:0.001:2; plot(X, sin(1./X.ˆ2))
Warning: Divide by zero.
Solutions to Practice Set A: Algebra and Arithmetic
227
−2 −1 0 1 2
−1
−0.5
0
0.5
1
This is better, but because of the wild oscillation near x =0, neither plot nor
ezplot gives a totally accurate graph of the function.
(c)
ezplot(’tan(x/2)’, [-pi pi]); axis([-pi, pi, -10, 10])
−3 −2 −1 0 1 2 3
−10
−5

10
20
30
40
50
x
x
4
and 2
x
Note the large vertical range. We learn from the plot that there are no points of
intersection between 2 and 6 or between −6 and −2; but there are apparently two
points of intersection between −2 and 2. Let’s change to plot now and focus on the
interval between −2 and 2. We’ll plot x
4
dashed.
X = -2:0.1:2; plot(X, 2.ˆX);
hold on; plot(X, X.ˆ4, ’ ’); hold off
Solutions to Practice Set A: Algebra and Arithmetic
229
−2 −1 0 1 2
0
2
4
6
8
10
12
14
16

points.
r1 = fzero(’2ˆx - xˆ4’, -0.9)
r2 = fzero(’2ˆx - xˆ4’, 1.2)
r3 = fzero(’2ˆx - xˆ4’, 16)
230
Solutions to the Practice Sets
r1 =
-0.8613
r2 =
1.2396
r3 =
16
Let’s check that these “solutions” satisfy the equation.
subs(’2ˆx - xˆ4’, ’x’, r1)
subs(’2ˆx - xˆ4’, ’x’, r2)
subs(’2ˆx - xˆ4’, ’x’, r3)
ans =
1.1102e-16
ans =
-8.8818e-16
ans =
0
So r1 and r2 very nearly satisfy the equation, and r3 satisfies it exactly. It is easily
seen that 16 is a solution. It is also interesting to try solve on this equation.
symroots = solve(’2ˆx - xˆ4 = 0’)
symroots =
-4*lambertw(-1/4*log(2))/log(2)
-4*lambertw(-1,-1/4*log(2))/log(2)
-4*lambertw(-1/4*i*log(2))/log(2)
-4*lambertw(1/4*log(2))/log(2)

Solutions to the Practice Sets
−10 −5 0 5 10
−10
−5
0
5
10
Here is a plot with more level curves.
[X, Y] = meshgrid(-10:0.1:10, -10:0.1:10);
contour(X, Y, 3*Y + Y.ˆ3 - X.ˆ3, 30, ’k’)
−10 −5 0 5 10
−10
−5
0
5
10
(b)
Now we plot the level curve through 5.
[X, Y] = meshgrid(-5:0.1:5, -5:0.1:5);
contour(X, Y, 3.*Y + Y.ˆ3 - X.ˆ3, [5 5], ’k’)
Solutions to Practice Set B: Calculus, Graphics, and Linear Algebra
233
−5 0 5
−5
0
5
(c)
We note that f(1, 1) = 0, so the appropriate commands to plot the level curve of f
through the point (1, 1) are
[X, Y] = meshgrid(0:0.1:2, 0:0.1:2);


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status