Title: Lecture 4 MatLab
1Lecture 4 - MatLab
- January 25, 2001
- CVEN 302
2Lectures Goals
- Programming flow controls
- Misc. functions
- Program Design
- Organization
- Setup
- Test
- Debug
- Document
3switch flow control
- switch(relation expression)
- case 1(value)
- block of statements
- case 2(value)
- block of statements
- case 3 (value)
- block of statements
- otherwise
-
- end
- example cond4.m
- The switch command
- can be used for multiple
- case of the if command.
4for flow control
- for(index )
-
- block of statements
-
- end
- example
- x4,5,6,7 sumx0
- for k 1length(x)
- sumx sumx x(k)
- end
- The for command is
- used to do a series of
- steps of redundant
- process.
-
5for flow control
- example
- (counter) startstepfinal
- value size value
- k 12n (1,3,5,n)
- m 0pi/15pi (0,Pi/15,,Pi)
- k n -11 (n,n-1,,1)
- The for command can
- have various forms of
- step sizes.
-
6while flow control
- while(expression)
-
- block statements
-
- end
- Example
- x4
- while (x-1)gt 0.01
- x sqrt(x)
- disp(x)
- end
- The while command
- can do redundant process
- by checking on a logical
- or relational operation or
- combination of the two.
-
7Escape commands
- break is an escape from a loop, which will go
the end of the loop and start the next step in
the program. - return is an escape from a program or
subroutine or a function and return to the main
program.
8Misc. Functions
- disp(expression) - displaces the expression to
the screen. - format (long,e,short, ...) determines the format
of numerical values. - global variable makes a variable available in
any function or program as shared memory.
9Design Steps for a Program
- Design the code to do the problem
- Input types
- Output types
- Write up the code with modified comments.
- Test the code with a simple set of test examples.
- Document the code
10Design of the M-files
- Remain consistent in how you put your
- program together. Break it into parts
- Input information - comments and variables
definition and options verify input
parameters. - Primary computational task
- Output options (plot information, ascii,
filenames, etc.)
11Program Design
- Visual blocking show the layout of the program.
- Whitespace in the text of the program.
- Meaningful variable names
- Documentation
12Visual Blocking
- Which is easier to read?
- n length(x)
- if mean(x) lt0
- y(1)0 y(n) 0
- for k2n-1
- y(k) x(k1)-x(k-1)
- end
- else
- y(1)x(1) y(n) x(n)
- for k2n-1
- y(k)0.5( x(k1) x(k-1))
- end
- end
- How do you visualize your
- code?
- n length(x)
- if mean(x) lt0
- y(1)0 y(n) 0
- for k2n-1
- y(k) x(k1)-x(k-1)
- end
- else
- y(1)x(1) y(n) x(n)
- for k2n-1
- y(k)0.5( x(k1) x(k-1))
- end
- end
13Whitespace layout
- Which line is easier to read?
- y(k)0.5(x(k1)x(k-1)) or
- y(k) 0.5(x(k1)x(k-1)) or
- y(k) 0.5( x(k1) x(k-1) ) or
- y(k) 0.5 ( x(k1) x(k-1) )
14Variable Names
Use variable names, which have meaning so you can
read the code and understand it.
Variable typical code meaningful
code inside diameter d5 ins_diam
5 thickness t0.02 thickness
0.02 inside radius rd/2 ins_radius
ins_diam / 2 outside radius r2rt
out_radius ins_radius thickness
15Documentation
- Documentation is critical for any code that is
not going to be used and immediately discarded.
Documentation takes the form of comment
statements that describe the input and output
parameters of a function as well as the steps
performed in the analysis.
16Documentation Recommendations
- If a program is incorrect, it matters little what
the documentation says. - If documentation does not agree with the code it
is not worth much. - Consequently, code must largely document itself.
If it cannot, rewrite the code rather than
increase the supplementary documentation. Good
code needs fewer comments than bad code does.
17Documentation Recommendations
- Comments should provide additional information
that is not readily obtained from the code
itself. Comments should never parrot the code. - Logical variable names and labels, and layout
that emphasizes logical structure, help make a
program self-documenting. - Kernighan and Plaugher, The Element of
Programming Style, 1978
18Input
- Function definition
- Summary is what the program does.
- Synopsis run program
- Input definition of terms
- Output what type of output
- Notes
19Input Example
- function rho H2Odensity(T,units)
- H2Odensity Density of saturated liquid water
-
- Synopsis rho H2Odensity
- rho H2Odensity(T)
- rho H2Odensity(T,units)
-
- Input T (optional) temperature at which
density is - evaluatedDefault T 20 C If units F
- then T is degrees F
- units (optional) units for input
temperature, - Default C, units C for Celsius
- units F for Fahrenheit
-
- Output rho density, kg/m3,if unit C
or lbm/ft3 if - unit F
-
- Notes Use 4th order polynomial curve fit of
the data in
20Organization of Numerical Method
- Start with an abstract or verbal description of a
- computational task, it will be necessary to
develop a - sequence of smaller tasks that can be implemented
- sequentially.
- Stepwise refinement is designed to break up a
large - task into a set of smaller tasks. The same
procedure - is called top-down design or divide and
conquer.
21Implementation and Testing
- Break the problem into small modules/segments of
- code, which you can test.
- Testing
- It is important to verify that m-files are
working correctly before they are used to produce
results that inform other decisions. The best
check on any calculation, be it manual or
automatic, is by an independent test. One which
you have to designed.
22Debugging
- Searching for and removing bugs is an inevitable
part of programming. Bugs are caused by a number
of factors from outright programming blunders to
the improper application of numerical method.
23Defensive Programming
- Do not assume that the input data are correct.
Test them. - Guard against the occurrence of an impossible
condition that would cause a failure in a
calculation. - Provide a catch or default condition for a
sequence of if elseif else constructs. - Provide diagnostic error messages that will help
the user determine why the function failed to
complete it calculation.
24Debugging Tools
- type and dbtype commands
- type prints out the file error
- dbtype will print out the file with line
numbers. dbtype startstop will print out
lines from start to stop. - Error function - error(message) will print out
the message and halt execution.
25Debugging Tools
- pause command temporarily suspends the
execution of the m-file. Useful way to print a
warning message without necessarily aborting an
analysis. - keyboard command keyboard command suspends
execution of an m-file and gives control to user.
It gives the user access to internal variables
of the function containing command.
26Debug example
- The example program
- keyboard stops a program to check internal
- variables. It is a powerful tool.
- Kgtgt is the prompt and you can use it to check
numbers out. - Kgtgt return will return to the executable
program.