Title: Programming with Matlab
1Programming with Matlab
- ME303 Kinematics
- Fall 05-06
- R. Layton
2Make the computer your servant
not your master!
3Programming in our course
- For the project, as appropriate
- Iterative analysis
- Possible optimization
- For Exam 8 (acceleration analysis)
- Write your own code to solve an acceleration
analysis problem - Effective use of user-defined functions is
required
4The Matlab environment
Help browser
Workspace
New script file (m file)
Command Window
Command History
5Creating variable names
- Case sensitive
- Any length
- Begin with either an uppercase or lowercase
letter, followed by any combination of letters,
numbers and underscore (_) - No blank spaces may appear
- Two commonly used conventions
- exit_pressure
- ExitPressure
6Writing good code
- Break the problem into the smallest possible
pieces - Write a short function to solve each piece
- Verify each small piece of the code
7Two types of m file
- Script files
- A text file containing a list of commands, each
of which is operated on by Matlab as if they were
typed in the Command window - Function file
- Script files that create their own local and
independent workspace within Matlab - The first line follows a prescribed format,
identifying input and output arguments
8Accessing files
- Script files and function files are run by typing
their file name (without the .m) in the command
window - Matlab must be provided with the path to the
directory in which the file resides - Suggested you save these paths
9Why use functions?
- Avoiding duplicate code
- Limiting the effect of changes to specific
sections of code - Reducing the complexity of the overall code by
making it more readable and manageable - Isolating complex operations
- Making debugging and error isolation easier
10Function filenames
- Save your functions in the file function_name.m
- Function-naming rules variable-naming rules
- Choose function name that is not already defined
- use exist to check - if you accidentally overwrite an existing
function you might create a problem that is hard
to troubleshoot.
11Input and output arguments
- Script file (main program)
Norton problem 7-4k declare some
constants ... w3o,w4o FourbarVelocity(a, b,
c, d, th2, w2,th3o, th4o)
function w3,w4 FourbarVelocity(L2, L3, L4,
L1,th2, w2, th3, th4) function to find the
possible solutions to a four bar mechanism
inputs... ? w3 .. assign a value to the
first output argument w4 .. assign a value
to the second output argument last line
12Local / Global variables
- Note that Matlab matches input / output arguments
by the order--the names do not have to match - Only the variables used in the top level
function/script will be available in the
workspace. Here L1, L2, L3, L4 are local
variables (they are not defined in the workspace)
13Matlab Editor
Note filename matches function name
Save and Run Button
Output arguments
Input arguments
Click and drag the tab to undock a window
14Function debugging tips
- Be sure to assign output arguments!
- Verify each small piece of the code using hand
calculations or existing reliable code - Once a function is verified leave it alone!
15Larger example
Norton problem 7-4k define constants... d 4
a 6 b 10 c 7 th2 88/180pi w2 4
Rpa 8 delta 45pi/180 th3o,th4o,th3x,th4x
Northbar(d,a,b,c,th2) 6-5k
velocities w3o,w4o,Vapo,Vbpo,Vppo
FourbarVels(a,b,c,d,th2,th3o,th4o,w2,Rpa,delta) w
3x,w4x,Vapx,Vbpx,Vppx FourbarVels(a,b,c,d,th2,t
h3x,th4x,w2,Rpa,delta) 7-4k accelerations alf3
o,alf4o FourbarAlphas(a,b,c,d,th2,th3o,th4o,w2,
w3o,w4o,alf2) Aao,Abao,Abo,Aapo,Abapo,Abpo,Apo,Ap
po ... FourbarAccels(a,b,c,th2,th3o,th4o,w2,w3o,
w4o,alf2,alf3o,alf4o,Rpa,delta) alf3x,alf4x
FourbarAlphas(a,b,c,d,th2,th3x,th4x,w2,w3x,w4x,alf
2) Aax,Abax,Abx,Aapx,Abapx,Abpx,Apx,Appx ...
FourbarAccels(a,b,c,th2,th3x,th4x,w2,w3x,w4x,alf2,
alf3x,alf4x,Rpa,delta)
16A note on syntax
- A mathematical expression might be written in
Matlab as - t (1/(1px))k
- But in a report, use correct mathematical syntax,
17Get Help!!
- http//www.mathworks.com/access/helpdesk_r12p1/hel
p/techdoc/learn_matlab/learn_matlab.shtml - http//www.indiana.edu/statmath/math/matlab/getti
ngstarted/index.html - E. B. Magrab, et al, An Engineers Guide to
Matlab, Prentice Hall, 2000.