What is MATLAB - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

What is MATLAB

Description:

Originally, it was a front-end to FORTRAN matrix routines developed in the ... E.g. A(2,3) refers to entry in second row and third column. ... – PowerPoint PPT presentation

Number of Views:214
Avg rating:3.0/5.0
Slides: 23
Provided by: amatthe
Category:
Tags: matlab

less

Transcript and Presenter's Notes

Title: What is MATLAB


1
What is MATLAB ?
  • MATrix LABratory
  • Originally, it was a front-end to FORTRAN matrix
    routines developed in the 1970s _at_ U. of New
    Mexico and Stanford
  • Today it is a powerful, interactive mathematics
    ENVIRONMENT with many powerful capabilities
    (matrices, symbolic math, analysis)
  • Not unlike a UNIX shell

2
Support Materials
  • MATLAB is available _at_ the ECE front desk, and at
    the ODU bookstore
  • The only way to master MATLAB is to use it (just
    like any programming language or skill)
  • Users Guide (comes with student edition)
  • Internet FAQs (e.g. www.mathworks.com)
  • MATLAB Primer (Bound copy 3.00)

3
Accessing Matlab
  • Start Menu.. Programs.. Matlab
  • To exit..
  • gtgtquit

4
Entering Matrices
  • MATLAB works with essentially one kind of object
    a rectangular numerical MATRIX with possibly
    complex entries
  • 1 x 1 interpreted as scalars
  • 1 x n or m x 1 interpreted as vectors
  • Entered by explicit list of elements, or
  • Generated by built-in statements and functions
  • Created in M-files
  • Loaded from external data files

5
Entering matrices (contd.)
  • Example A 1,2,3 4,5,6 7,8,9 or
  • A
  • 1 2 3
  • 4 5 6
  • 7 8 9 creates a 3 x 3 matrix and assigns it to
    a variable A.
  • , or blank separates the element in a matrix
  • Avoid blank spaces while listing a number in
    exponential form (e.g. 2.34e-9)
  • Large Matrix best done in M file (easy to edit)
  • Built in functions rand, magic, hilb

6
Entering matrices (contd.)
  • rand (n) creates a n x n matrix with random
    entries uniformly distributed between 0 and 1
  • rand (m x n) will create an m x n matrix
  • magic (n) will create a an integral n x n matrix
    which is a magic square
  • hilb(n) will create the n x n Hilbert matrix
  • Individual matrix and vector entries can be
    referenced with indices (only positive integers)
    within the parentheses
  • E.g. A(2,3) refers to entry in second row and
    third column.
  • X(3) woild denote third coordinate of a vector x.

7
Matrix Operations
  • Addition
  • Substraction -
  • Multiplication x
  • Power
  • Transpose
  • Left Division \
  • Right division /
  • E.g. x A\b is the solution of A x b
  • x b/A is the solution of x A b

8
Array Operations
  • Addition substraction Operate entrywise
  • Other can be made entrywise by preceding them
    with a period for ,,\,/
  • E. g. 1 2 3 4 .1 2 3 4 will yield 1 4 9
    16
  • 1 2 3 4.2 will yield 1 4 9 16
  • Useful in MATLAB graphics

9
Statements, Expressions Variables
  • MATLAB is an expression language CASE SENSITIVE
  • Statements are of the form
  • Variable expression, or simply
  • Expression
  • Expressions are composed from operators,
    functions , and variable names.
  • Result is a Matrix assigned to the variable for
    future use.
  • If variable name and sign are omitted, then a
    variable ans (for answer) is created.
  • Statement terminated with a CR, use to continue
    to next line
  • Same line use comma to separate statements
  • Last character semicolon suppresses the printing
  • Who lists all the variables
  • Clear clears the variables
  • Runaway Display can be stopped by CTRL-C

10
Matrix Building Functions
  • Convenient Matrix Building Functions are
  • Eye
  • Zeros
  • Ones
  • Diag
  • Triu
  • Tril
  • Rand
  • Hilb
  • Magic
  • Toeplitz

11
For,While, if and relations
  • MATLAB flow control statements operate like those
    in most computer languages
  • For
  • x for i 14, x x,i2,end
  • x for i 4-11, x x,i2,end
  • While
  • While relation
  • Statements
  • End
  • If
  • If relation
  • Statements
  • end

12
Relations
  • lt less than
  • gt greater than
  • lt less than or equal
  • gt greater than or equal
  • equal
  • not equal
  • and
  • or
  • not

13
Scalar Vector functions
  • Scalar
  • Sin asin exp abs round
  • Cos acos log sqrt floor
  • Tan atan rem sign ceil
  • Vector
  • Max sum median any
  • Min prod mean all
  • Sort std

14
Matrix Functions
  • Eig chol svd inv lu qr
  • Hess schur rref expm sqrtm poly
  • Det size norm cond rank

15
Command Line Editing Recall
  • Use left right arrows
  • Backspace delete keys
  • Home, end, Delete
  • Up/Down arrow keys

16
Submatrices Colon Notation
  • To achieve fairly complex data manipulation
  • Colon Notation (generate vectors and reference
    submatrices
  • Expression 15 generates 1 2 3 4 5
  • Expressions 0.20.21.2 generates 0.2 0.4 0.6
    0.8 1.0 1.2
  • Expression 5-11 gives 5 4 3 2 1
  • X 0.00.12.0ysin(x)x,y gives a table of
    sines
  • Colon Notation used to access submatrices of a
    matix
  • A(14,3), A(,3), A(14,) , A(, 2,4)
  • A(2,4,5) B(,13)
  • A(2,4) A(,2,4)1,23,4

17
M files
  • To execute a sequence of statements
  • Script files
  • Sequence of normal MATLAB statements
  • Variables are global
  • Used to enter data into a large matrix
  • Entry errors can be easily edited
  • Function files
  • Provide extensibility to MATLAB
  • Create new functions specific to a problem
  • Variables are local
  • We can however declare them global if so desired.

18
Function files
  • Example
  • function a randint(m,n)
  • a floor (10 rand(m,n)
  • Place in a file called randint.m
  • first line declares function name,input
    arguments and output arguments
  • without this line the file would be a script
    file
  • A statement z randint(4,5) will pass 4,5 to m,n
    in the function file with the output result
    passed to variable z.

19
Function file (contd.)
  • Example 2
  • Function mean, stdev stat (x)
  • m,n size(x)
  • If m 1
  • M n
  • End
  • Mean sum(x)/m
  • Stdev sqrt (sum(x.2)/m mean.2)
  • to write comment statements

20
Text Strings, error messages, inputHardcopy
  • Text Strings use single quotes
  • Use disp to display text strings
  • Use error to display error messages
  • Use input to interactively input data
  • Use diary to get a hardcopy
  • To turn off use diary off

21
Graphics
  • Use plot to create linear x,y plots
  • x -40.14 y sin(x) plot (x,y)
  • x -1.50.011.5 y exp(-x.2) plot (x,y)
  • t 0.0012pixcos(3t)ysin(2t),plot(x,y)
  • Use shg to see the graphics screen
  • Labelling
  • Title xlabel ylabel gtext text axis
  • Default is auto scaling
  • Multiple plots
  • Hold
  • Linetypes and pointtypes

22
Graphics (contd.)
  • 3-D mesh plots
  • Use function mesh
  • 3-D perspective of elements of matrix z
  • Mesh (eye(10))
  • xx -2.12yyxxx,y meshdom(xx,yy)z
    exp(-x.2 - -y.2)mesh(z)
Write a Comment
User Comments (0)
About PowerShow.com