Lecture 3 Matlab Introduction - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Lecture 3 Matlab Introduction

Description:

Lecture 3 - Matlab Introduction. CVEN 302. June 1, 2001. Lecture Goals. Matlab files ... Bugs are caused by a number of factors from outright programming ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 43
Provided by: San59
Category:

less

Transcript and Presenter's Notes

Title: Lecture 3 Matlab Introduction


1
Lecture 3 - Matlab Introduction
  • CVEN 302
  • June 1, 2001

2
Lecture Goals
  • Matlab files
  • Matlab controls

3
M-files
  • These files load constants lists and executable
  • programs into memory. For example
  • dir
  • myCon.m
  • type myCon
  • myCon or load myCon

4
Function M-files
  • These files are similar to subroutines and will
  • act as built-in functions, i.e. a tool box
    similar
  • to functions, sin(x), log(x)
  • Open a new m-file

5
Function files
  • Function name(input variables)
  • function statements
  • There are set of example files show be in the
    working directory.
  • twosum(x,y)
  • addmult(x,y)

6
Function m-files
  • The functions act similar to any of the built-in
    functions, sin(x), ln(x), etc. The inputs and
    outputs of the functions can be a variable,
    vector and/or a matrix.

7
Input functions in m-files
  • Input function allows a program to input data
    into memory. The function is defined as
  • variable input( string request )
  • Example inputAbuse.m file

8
Output functions in m-files
  • Output function allows a program to output data
    to screen. The function is defined as
  • fprintf( g\n,variable)
  • g is a definition of type of variable
  • \n causes a carriage return.

9
Flow controls
  • There are three types of flow control devices.
  • Relational Operations
  • Arithmetic Operations
  • Logistic Operations

10
Relational Operations
  • Relational operations are
  • used for comparison of
  • different values and
  • generate a true/false
  • result.
  • lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or
  • equal
  • equal to
  • not equal

11
Logistic Operations
  • Logistic operations are
  • used to combine relational
  • operations to generate a
  • true/false result.
  • and
  • or
  • not

12
Relational and Logistic Operations
  • a2b4
  • Relational Operations
  • aisSmaller altb results in aisSmaller 1
    (true)
  • bisSmaller blta results in bisSmaller 0
    (false)
  • Logistic Operations
  • bothTrue aisSmaller bisSmaller bothTrue 0
    (false)
  • eithTrue aisSmaller bisSmaller eithTrue 1
    (true)
  • eithTrue 0 (false)

13
Operational Precedence
  • Operational precedence means the hierarchy of the
    operators
  • Arithmetic Operations
  • Relationship Operations
  • Logical Operations

14
Operational Precedence example
  • Arithmetic Operations
  • x 2 32/4 is
  • x 2 ( 32 / 4 ) not x 2 3(2/4)
  • So for Operational precedence
  • y 3 gt 8-3 sqrt(15) gt 4
  • y 3 gt 5 3.873 gt 4 arithmetic operations
  • y 0 0 relational operations
  • y 0 logistic

15
Program flow controls
  • if else end statements
  • switch structure
  • for structure
  • while statements

16
if flow control
  • The if command can be
  • used to control the flow
  • of the program. There
  • are three basic forms of
  • the if controls.
  • if (relation expression)
  • block of statements
  • end
  • example cond1.m

17
if flow control
  • The if command can
  • be used with an else
  • command.
  • if (relation expression)
  • block of statements
  • else(relation expression)
  • block of statements
  • end
  • example cond2.m and
  • cond3.m

18
switch 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.

19
for 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.

20
for 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.

21
while 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.

22
Escape 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.

23
Misc. 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.

24
Design 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

25
Design 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.)

26
Program Design
  • Visual blocking show the layout of the program.
  • Whitespace in the text of the program.
  • Meaningful variable names
  • Documentation

27
Visual 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

28
Whitespace 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) )

29
Variable 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
30
Documentation
  • 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.

31
Documentation 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.

32
Documentation 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

33
Input
  • Function definition
  • Summary is what the program does.
  • Synopsis run program
  • Input definition of terms
  • Output what type of output
  • Notes

34
Input 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

35
Organization 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.

36
Implementation 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.

37
Debugging
  • 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.

38
Defensive 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.

39
Debugging 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.

40
Debugging 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.

41
Debug 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.

42
Homework
  • Check the Homework webpage.
  • You will be writing a simple program to generate
    a set of points and generate a plot.
Write a Comment
User Comments (0)
About PowerShow.com