MATLAB Basics - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

MATLAB Basics

Description:

every command generates a result which is shown in the Command Window after the instruction ... the workspace can be emptied by issuing the clear command ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 21
Provided by: Walt90
Category:
Tags: matlab | basics | emptied

less

Transcript and Presenter's Notes

Title: MATLAB Basics


1
MATLAB Basics
2
  • Workspace
  • variable list
  • includes size and type

Command window
Command history
3
  • instructions or commands are typed directly into
    the Command Window
  • every command generates a result which is shown
    in the Command Window after the instruction
  • a value of 3 has been assigned to variable a
  • numeric variables are stored in arrays
  • Workspace Window indicates that the variable a is
    a 1x1 matrix holding a double precision value
    which requires 8 bytes of storage.
  • note that the assignment command was recorded in
    the Command History Window

4
  • each command is executed as it is entered
  • value of 4 is assigned to variable b
  • sum of the values assigned to a and b is assigned
    to variable c and result is immediately displayed
  • if the result of a command is not assigned to a
    named variable, then the result is assigned to
    the default variable ans
  • workspace now contains 4 variables
  • line spacing in the Command Window can be reduced
    by setting the format to compact
  • the Command Window will scroll automatically or
    it can be completely cleared by issuing the clc
    command

5
  • the workspace can be emptied by issuing the clear
    command

6
  • these commands produce the calculated volume of a
    cylindrical tank, d 3 ft, h 4 ft
  • note that the value for pi is predefined
  • the resolution of calculated results can be
    changed by using the commands format long and
    format short
  • note that variable names are case sensitive,
    i.e., a and A are two different variables that
    stored separately in the workspace

7
  • arrays or matrices are entered row by row with
    the column separated by spaces and the rows
    separated by semicolons.
  • rows may be split between multiple command lines
  • standard linear algebra operations are applied
    directly to the matrix variables
  • individual array elements are selected by placing
    the row and column indices in parentheses
  • the selected element may be displayed or changed

8
  • the colon operator is used to generate
    sequences of numbers
  • syntax is firstincrementlast
  • increment may be positive or negative so the
    sequence can be increasing or decreasing

9
  • transpose of a matrix is generated by placing an
    apostrophe after the matrix name or expression
  • transpose of a row vector is a column vector

10
  • Linear equation set in matrix format
  • simple method for solving simultaneous linear
    equation set is to use the left-divide or
    backslash operator
  • c a\b is functionally equivalent to c
    inverse(a)b
  • actual method used for the solution is based on
    Gauss elimination rather a matrix inverse

11
  • plots are generated from arrays of the coordinate
    data
  • the plot appears in a separate Plot Window

12
  • plot of cos(x) for 11 equally spaced values of x
    between -pi and pi
  • math functions are applied to matrices on an
    element by element basis
  • resulting plot needs more data points to generate
    a smooth curve

13
  • generate 201 equally spaced value of x between
    pi and pi
  • to avoid generating a listing of the coordinate
    data, suppress the output from an individual
    command by placing a semicolon at the end

14
  • rather than repeatedly typing commands, it would
    be easier to define a script for a sequence of
    commands.
  • matlab stores command scripts in m-files
  • first, pick a storage location for your files
  • click on the Current Directory tab
  • click on the small white rectangle near the path
    name to browse the directory structure
  • create a directory to hold all matlab work

15
  • select File/New/M-file
  • a separate Matlab edit window is opened for the
    creation of the matlab script
  • enter the commands necessary to calculate the
    cylindrical tank volume
  • save the file as volume.m

16
Tank Filling Time Equations
17
  • Create the program with separate sections for the
    Constants and Calculations
  • Note that the lines are ended with a semicolon
    () which suppresses the execution output for
    that line
  • A program line must be terminated with a
    semicolon before tabbing over for the comment
  • The percent () character marks the beginning of
    a comment
  • Read the description of the disp function in
    the matlab help
  • Save the m-file in the Current Directory as
    tank.m

18
  • Execute the program by typing the file name in
    the command window.

19
  • Add an Input section to the program to allow the
    user to specify the velocity at each inlet
  • Delete the inlet velocity initializations that
    were in the Constants section
  • Read about the input function in the matlab
    help facility
  • Save the changes

20
  • Clear the Workspace
  • Run the program
  • Use the same inlet velocities to check that the
    same result is obtained
Write a Comment
User Comments (0)
About PowerShow.com