EENG 2101 Matlab for Engineers - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

EENG 2101 Matlab for Engineers

Description:

The prompt string between single quotes will be printed and Matlab will wait for ... Suppresses echoing variable to screen. EENG 2101. Matlab for Engineers ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 17
Provided by: dbe93
Category:

less

Transcript and Presenter's Notes

Title: EENG 2101 Matlab for Engineers


1
EENG 2101Matlab for Engineers
  • Introduction to programming in Matlab
  • Matlab input and output
  • input statement
  • variableinput(Enter a value )
  • The prompt string between single quotes will be
    printed and Matlab will wait for the user to
    enter a value.
  • variableinput(Enter a value )
  • Suppresses echoing variable to screen

2
EENG 2101Matlab for Engineers
  • Introduction to programming in Matlab
  • Matlab program output
  • Most simple form of output is to type the
    variable name.
  • x25 x will cause the value of x to be
    printed
  • x5 10 15 20 25
  • x
  • x
  • 5 10 15 20 25
  • disp statement disp(x)
  • 5 10 15 20 25

3
EENG 2101Matlab for Engineers
  • Introduction to programming in Matlab
  • Formatted output fprintf statement
  • fprintf(format_string, variable(s))
  • fprintf(Gain f dB,gain)
  • Prints the string Gain followed by the value
    of gain in fixed-point notation.
  • fprintf(Gain f dB\n,gain)
  • Prints the string Gain followed by the value
    of gain in fixed-point notation and a line feed.

4
EENG 2101Matlab for Engineers
  • Introduction to programming in Matlab
  • Other formats that may be specified by the
    placeholder
  • e prints in scientific notation
  • g uses the shorter of fixed-point or
    scientific notation
  • Width and precision fields may be included to
    specify the number of characters to be printed in
    a numerical result and the location of the
    decimal point in either fixed-point or scientific
    notation.
  • 7.2f 7 total characters (width), 2 after
    decimal point

5
EENG 2101Matlab for Engineers
  • Introduction to programming in Matlab
  • Matlab programs are of two typesscripts and
    functions
  • Scripts
  • sequences of Matlab executable statements
  • stored as files with the extension .m
  • invoked by typing the name of the file without
    extension
  • require no arguments
  • return no results

6
EENG 2101Matlab for Engineers
  • Introduction to programming in Matlab
  • Functions
  • sequences of Matlab executable statements
  • stored as files with the extension .m
  • require arguments
  • return results to the calling environment
  • require standard syntax
  • function outvar fname(invar)
  • where invar is the input variable, outvar is
    the output variable, fname is the function name
    (which must be the same as the file name)

7
EENG 2101Matlab for Engineers
  • Example cosine function that accepts arguments
    in degrees
  • function ydcos(phi)
  • ycos(phipi/180)
  • Using this function
  • dcos(30) returns the cosine of 30
  • zdcos(-120) assigns cos(-120) to z
  • Adcos(010360) vector input and output
  • Local variables variables used within functions
    are local to those functions and are not
    accessible from outside the function.

8
EENG 2101Matlab for Engineers
  • Functions with multiple inputs and/or outputs
  • function outvar1, outvar2, fname (invar1,
    invar2)
  • Matlab treats lines beginning with as
    comments. Comment lines immediately following
    the function declaration and prior to the first
    Matlab command are printed when help fname is
    typed.
  • function ydcos(phi)
  • ydcos(phi) computes the cosine of angle phi
    whose
  • value is given in degrees
  • ycos(phi180/pi)

9
EENG 2101Matlab for Engineers
  • Rules for writing functions
  • The first line must be the function declaration
    listing the keyword function, the output
    variable(s), the function name (which must match
    the file name), and the input variable(s)
  • Use comments!
  • Be careful in writing functions with more than
    one input argument that the proper matrix
    operators are used.
  • function retvalexpcos(a,w,t)
  • will work if a, w, and t are all scalars
  • but will produce an error message if t is a
    matrix
  • retvalexp(-at)cos(wt)

10
EENG 2101Matlab for Engineers
  • Matlab relational operators
  • lt Less than lt Less than or equal to
  • gt Greater than gt Greater than or equal to
  • Equal to Not equal to
  • x3 y4 x lt y
  • Returns a value of 1, indicating TRUE.
  • x3 5 7 y9 -1 7 x y
  • Returns 0 0 1 indicating that the last elements
    in x and y are the same, but the other elements
    in x differ from y.

11
EENG 2101Matlab for Engineers
  • Matlab logical operators
  • AND OR NOT
  • Example
  • x y z gt4
  • x3, y5, z2
  • x2 1 2 y1 1 1 z6 3 1
  • x9 0 7 y 8 0 7 z3 4 5

12
EENG 2101Matlab for Engineers
  • Matlab find command returns the indices of
    nonzero elements in a vector x.
  • voltage12.4 12.4 12.5 11.9 10.8 12.1
    12.4
  • low_voltagesfind(voltagelt12.0)
  • fault_voltagesvoltage(low_voltages)
  • The find function will return either an element
    number or, if called with two returned values,
    will supply the indices in row and column form.

13
EENG 2101Matlab for Engineers
  • Matlab if, and else commands
  • if ltlogical statementgt
  • Matlab executable statements
  • end
  • if ltlogical statementgt
  • Matlab executable statements
  • else
  • Matlab executable statements
  • end

14
EENG 2101Matlab for Engineers
  • Matlab elseif command
  • if ltlogical statementgt
  • Matlab executable statements
  • elseif ltlogical statementgt
  • Matlab executable statements
  • elseif ltlogical statementgt
  • Matlab executable statements
  • else
  • Matlab executable statements
  • end

15
EENG 2101Matlab for Engineers
  • Matlab for loops
  • for indexexpression
  • Matlab executable statements
  • end
  • for k110
  • Matlab statements (executed 10 times)
  • end

16
EENG 2101Matlab for Engineers
  • Matlab while loops
  • while logical_expression
  • Matlab executable statements
  • end
  • The loop repeats until the logical expression
    becomes false (i.e., becomes zero) program flow
    then passes to the statement following the end.
Write a Comment
User Comments (0)
About PowerShow.com