in black and white
Main menu
Home About us Share a book
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

A Guide to MATLAB for Beginners and Experienced Users - Brian R.H.

Brian R.H., Roland L.L. A Guide to MATLAB for Beginners and Experienced Users - Cambrige, 2001. - 346 p.
Download (direct link): beginnersandex2001.pdf
Previous << 1 .. 66 67 68 69 70 71 < 72 > 73 74 75 76 77 78 .. 91 >> Next

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 command 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 MATLAB M-file debugger to insert "breakpoints" 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 this from the command line with the command dbstop. Once a breakpoint is 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 Figure 11-8 below.) Then when you call the M-file, execution will stop at the breakpoint, and just as in the case of keyboard, control will return to the Command Window, where you will be given a special prompt with a K in it. Again, when you are ready to resume execution of the M-file, type dbcont. When you are done with the debugging process, dbclear "clears" the breakpoint from the M-file.

Let's illustrate these techniques with a real example. Suppose you want to construct a function M-file that takes as input two expressions f and g (given either as symbolic expressions or as strings) and two numbers a and b, plots the functions f and g between x = a and x = b, and shades the region in between them. As a first try, you might start with the nine-line function M-file shadecurves.m given as follows:

function shadecurves(f, g, a, b)

%SHADECURVES Draws the region between two curves % SHADECURVES(f, g, a, b) takes strings or expressions f % and g, interprets them as functions, plots them between % x = a and x = b, and shades the region in between. 228

Chapter 11: Troubleshooting

Figure 11-3

% Example: shadecurves('sin(x)', '-sin(x)', 0, pi)

ffun = inline(vectorize(f)); gfun = inline(vectorize(g));

xvals = a:(b - a)/50:b;

plot([xvals, xvals], [ffun(xvals), gfun(xvals)])

Trying this M-file out with the example specified in the help lines, that is, executing

>> shadecurves('sin(x)', '-sin(x)', 0, pi)


>> syms x; shadecurves(sin(x), -sin(x), 0, pi)

gives the output shown in Figure 11-3.

This is not really what we wanted; the figure we seek is shown in Figure 11-4. To begin to determine what went wrong, let's try a different example, say

>> shadecurves('x"2', 'sqrt(x)', 0, 1) >> axis square


>> syms x; shadecurves(x"2, sqrt(x), 0, 1) >> axis square

Now we get the output shown in Figure 11-5. Debugging Techniques


Figure 11-4

Figure 11-5 230

Chapter 11: Troubleshooting

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.m

On line 9 ==> patch([xvals, xvals], [ffun(xvals), gfun(xvals)])

So we go back and try

>> help patch

to see if we can get the syntax right. The help lines indicate that patch requires 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)], [.2,0,.8])

That gives us now as output to 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

>> syms x; shadecurves(x"2, x"4, -1.5, 1.5)

we now get the bizarre picture shown in Figure 11-7.

There aren't a lot of lines in the M-file, and lines 7 and 8 seem OK, so the problem must be with the last line. We need to reread the online help for patch. It indicates that patch draws a filled 2D polygon defined by the vectors X and 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 obtain in the Command Window a K>> prompt. At this point, it is logical to try to list the coordinates of the points that are the vertices of our filled polygon, so we try Debugging Techniques 231

Figure 11-7 232

Chapter 11: Troubleshooting
Previous << 1 .. 66 67 68 69 70 71 < 72 > 73 74 75 76 77 78 .. 91 >> Next