220 likes | 481 Vues
What is MATLAB ?. MATrix LABratory Originally, it was a front-end to FORTRAN matrix routines developed in the 1970’s @ U. of New Mexico and Stanford Today it is a powerful, interactive mathematics ENVIRONMENT with many powerful capabilities (matrices, symbolic math, analysis)
E N D
What is MATLAB ? • MATrix LABratory • Originally, it was a front-end to FORTRAN matrix routines developed in the 1970’s @ 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
Support Materials • MATLAB is available @ 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) • User’s Guide (comes with student edition) • Internet FAQ’s (e.g. www.mathworks.com) • MATLAB Primer (Bound copy ~$3.00)
Accessing Matlab • Start Menu.. Programs.. Matlab • To exit.. • >>quit
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
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
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.
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
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
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
Matrix Building Functions • Convenient Matrix Building Functions are • Eye • Zeros • Ones • Diag • Triu • Tril • Rand • Hilb • Magic • Toeplitz
For,While, if – and relations • MATLAB flow control statements operate like those in most computer languages • For • x =[]; for i = 1:4, x = [x,i^2],end • x =[]; for i = 4:-1:1, x = [x,i^2],end • While • While relation • Statements • End • If • If relation • Statements • end
Relations • < less than • > greater than • <= less than or equal • >= greater than or equal • == equal • ~= not equal • & and • | or • ~ not
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
Matrix Functions • Eig chol svd inv lu qr • Hess schur rref expm sqrtm poly • Det size norm cond rank
Command Line Editing & Recall • Use left & right arrows • Backspace & delete keys • Home, end, Delete • Up/Down arrow keys
Submatrices & Colon Notation • To achieve fairly complex data manipulation • Colon Notation (generate vectors and reference submatrices • Expression 1:5 generates [1 2 3 4 5] • Expressions 0.2:0.2:1.2 generates [0.2 0.4 0.6 0.8 1.0 1.2] • Expression 5:-1:1 gives [5 4 3 2 1] • X= [0.0:0.1:2.0]’;y=sin(x);[x,y] gives a table of sines • Colon Notation – used to access submatrices of a matix • A(1:4,3), A(:,3), A(1:4,:) , A(:, [2,4]) • A(:[2,4,5]) = B(:,1:3) • A(:[2,4]) = A(:,[2,4])*[1,2:3,4]
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.
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.
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
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
Graphics • Use plot to create linear x,y plots • x = -4:0.1:4; y = sin(x); plot (x,y) • x = -1.5:0.01:1.5; y = exp(-x.^2); plot (x,y) • t = 0:.001:2*pi;x=cos(3*t);y=sin(2*t),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
Graphics (contd.) • 3-D mesh plots • Use function mesh • 3-D perspective of elements of matrix z • Mesh (eye(10)) • xx = -2:.1:2;yy=xx;[x,y] = meshdom(xx,yy);z = exp(-x.^2 - -y.^2);mesh(z)