MATLAB Tutorial - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

MATLAB Tutorial

Description:

MATLAB Tutorial For EE321 By Aaron Lewicke Contents Background Introduction Workspace Math Functions M-Files Examples Introduction MATLAB works in arrays and matrices. – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 19
Provided by: AaronL64
Category:

less

Transcript and Presenter's Notes

Title: MATLAB Tutorial


1
MATLAB Tutorial
  • For EE321
  • By Aaron Lewicke

2
Contents
  • Background
  • Introduction
  • Workspace
  • Math
  • Functions
  • M-Files
  • Examples

3
Introduction
  • MATLAB works in arrays and matrices. This option
    offers a quick way to manipulate large sets of
    data.
  • The programming layout is similar to other types
    of programming languages
  • MATLAB also offers graphical user interface tools
    to help develop applications

4
Workspace
5
  • As you work in the command window, MATLAB
    remembers the commands you have typed. These
    commands are said to make up the workspace. To
    check what the current variables type
  • gtgt who
  • This will bring up a list of variable names.
    For a detailed list of variables, including size
    type, and class, type
  • gtgt whos
  • Other useful commands are
  • gtgt clc
  • (clears command window)
  • gtgt clear
  • (clears variables and functions in workspace)

6
Variables
  • Variables in MATLAB have rules just like any
    other language.
  • Variables are case sensitive.
  • Names of variables can contain up to 31
    characters. Any characters beyond 31 are
    ignored.
  • Variable names must start with a letter, then can
    be followed by letters, numbers, or underscores.
    Punctuation marks are not allowed since they may
    have a special meaning.
  • There are also reserved names for special
    variables. These variables are
  • ans, pi, eps, flops, inf, NaN, nan, i, j,
    nargin, nargout, realmin, realmax
  • To illustrate the meanings of some of these
    reserved variables we will use another useful
    command, the help command. MATLAB has an
    online help library built into the program. To
    use, simply type
  • gtgt help function or special variable name goes
    here

7
help example
  • gtgt help i
  • I Imaginary unit.
  • As the basic imaginary unit SQRT(-1), i and j
    are used to enter
  • complex numbers. For example, the
    expressions 32i, 32i, 32j,
  • 32j and 32sqrt(-1) all have the same
    value.
  • Since both i and j are functions, they can be
    overridden and used
  • as a variable. This permits you to use i or
    j as an index in FOR
  • loops, etc.
  • See also J.

8
  • Note that at the end of each help section MATLAB
    gives related variables or functions for you to
    look up. This is useful if you dont find
    exactly what you are looking for on the first
    attempt.
  • MATLAB, like other languages, gives the user the
    ability to add comments to document their work.
    When the sign is added, MATLAB will regard the
    rest of the line as a comment.
  • gtgt c1 1-2i the appended I signifies the
    imaginary part
  • When something is a comment it is green type.
  • Another punctuation mark that is used is the
    semi-colon
  • This mark will tell MATLAB not to display the
    output of a particular line of code.
  • gtgt c1 1-2i
  • gtgt c1 1-2i
  • c1
  • 1.0000 - 2.0000i

9
  • To continue a statement onto another line use
  • gtgt cost 10
  • gtgt items 5
  • gtgt average_cost cost/...
  • items
  • average_cost
  • 2
  • You must not separate a statement in the middle
    of the variable name. MATLAB will view it as two
    different variables. Also, you cannot separate a
    comment to another line.

10
Math
  • MATLAB can do simple math just as a calculator.

OPERATION SYMBOL EXAMPLE
Addition, a b 3 22
Subtraction, a b - 90-44
Multiplication, ab 3.144.20
Division, a b / or \ 56/8 8\56
Exponentiation, ab 216
11
Vectors and Matrices
  • To create a vector or matrix in MATLAB use the
    brackets.
  • gtgt a 1 2 3 4
  • a
  • 1 2 3 4
  • gtgt a 1,2,3,4
  • a
  • 1 2 3 4
  • gtgt a 1 2 3 4
  • a
  • 1
  • 2
  • 3
  • 4
  • gtgt a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
    16
  • a
  • 1 2 3 4
  • 5 6 7 8
  • 9 10 11 12
  • 13 14 15 16

