Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

MATLAB Tutorial

For EE321 By Aaron Lewicke

Contents
Background Introduction Workspace Math Functions M-Files Examples

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

Workspace

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 >> who This will bring up a list of variable names. For a detailed list of variables, including size type, and class, type >> whos Other useful commands are >> clc (clears command window) >> clear (clears variables and functions in workspace)

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

>> help function or special variable name goes here

help example
>> 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 3+2i, 3+2*i, 3+2j, 3+2*j and 3+2*sqrt(-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.

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. >> 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. >> c1 = 1-2i; >> c1 = 1-2i

c1 =

1.0000 - 2.0000i

To continue a statement onto another line use

>> cost = 10;


>> items = 5; >> average_cost = cost/... items

average_cost =

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.

Math
MATLAB can do simple math just as a calculator.
OPERATION Addition, a + b Subtraction, a b Multiplication, a*b Division, a b Exponentiation, ab SYMBOL + * / or \ ^ EXAMPLE 3 + 22 90-44 3.14*4.20 56/8 = 8\56 2^16

Vectors and Matrices


To create a vector or matrix in MATLAB use the [] brackets. >> a = [1 2 3 4] a= 1 2 3 4 >> a = [1,2,3,4] a= 1 2 3 4 >> a = [1; 2; 3; 4] a= 1 2 3 4 >> 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

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. >> a(2,3) ans =

Some math functions useful for matrices OPERATION SYMBOL EXAMPLE

Division, element by element or dotdivision


Dot-transpose Transpose Empty numeric array Exponentiation, element by element

./ or .\

a.\b=b./a

. [] .^

a. a [] a.^2

Exponentiation, matrix
Multiplication, element by element or dot-product

^
.*

a^2
a.*b

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.

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 >> edit name of m-file MATLAB has provided several functions that are useful in m-files. FUNCTION DESCRIPTION

disp(variable)
echo input keyboard pause, pause(n) waitforbuttonpress

Display results without identifying variable names.


Control the Command window echoing of script file commands. Prompt user for input. Give control to keyboard temporarily. Type return to return control to the executing script m-file. Pause until user presses any keyboard key, pause for n seconds. Pause until user presses mouse button or keyboard key.

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 > ); items = erasers + pads + tape cost = erasers*25 + pads*52 + tape*99 average_cost = cost/items

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 = 1:a if b == j x = x*Q*A(:,:,j); elseif c == j x = x*Q*A(:,:,j); elseif (b|c) ~ j; x = x*A(:,:,j); end end y = x;

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/

You might also like