Programming with Matlab - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Programming with Matlab

Description:

... the smallest possible pieces. Write a short function to solve each piece ... Verify each small piece of the code using hand calculations or existing reliable ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 18
Provided by: stub8
Category:

less

Transcript and Presenter's Notes

Title: Programming with Matlab


1
Programming with Matlab
  • ME303 Kinematics
  • Fall 05-06
  • R. Layton

2
Make the computer your servant
not your master!
3
Programming 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

4
The Matlab environment
Help browser
Workspace
New script file (m file)
Command Window
Command History
5
Creating 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

6
Writing 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

7
Two 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

8
Accessing 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

9
Why 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

10
Function 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.

11
Input 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 file

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
12
Local / 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)

13
Matlab Editor
Note filename matches function name
Save and Run Button
Output arguments
Input arguments
Click and drag the tab to undock a window
14
Function 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!

15
Larger 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)
16
A note on syntax
  • A mathematical expression might be written in
    Matlab as
  • t (1/(1px))k
  • But in a report, use correct mathematical syntax,

17
Get 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.
Write a Comment
User Comments (0)
About PowerShow.com