u = [ 2 4 5] has one row with three components (a 3 v = [2 4 5] has three rows separated by semicolons (a 3 w = 2:5 generates the row vector w = [ 2 3



Similar documents
AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

LINEAR ALGEBRA. September 23, 2010

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

Similar matrices and Jordan form

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Linear Algebra Review. Vectors

Introduction to Matlab

Eigenvalues and Eigenvectors

Using row reduction to calculate the inverse and the determinant of a square matrix

Curve Fitting, Loglog Plots, and Semilog Plots 1

[1] Diagonal factorization

Linear Algebra and TI 89

CS3220 Lecture Notes: QR factorization and orthogonal transformations

(!' ) "' # "*# "!(!' +,

Orthogonal Diagonalization of Symmetric Matrices

Data Mining: Algorithms and Applications Matrix Math Review

How long is the vector? >> length(x) >> d=size(x) % What are the entries in the matrix d?

by the matrix A results in a vector which is a reflection of the given

MATH APPLIED MATRIX THEORY

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Introduction to Matrix Algebra

13 MATH FACTS a = The elements of a vector have a graphical interpretation, which is particularly easy to see in two or three dimensions.

Notes on Determinant

MAT 200, Midterm Exam Solution. a. (5 points) Compute the determinant of the matrix A =

x = + x 2 + x

Similarity and Diagonalization. Similar Matrices

2+2 Just type and press enter and the answer comes up ans = 4

Eigenvalues, Eigenvectors, Matrix Factoring, and Principal Components

Applied Linear Algebra I Review page 1

1 Introduction to Matrices

How To Use Matlab

Beginning Matlab Exercises

Lecture 2 Matrix Operations

Beginner s Matlab Tutorial

5: Magnitude 6: Convert to Polar 7: Convert to Rectangular

Linear Equations ! $ & " % & " 11,750 12,750 13,750% MATHEMATICS LEARNING SERVICE Centre for Learning and Professional Development

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Solution to Homework 2

DATA ANALYSIS II. Matrix Algorithms

Analysis of System Performance IN2072 Chapter M Matlab Tutorial

Abstract: We describe the beautiful LU factorization of a square matrix (or how to write Gaussian elimination in terms of matrix multiplication).

Operation Count; Numerical Linear Algebra

Chapter 6. Orthogonality

Solving Linear Systems, Continued and The Inverse of a Matrix

Review Jeopardy. Blue vs. Orange. Review Jeopardy

Lecture 5: Singular Value Decomposition SVD (1)

Au = = = 3u. Aw = = = 2w. so the action of A on u and w is very easy to picture: it simply amounts to a stretching by 3 and 2, respectively.

Linear Algebra: Determinants, Inverses, Rank

1 VECTOR SPACES AND SUBSPACES

18.06 Problem Set 4 Solution Due Wednesday, 11 March 2009 at 4 pm in Total: 175 points.

Elementary Matrices and The LU Factorization

Solving Systems of Linear Equations

MATLAB Basics MATLAB numbers and numeric formats

Question 2: How do you solve a matrix equation using the matrix inverse?

Algebra 2 Chapter 1 Vocabulary. identity - A statement that equates two equivalent expressions.

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

8 Square matrices continued: Determinants

Lecture notes on linear algebra

Introduction to Matrices for Engineers

Lecture 1: Systems of Linear Equations

Linear Algebra Notes for Marsden and Tromba Vector Calculus

Notes on Orthogonal and Symmetric Matrices MENU, Winter 2013

7 Gaussian Elimination and LU Factorization

MAT 242 Test 2 SOLUTIONS, FORM T

Excel supplement: Chapter 7 Matrix and vector algebra

October 3rd, Linear Algebra & Properties of the Covariance Matrix

Lab 1. The Fourier Transform

Introduction to Matlab: Application to Electrical Engineering

SALEM COMMUNITY COLLEGE Carneys Point, New Jersey COURSE SYLLABUS COVER SHEET. Action Taken (Please Check One) New Course Initiated

MatLab Basics. Now, press return to see what Matlab has stored as your variable x. You should see:

Solving Systems of Linear Equations Using Matrices

Unit 7 Quadratic Relations of the Form y = ax 2 + bx + c

EL-9650/9600c/9450/9400 Handbook Vol. 1

MATH 423 Linear Algebra II Lecture 38: Generalized eigenvectors. Jordan canonical form (continued).

MATH2210 Notebook 1 Fall Semester 2016/ MATH2210 Notebook Solving Systems of Linear Equations... 3

CD-ROM Appendix E: Matlab

Vectors 2. The METRIC Project, Imperial College. Imperial College of Science Technology and Medicine, 1996.

9 MATRICES AND TRANSFORMATIONS

Lecture Notes 2: Matrices as Systems of Linear Equations

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

Examination paper for TMA4115 Matematikk 3

Brief Introduction to Vectors and Matrices

Lecture 2 Mathcad Basics

Excel Basics By Tom Peters & Laura Spielman

Some Lecture Notes and In-Class Examples for Pre-Calculus:

Content. Chapter 4 Functions Basic concepts on real functions 62. Credits 11

Math 312 Homework 1 Solutions

MATH 304 Linear Algebra Lecture 9: Subspaces of vector spaces (continued). Span. Spanning set.

Manifold Learning Examples PCA, LLE and ISOMAP

Solving Systems of Linear Equations

What are the place values to the left of the decimal point and their associated powers of ten?

The Projection Matrix

Vector and Matrix Norms

CAMI Education linked to CAPS: Mathematics

Grade 5 Common Core State Standard

SF2940: Probability theory Lecture 8: Multivariate Normal Distribution

MATH 304 Linear Algebra Lecture 18: Rank and nullity of a matrix.

Unit 18 Determinants

Transcription:

MATLAB Tutorial You need a small numb e r of basic commands to start using MATLAB. This short tutorial describes those fundamental commands. You need to create vectors and matrices, to change them, and to operate with them. Those are all short high-level commands, because MATLAB constantly works with matrices. I believe t h a t y ou will like t h e p o wer that this software gives, to do linear algebra by a series of short instructions: create E E = eye(3) create u u = E(: ) change E E(3 ) = 5 multiply E v = E 2 3 2 3 2 3 2 3 0 0 0 0 4 0 0 5 4 0 5 4 0 0 5 4 0 5 0 0 0 5 0 5 The word eye stands for the identity matrix. The submatrix u = E(: ) picks out column. The instruction E(3 ) = 5 resets the (3 ) entry to 5. The command E u multiplies the matrices E and u. All these commands are repeated in our list b e l o w. Here is an example of inverting a matrix and solving a linear system: 0 5 create A create b invert A solve Ax = b A = ones(3) + e y e(3) b = A(: 3) C = inv( A) x = Anb or x = C 2 3 2 3 2 3 2 3 b 2 :75 ;:25 ;:25 4 2 5 4 5 4 ;:25 :75 ;:25 5 4 0 2 2 ;:25 ;:25 :75 The matrix of all ones was added to eye(3), and b is its third column. Then inv(a) produces the inverse matrix (normally in decimals for fractions use format rat). The system Ax = b is solved by x = inv( A) b, which is the slow way. The backslash command x = Anb uses Gaussian elimination if A is square and never computes the inverse matrix. When the right side b equals the third column of A, the solution x must be [0 0 ] 0. (The transpose symbol 0 makes x a c olumn vector.) Then A x picks out the third column of A, and w e have Ax = b. Here are a few comments. The comment sym bol is % : % The symb o ls a and A are dierent: MATLAB is case-sensitive. % Type help slash for a description of how to use the backslash symbol. The word help can be followed by a MATLAB symb o l or command name or M-le name. u u

u = [ 2 4 5] has one row with three components (a 3 v = [2 4 5] has three rows separated by semicolons (a 3 w = 2:5 generates the row vector w = [ 2 3 4 5] with unit steps u = :2 :7 takes steps of 2 to give u = [ 3 5 7] Note: The command name is upper case in the description given by help, but must be lower case in actual use. And the backslash Anb is dierent when A is not square. % To display all 6 digits type format long. The normal format short gives 4 digits after the decimal. % A semicolon after a command avoids display o f t h e result. A = ones(3) will not display the 3 3 identity matrix. % Use the up-arrow cursor to return to previous commands. How to input a row or column vector 0 v = [2 4 5] 0 or v = u transposes u to produce the same v How to input a matrix (a row at a time) A = [ 2 3 4 5 6] has two rows (always a semicolon between rows) type 6] A = [ 2 3 also produces the matrix A but is harder to 4 5 B = [ 2 3 4 5 6] 0 is the transpose of A. Thus A T is A 0 in MATLAB How to create special matrices diag(v) produces the diagonal matrix with vector v on its diagonal toeplitz(v) gives the symmetric constant-diagonal matrix with v as rst row and rst column toeplitz(w v ) gives the constant-diagonal matrix with w as rst column and v as rst row ones(n) gives an n n matrix of ones 2

zeros(n) gives an n n matrix of zeros eye(n) gives the n n identity matrix rand(n) gives an n n matrix with random entries between 0 and (uniform distribution) randn(n) gives an n n matrix with normally distributed entries (mean 0 and variance ) ones(m n) zeros(m n) rand(m n) give m n matrices ones(size(a)) zeros(size(a)) eye(size(a)) give matrices of the same shape as A How to change entries in a given matrix A A(3 2 ) = 7 resets the (3 2) entry to equal 7 A(3 :) = v resets the third row to equal v A(: 2) = w resets the second column to equal w The colon symb o l : stands for all (all columns or all rows) A([2 3] :) = A([3 2] :) exchanges rows 2 a n d 3 o f A How to create submatrices of an m n matrix A vector) vector) A(i j) returns the (i j) entry of the matrix A (scalar = A(i :) returns the ith row of A (as row A(: j ) returns the jth column of A (as column A(2 : 4 3 : 7 ) returns rows from 2 to 4 a n d columns from 3 to 7 (as 3 5 A([2 4] :) returns rows 2 and 4 a n d all columns (as 2 n A(:) returns one long column formed from the columns of A (mn triu(a) sets all entries b e l o w the main diagonal to zero (upper triangular) tril(a) sets all entries above the main diagonal to zero (lower triangular) Matrix multiplication and inversion A B gives the matrix product AB (if A can multiply B) A: B gives the entry-by-entry product (if size(a) = size(b)) inv( A) gives A ; if A is square and invertible pinv( A) gives the pseudoinverse of A AnB gives inv ( A) B if inv( A) exists: backslash is left division x = Anb gives the solution to Ax = b if inv( A) exists See help slash when A is a rectangular matrix! 3

Numbers and matrices associated with A det(a) is the determinant (if A is a square rank(a) is the rank (numb e r of pivots = dimension of row space and of column space) size(a) is the pair of numb ers [m n] trace(a) is the trace = sum of diagonal entries = sum of eigenvalues null(a) is a matrix whose n ; r columns are an orthogonal basis for the nullspace of A orth(a) is a matrix whose r columns are an orthogonal basis for the column space of A Examples E = eye(4) E(2 ) = ;3 creates a 4 4 elementary elimination matrix E A subtracts 3 times row o f A from row 2. B = [ A b] creates the augmented matrix with b as extra column E = eye(3) P = E([2 3] :) creates a p e r m utation matrix Note that triu(a) + tril(a) ; diag(diag(a)) equals A Built-in M-les for matrix factorizations (all important!) [L U P ] = lu(a) gives three matrices with P A = LU e = eig(a) i s a vector containing the eigenvalues of A [S E] = eig(a) g i v es a diagonal eigenvalue matrix E and eigenvector matrix S with AS = SE. If A is not diagonalizable (too few eigenvectors) then S is not invertible. [Q R] = qr(a) g iv es an m m orthogonal matrix Q and m n triangular R with A = QR Creating M-les M-les are text les ending with.m which M A TLAB uses for functions and scripts. A script is a sequence of commands which may b e executed often, and can b e placed in an m-le so the commands do not have to be retyped. MATLAB's demos are examples of these scripts. An example is the demo called house. Most of MATLAB's functions are actually m-les, and can b e viewed by writing type xxx where xxx is the name of the function. 4

5 To write your own scripts or functions, you have to create a new text le with any name you like, provided it ends with.m, so MATLAB will recognize it. Text les can b e created, edited and saved with any text editor, like emacs, EZ, or vi. A script le is simply a list of MATLAB commands. When the le name is typed at the MATLAB prompt, the contents of the le will be executed. For an m-le to be a function it must start with the word function followed by the output variables in brackets, the function name, and the input variables. Examples function [C]=mult(A) r=rank(a) C = A 0 A Save the above commands into a text le named mult.m Then this funtion will take a matrix A and return only the matrix product C. The variable r is not returned because it was not included as an output variable. The commands are followed by so that they will not b e printed to the MATLAB window every time they are executed. It is useful when dealing with large matrices. Here is another example: function [V,D,r]=properties(A) % This function nds the rank, eigenvalues and eigenvectors of A [m,n]=size(a) if m==n [V,D]=eig(A) r=rank(a) else disp('error: The matrix must be square') end Here the function takes the matrix A as input and only returns two matrices and the rank as output. The % is used as a comment. The function checks to see if the input matrix is square and then nds the rank, eigenvalues and eigenvectors of a matrix A. Typing properties(a) only returns the rst output, V, the matrix of eigenvectors. You must type [V,D,r]=properties(A) to get all three outputs.

6 Keeping a diary of your work The command diary('le') tells MATLAB to record everything done in the MATLAB window, and save the results in the text le named 'le'. Typing diary on or diary o toggles the recording. Old diary les can be viewed using a text editor, or printed using lpr in unix. In MATLAB, they can b e viewed using the type le command. Saving your variables and matrices The command diary saves the commands you typed as well as MATLAB's output, but it does not save the content of your variables and matrices. These variables can b e listed by the command whos which also lists the sizes of the matrices. The command save 'xxx' will save the matrices and all variables listed by the whos command into the le named xxx. MATLAB labels these les with a.mat extension instead of.m which are scripts or functions. xxx.mat les can b e read by MATLAB at a later time by typing load xxx. Graphics The simplest command is plot(x y) w hich uses two vectors x and y of the same length. The p o in ts (x i y i) will be plotted and connected by solid lines. If no vector x is given, MATLAB assumes that x(i) = i. Then plot(y) has equal spacing on the x-axis: the points are (i y(i)). The type and color of the line between p o i n ts can b e c hanged by a third argument. The default with no argument i s a solid black line \{". Use help plot for many options, we indicate only a few: MATLAB 5: plot(x y 0 r+ : 0 ) plots in r = red with + fo r p o in ts and dotted line MATLAB 4: plot(x y 0 ;; 0 ) i s a dashed line and plot(x y 0 0 ) i s a dotted line You can omit the lines and plot only the discrete p o i n ts in dierent ways: plot(x y 0 o 0 ) gives circles. Other options are 0 + 0 or 0 0 or 0 0 For two graphs on the same axes use plot(x y X Y ). Replace plot by loglog or semilogy or semilogx to change one or b o t h axes to logarithmic scale. The command

7 axis ([a b c d]) will scale the graph to lie in the rectangle a x b, c y d. To title the graph or label the x-axis or the y-axis, put the desired label in quotes as in these examples: title (`height of satellite') xlabel (`time in seconds') ylabel (`height in meters') The command hold keeps the current graph as you plot a new graph. Repeating hold will clear the screen. To print, or save the graphics window i n a l e, s e e help print or use print -Pprintername print -d lename