Christisn Vidad Lab 1
Christisn Vidad Lab 1
REPORT
CpE 311
NUMERICAL METHODS
CHRISTIAN A. VIDAD
BSCPE - III
LAB #1: Preliminaries
Exercise 1:
Edit the file by typing the command edit demoscript.m(as yourinstructor) or by double-clicking on the file
in the ``Current Directory'' window pane, or using the ``File Open'' menu pick. An edit window will show
up. Read through the file: the comments make it self-explanatory. You can tell Matlab to execute (that is,
cause the statements in the file to be executed) the file by typing its name, without the ``.m'', on the
command line. (There is another method for executing a file that is not so appropriate for this course: you
can choose ``Run'' from the Debug menu on the edit window.) Do not be confused by the final few
statements in the file. They refer to the following exercise. The following two exercises illustrate the use
of the debugging capability of Matlab. Most of the time you will be able to see what is wrong from the
Matlab error messages, but sometimes the error is not obvious. In Exercise 2 below, you will see what
you might do when you just cannot see why something is wrong.
Exercise 2:
1.Turn on debugging with the commands
dbstop if error
dbstop if naninf
or through the Breakpoints menu on the Edit window. Now, look at demoscript.m in the edit window. At
the bottom there is a statement %bad=1/(x-1);. The percent is a comment character, so this statement is
not executed. Make this statement active by deleting the percent character and save the changed file.
2. Execute the script file by typing its name without the .m at the command line or by choosing ``Run''
from the Debug menu on the Edit window.
3. The division by zero caused an exception and Matlab popped up a window with the offending source
line highlighted. You should also note the changed prompt in the command window. Note: The window
with debugging information in it is the same as the edit window. It may not automatically pop up and you
may have to look for it on your desktop.
4. In the edit window, place the mouse pointer on top of the x in the expression bad=1/(x-1) and leave it
there motionless for a second or so (this is called`hovering''). The value of x should be displayed. You can
then see why the error occurred.
5. Look at the prompt in the command window. It has changed to K». You can do any legal Matlab
command at this changed prompt. In particular, you can type the name x to get Matlab to print out its
value. This is an alternative way to see the value of a variable in a debug situation.
6. You can exit from debugging mode either using the menu in the edit window or with the command
Dbquit This action will return Matlab to its usual » prompt.
7. You can turn off debugging feature with the command dbclear all or from the debugging menu in the
edit window.
8. Debugging (finding errors in) code you have written is the most time-consuming and least rewarding
task in writing code. I am sure you think you will not be making errors, but everyone makes errors and
they can be very difficult to find. You can often find your errors based on the line numbers included with
Matlab's error messages, but sometimes the error remains a mystery. In that latter case, the Matlab
debugger is the most powerful tool you have available. Another important use of the debugger is for
tracing through a piece of code to help you understand how it works. The benefit of the debugger used for
tracing is that the effect of each statement is immediately clear. Careful tracing is the quickest way to
learn how code works. The following exercise illustrates how you can use the debugger to trace
execution. It uses the same demoscript.m file as before.
Exercise 3:
1.Type the command clear at the command line. This will return Matlab to its state just after starting up.
No variables will show in the ``Workspace'' windowpane.
2. Click on the first executable line (x=1.3) of the demoscript.m file in the edit window. Go to the
``Breakpoints'' menu and ``set'' a breakpoint there. A breakpoint is a place where the debugger will
always stop execution. When a breakpoint is set, a red dot will be placed next to the line number in the
edit window. You can also click on the dash to the left of the line number to set a breakpoint at that line.
3. Begin executing the file either by choosing ``Run'' from the debug menu in the edit window, by
pressing the ``Run'' button, or by typing the name demoscript at the command line. The edit window will
show that the script is poised to execute the first line of the file. (That line has not yet been executed.)
4. Predict, in your mind, what will happen when the line is executed. In this case, the value of x will
change to 1.3 and the result ``x=1.3'' will be printed because there is no semicolon at the end of the line.
5. Choose ``Step'' from the Debug menu , press the ``Step'' button (hovering over a button brings up its
description) or issue the command ``dbstep'' at the command line. This will cause the current line (line 8)
to be executed. You will see the variable x appear in the Workspace windowpane and x=1.3000 appears
in the command window.
6. Predict what will happen on the next step. What will the value of the variable xsquared become?
7. Predict what will happen on the next step. What will the value of the variable p become? (An estimate
is good enough.)
8. Continue using ``Step'' and watch the script execute, one line at a time.
9. If the next line were to call a function, the button next to the ``Step'' button is the ``Step in'' button. The
``Step'' button goes on, using the value of the function while the ``Step in'' button jumps to the code inside
the function and steps there.
10. Pressing the ``Continue'' button causes Matlab to continue executing the script until another breakpoint
is reached or until the end of the script is reached.
11. Press the ``End Debugging'' button to exit from debug mode. Alternatively, choose Debug Exit Debug
Mode from the menus or use the command dbquit.
Getting help
It is important to be able to get help when you need it. Matlab provides two help facilities from inside
Matlab itself and a third on the web. The easiest way to get help is to use the ``Help'' menu at the top of the
Matlab window. Command-line help is also available from the Matlab prompt by typing ``help command''.
For example,
help diary
You will get a short description of how to use the command. You will also get a list of related commands
near the bottom of the help description, and you will often find other appropriate commands there. When
you write your own Matlab files, you should always include some special comments in the beginning of
the file. The comments up to the first executable statement or blank line will be printed out in response to
the help command. For example, the command
help demoscript
will give a quick help message from the first three lines of demoscript.m. You may notice that the first of
these lines is included in the file listing in the ``Current Directory'' windowpane. A second way to get help
from the command prompt is the following.
Helpdesk
This command brings up a comprehensive help facility, the same one that the Help menu brings up.