Desktop Tools and Development Environment Previous page   Next Page

Completing the Example

To correct the problem in the example, do the following:

  1. End the debugging session. One way to do this is to select Exit Debug Mode from the Debug menu.
  2. In collatzplot.m line 12, change the string plot_seq to seq_length(N) and save the file.
  3. Clear the breakpoints in collatzplot.m. One way to do this is by typing
  1. in the Command Window.

  1. Run collatzplot for m = 3 by typing
  1. in the Command Window.

  1. Verify the result. The figure shows that the length of the Collatz series is 1 when n = 1, 2 when n = 2, and 8 when n = 3, as expected.

    Image of plot. Ther is one point at 1,1, one point at 2,2, and one point at 3,8.

  2. Test the function for a slightly larger value of m, such as 6, to be sure the results are still accurate. To make it easier to verify collatzplot for m = 6 as well as the results for collatz, add this line at the end of collatz.m
  1. which displays the series in the Command Window. The results for when n = 6 are

    Then run collatzplot for m = 6 by typing

  1. To make debugging easier, you ran collatzplot for a small value of m. Now that you know it works correctly, run collatzplot for a larger value to produce more interesting results. Before doing so, you might want to suppress output for the line you just added in step 6, line 19 of collatz.m, by adding a semicolon to the end of the line so it appears as
  1. Then run

    The following figure shows the lengths of the Collatz series for n = 1 through n = 500.

    Image of plot showing 500 points.


Previous page  Correcting an M-File Running Sections in M-Files That Have Unsaved Changes Next page

© 1994-2005 The MathWorks, Inc.