+ All Categories
Home > Documents > Matlab CH01

Matlab CH01

Date post: 06-Mar-2016
Category:
Upload: riotdiscoman1438
View: 269 times
Download: 0 times
Share this document with a friend
Description:
Class slides

of 17

Transcript
  • Chapter 1 Slide 1ME280 - Structured Programming

    MATLAB Windows

    Default View

    Command Window main window to enter commands and run programs

    Current Folder Window shows MATLAB m-files in the current folder

    Workspace Window provides information about currently used variables

    Command History Window log of commands entered in the command window

  • Chapter 1 Slide 2ME280 - Structured Programming

    MATLAB Windows

    Additional Windows

    Figure Window displays graphs from graphics commands automatically upon execution of the graphics command

    Editor Window provides capability for writing and editing programs and is opened under the File New Script menu or button

    Help Window provides help information interactively and is accessed via the Help menu or F1

  • Chapter 1 Slide 3ME280 - Structured Programming

    MATLAB Windows

    Configuring MATLAB

    Close an individual window within MATLAB by clicking the in the upper right corner of the window

    Desktop Layout menu provides options for opening and closing various MATLAB windows or combinations of MATLAB windows

    Undock a window by clicking and then in the upper right corner of the window

    Redock by toggling in the upper right corner of the window

  • Chapter 1 Slide 4ME280 - Structured Programming

    Command Window

    Entering commands once a command is entered it cannot be modified and reexecuted

    >> denotes the command line prompt

    Edit commands before they are executed by using backspace, left-arrow, right-arrow and delete keys

    Select and edit previous commands entered using the or

    Smart recall retrieves previously entered commands based on the first few characters of the command and the up-arrow key

    Tab completion allows automatic completion of a previously defined name based on the first few letters of the name

    Semicolon (;) prevents the value of a command from being written

    Commas (,) or semicolons (;) allow several commands on the same line

    Ellipse () may be used to continue a long command onto another line

    % symbol denotes a comment line that is not executed (documenting)

  • Chapter 1 Slide 5ME280 - Structured Programming

    Scalar Arithmetic Operators

    Mathematical Operators

    Addition (3 + 5 or a + b)

    Subtraction (7 3 or a b)

    Multiplication (6 * 4 or a * b)

    Division (2 / 3 = 3 \ 2 or N / D = D \ N)

    Exponential ( 5 ^ 3 or a ^ b)

    Order of Precedence

    Parentheses starting from the innermost

    Exponentiation

    Multiplication and division

    Addition and subtraction

  • Chapter 1 Slide 6ME280 - Structured Programming

    Answer Display

    Format Command - changes the display format of numbers

    Style Result Example

    short (default)Short fixed decimal format, with 4 digits after the decimal point. If you are displaying a matrix with a

    wide range of values, consider using shortG 3.1416

    longLong fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after

    the decimal point for single values. 3.141592653589793

    shortE

    Short scientific notation, with 4 digits after the decimal point. Integer-valued floating-point numbers

    with a maximum of 9 digits do not display in scientific notation. 3.1416e+00

    longE

    Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after

    the decimal point for single values. Integer-valued floating-point numbers with a maximum of 9

    digits do not display in scientific notation. 3.141592653589793e+00

    shortG The more compact of short fixed decimal or scientific notation, with 5 digits. 3.1416

    longGThe more compact of long fixed decimal or scientific notation, with 15 digits for double values, and

    7 digits for single values. 3.14159265358979

    shortEngShort engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple

    of 3. 3.1416e+000

    longEngLong engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.

    3.14159265358979e+000

    bankCurrency format, with 2 digits after the decimal point. 3.14

  • Chapter 1 Slide 7ME280 - Structured Programming

    Answer Display

    Format Command - changes the display format of line spacing

    Style Result Example

    compact

    Suppresses excess line feeds to show more output in a single screen. Contrast with loose. theta = pi/2

    theta =

    1.5708

    loose

    Adds linefeeds to make output more readable. Contrast with compact. theta = pi/2

    theta =

    1.5708

  • Chapter 1 Slide 8ME280 - Structured Programming

    Functions

    Mathematical Functions

    Absolute value (abs)

    Sine (sin or sind), cosine (cos or cosd), tangent (tan or tand), arcsine (asin), arccosine (acos), arctangent (atan or atan2)

    Round (round), ceiling (ceil), floor (floor)

    Square root (sqrt)

    Natural logarithm (log), logarithm base 10 (log10)

    Many others use shift+F1 to browse available functions

    General Functions

    Clock provides a vector as [year month day hour minute second]

    Date provides the current date as dd-mm-yyyy

    Calendar provides a calendar for the current month or specified month and year

    Many others

  • Chapter 1 Slide 9ME280 - Structured Programming

    Scalar Number

    Constants

    pi ()

    eps represents the smallest difference between two numbers ( 2.2204e-16)

    i or j represents the imaginary number

    Indefinite numbers

    Infinity is a valid input and is designated by Inf

    Division by zero gives a warning and returns Inf

    Not-a-Number is an undefined value designated by NaN

    Returned by calculations such as 0 / 0

  • Chapter 1 Slide 10ME280 - Structured Programming

    Variables

    Variables - fundamental to programming and are actually pointers to allocated memory space

    for storage

    Name starts with a letter character

    Name may only have letters, underscore, and the digits 0-9

    Should not conflict with exiting variables or functions built into MATLAB

    It is possible to reassign Inf for example but it may become confusing

    Often i or j are reassigned as loop variables when a program does not deal with complex numbers

    MATLAB is case sensitive

    Assign using the equals operator (a = 2, a = 2 * b, a = sin(pi))

    Reassignment in terms of themselves (a = 3 * a - 7, NOT the same as 2a = 7)

    Cannot be defined as a MATLAB keyword such as (for, if, else, break, etc.)

    If defined as an existing MATLAB function, function cannot be used

    ans variable of the last expression calculated not assigned to a variable

  • Chapter 1 Slide 11ME280 - Structured Programming

    Useful Management Commands

    Commands

    who - provides a list of variable names in the current workspace

    whos provides a list of variables currently in memory and information about their size and type

    clear - deletes all the variables stored in memory and shown in the current workspace

    clear VarName1 VarName2 etc. removes only the listed variables from memory

    clc clears the command window including those of previous sessions

    ans - returns the value of the last expression calculated that was not assigned to a variable

    Should avoid relying on the ans variable

  • Chapter 1 Slide 12ME280 - Structured Programming

    Script Files Introduction

    Command Window

    Executes a single command at a time

    Cannot be saved and executed again

    Not interactive

    Changing the value of a variable in equations previously entered requires reentering all equations affected by the variable change

    Script Files

    File with a list of commands that are executed sequentially in the same manner as the command window

    Can be edited and saved as an M-file (*.m) for later use

    Any text editor such as Notepad may be used to write a script file

    MATLABs Editor Window assist with code development by issuing warnings, providing autocomplete, and color coded text

  • Chapter 1 Slide 13ME280 - Structured Programming

    Script Files Use

    Creating

    Use the MATLAB Editor Window since MATLAB is available to all students and the Editor Window permits the execution of the script file from within and debugging tools

    From MATLAB open the editor window by one of the following:

    File New Script menu New script File button Ctrl + N

    Saving

    File name requirements are the same as variable names

    From Editor Window save your M-file by one of the following:

    File Save as menu

    Save File button

    Ctrl + S

  • Chapter 1 Slide 14ME280 - Structured Programming

    Script Files Use

    Opening

    From MATLAB Desktop or Editor Window

    File Open menu

    Open File button

    Ctrl + O

    From the Current Folder Window

    Select the appropriate directory for the M-file

    Double click the file Right click the file and select open Select the file and use Ctrl+D

  • Chapter 1 Slide 15ME280 - Structured Programming

    Script Files Use

    Running

    From the MATLAB Editor Window do one of the following:

    Debug Run ScriptFileName menu

    Run button

    F5

    From MATLAB Current Folder Window do one of the following:

    Right click the appropriate M-file and select Run

    Select the desired M-file and press F9

    From MATLAB Command Window

    Change to the desired M-files directory if necessary

    cd C:\FilePath (useful for programs executing subprograms in different directories) Browse in MATLAB Desktop

    Type the name of the M-file to run

  • Chapter 1 Slide 16ME280 - Structured Programming

    Examples

    Problem 1

    Problem 2

    Define the variables a and b as: a=14, b= 21 then evaluate:

    Problem 3

    Define the variables x as x =12 degrees and verify the identity is correct:

    3 5

    3

    4 14 30ln50

    5 30

    456!

    ln 90

    e

    10

    sin coslog

    ba ba a e a

    a b b

    31

    sin 3sin sin 34

    x x x

  • Chapter 1 Slide 17ME280 - Structured Programming

    Examples

    Problem 4

    The prices of an oak tree and a pine tree are $54.95 and $39.95, respectively. Change the display format to bank, and calculate the following:

    (a) the total cost of 16 oak trees and 20 pine trees.

    (b) the same as part (a), and add 6.25% sale tax.

    (c) the same as part (b) and round the total cost to the nearest dollar.


Recommended