Sunteți pe pagina 1din 25

Debugging and program design 

Week 11
By JI, Hui
Debugging
• Every programmers, even experienced 
programmers, seldom get programs to run 
correctly the first time
• Bug
– An error in the program
• Debugging
– The process of detecting and correcting such 
errors
Syntax error
• Syntax errors are typing errors in MATLAB 
statements
• the most frequent type of error, and are fatal: 
MATLAB stops execution and displays an error 
message
• However,  the error messages can sometimes 
be rather unhelpful—even misleading
Common examples
• Forgetting closing bracket
>>disp(['the answer is ' , 'quite obvious']

• Forgetting completing loops or conditional 
statements
>>if x < 0
disp(' negative ' )
else if x == 0
disp(' zero')
else
disp(' positive ' )
end
Errors for matrix  calculation
• Illegal operations
>>[1 2 3] * [4 5 6]

>>[1 2 3] + [4 5]

>>[1 2;3 4]*[1;3]

>>[1 2 3;4 5 6]^2

>>[8 4 3 4;9 0 1]
Misc
• Using double quotation instead of single one
>>word =“Something wrong !"
• Left out the arguments when calling function
>>y=sin;
• Wrong variable name
>>name$ = 'Joe'

• There are a large number of possible syntax errors and 
you will probably have discovered a good few yourself
• With experience you will gradually become more adept 
at spotting your mistakes.
Name hiding
• Name hiding
– Recall that a workspace variable ‘hides’ a script or 
function of the same name. The only way to 
access such a script or function is to clear the 
offending variable from the workspace.
– MATLAB function hides a script of the same name, 
e.g. create a script called why.m that displays 
some junk message, and then type why at the 
command line.
Errors in logic
• These are  errors in the actual algorithm that 
gives real troubles
• They are the more difficult to find than syntax 
error
• the program runs, but gives the wrong 
answers! 
• It’s even worse that it may gives you correct 
answer most of the time
Some tips
• Try to run the program for some special cases 
where you know the answers.
• If you don’t know any exact answers, try to 
use your insight into the problem to check 
whether the answers seem to be of the right 
order of magnitude
• Try working through the program by hand or 
using debugging facilities to see if you can 
spot where things start going wrong.
Round error
• Demonstration
>>x = 0.1;
while x ˜= 0.2
x = x + 0.001;
[x,x‐0.2]
end
• Outcome
– The program never stop
• Why
– The variable x never has the value 0.2 exactly, because of 
rounding error.
– x misses 0.2 by about 8.3×10
Recipe
• It would be better to replace the while clause 
with while x <= 0.2
• or, even better, with while abs(x ‐ 0.2) > 1e‐8
• In general, it is always better to test for 
‘equality’ of two non‐integer expressions a
and b as follows:
if abs((a‐b)/a) < 1e‐8
Program design
• So far, we have been concentrating on the 
technical aspects of writing MATLAB statements 
correctly
• The examples we have examined have been very 
simple logically.
• To design a successful program you need to 
understand a problem thoroughly, and to break it 
down into its most fundamental logical stages. 
• a systematic procedure or algorithm is needed for 
solving the problem.
Computer program design process
1. Problem analysis
– recognize the need and develop an understanding 
of the nature of the problem
2. Problem statement
– Develop a detailed statement of the problem to 
be solved with a computer program
3. Processing scheme
– Define the inputs required and the outputs to be 
produced by the program
(cont’)
4. Algorithm design
– Design the step‐by‐step procedure using the top‐down 
design process that decomposes the overall problem 
into subordinate problems
5.  Implementing algorithm
– Translate or convert the algorithm into a computer 
language (e.g. MATLAB) and debug the syntax errors 
until the tool executes successfully.
6.  Test and evaluation
– Test all of the options and conduct a validation study 
of the computer program
How to understand problems
• Asking questions
– What do I know about the problem?
– What is the information that I have to process in 
order the find the solution?
– What does the solution look like?
– What sort of special cases exist?
– How will I recognize that I have found 
the solution?
Top‐down algorithm design
• Breaking the problem into a set of 
subproblems called modules
• Creating a hierarchical structure of problem 
and modules
• This process continues for as many levels as it 
takes to expand every task to the smallest 
details
• A step that needs to be expanded is an 
abstract step
(cont’)
Pseudocode for algorithm design
• Uses a mixture of English and formatting to 
make the algorithm explicit
• Preparing a Hollandaise sauce
(cont’)
• Psuedocode of preparing a Hollandaise sauce
Case study for top‐down design
• Planning a large party
Sub‐program for making list
• Problem
– Create an address list that includes each person’s 
name, address, telephone number, and e‐mail 
address
– This list should then be printed in alphabetical 
order
– The names to be included in the list are on scraps 
of paper and business cards
Top‐down design for making list
(cont’)
(cont’)
(cont’)
Level 1

Level 1

S-ar putea să vă placă și