12
  • To separate the elements in a row use a space or
    comma.
  • To separate rows use a semi-colon.
  • To address and element of the array/vector put
    the coordinates in parentheses.
  • gtgt a(2,3)
  • ans
  • 7

13
  • Some math functions useful for matrices

OPERATION SYMBOL EXAMPLE
Division, element by element or dot-division ./ or .\ a.\bb./a
Dot-transpose . a.
Transpose a
Empty numeric array
Exponentiation, element by element . a.2
Exponentiation, matrix a2
Multiplication, element by element or dot-product . a.b
14
Functions
  • MATLAB has many built-in functions.
  • Some math functions are
  • acos, acosh acot, acsc, acsch, asec, asech,
    asin, asinh, atan, atan2, atanh, cos, cosh, cot,
    coth, csc, csch, sec, sech, sin, sinh, tan, tanh,
    exp, log, log10, log2, pow2, sqrt, nextpow2, abs,
    angle, conj, imag, real, unwrap, isreal,
    cplxpair, fix, floor, ceil, round, mod, rem,
    sign, cart2sph, cart2pol, pol2cart, sph2cart,
    factor, isprime, primes, gcd, lcm, rat, rats,
    perms, nchoosek, airy, besselj, bessely, besselh,
    besseli, besselk, beta, betainc, betaln, ellipj,
    ellipke, erf, erfc, erfcx, erfinv, expint, gamma,
    gammainc, gammaln, legendre, cross, dot
  • To find out more on how these functions are used,
    try out the help command.
  • There are many other functions associated with
    other toolboxes in MATLAB. The hand-out lists
    all of the built-in signal processing functions.
  • The user also has the ability to write their own
    functions. This will be described in the next
    section regarding m-files.

15
M-files
  • To create a m-file on a PC, choose New from the
    File menu and select M-file.
  • All of the same properties of the workspace carry
    over to the script m-file.
  • The m-files can be run in the MATLAB workspace by
    typing the name of the file. Note, you do not
    need to append the .m
  • To edit an existing m-file, select Open from the
    File menu, or type
  • gtgt edit name of m-file
  • MATLAB has provided several functions that are
    useful in m-files.

FUNCTION DESCRIPTION
disp(variable) Display results without identifying variable names.
echo Control the Command window echoing of script file commands.
input Prompt user for input.
keyboard Give control to keyboard temporarily. Type return to return control to the executing script m-file.
pause, pause(n) Pause until user presses any keyboard key, pause for n seconds.
waitforbuttonpress Pause until user presses mouse button or keyboard key.
16
Sample m-file
  • script m-file example1.m
  • erasers 4 Number of each item
  • pads 6
  • tape input(Enter the number of rolls of tape
    purchased gt )
  • items erasers pads tape
  • cost erasers25 pads52 tape99
  • average_cost cost/items

17
Sample of a user-written function
  • function y ewe(A,Q,a,b,c)
  • compute the U matrix for a six degree of freedom
    robot
  • where a, b, c are the subscript values for U
    respectively
  • if c is not used, enter zero
  • x eye(4)
  • for j 1a
  • if b j
  • x xQA(,,j)
  • elseif c j
  • x xQA(,,j)
  • elseif (bc) j
  • x xA(,,j)
  • end
  • end
  • y x

18
Sources
  • Mastering MATLAB5, A comprehensive Tutorial and
    Reference. Hanselman and Littlefield. Prentice
    Hall 1998
  • Digital Signal Processing, A Computer-Based
    Approach. Mitra. McGraw-Hill Irwin 2001
  • http//www.mathworks.com/support/
Write a Comment
User Comments (0)
About PowerShow.com