(i) Direct entry of matrices: A =

Similar documents
Similar matrices and Jordan form

Linear Algebra Review. Vectors

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

Eigenvalues, Eigenvectors, Matrix Factoring, and Principal Components

Lecture 5: Singular Value Decomposition SVD (1)

Linear Algebra and TI 89

Review Jeopardy. Blue vs. Orange. Review Jeopardy

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial VI

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

1 Introduction to Matrices

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

Data Mining: Algorithms and Applications Matrix Math Review

[1] Diagonal factorization

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Matrices 2. Solving Square Systems of Linear Equations; Inverse Matrices

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

Introduction to Matrix Algebra

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Applied Linear Algebra I Review page 1

x = + x 2 + x

Beginner s Matlab Tutorial

MATHEMATICS FOR ENGINEERS BASIC MATRIX THEORY TUTORIAL 2

LINEAR ALGEBRA. September 23, 2010

Orthogonal Diagonalization of Symmetric Matrices

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Lecture 2 Matrix Operations

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

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

SF2940: Probability theory Lecture 8: Multivariate Normal Distribution

Eigenvalues and Eigenvectors

Brief Introduction to Vectors and Matrices

Introduction to Matrices for Engineers

MATH APPLIED MATRIX THEORY

Matrix Algebra in R A Minimal Introduction

Introduction to Matlab

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

Linear Algebra Notes

Matrix Calculations: Applications of Eigenvalues and Eigenvectors; Inner Products

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

Chapter 19. General Matrices. An n m matrix is an array. a 11 a 12 a 1m a 21 a 22 a 2m A = a n1 a n2 a nm. The matrix A has n row vectors

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

Chapter 6. Orthogonality

Similarity and Diagonalization. Similar Matrices

SF2940: Probability theory Lecture 8: Multivariate Normal Distribution

Notes on Determinant

Schneps, Leila; Colmez, Coralie. Math on Trial : How Numbers Get Used and Abused in the Courtroom. New York, NY, USA: Basic Books, p i.

LS.6 Solution Matrices

Financial Econometrics MFE MATLAB Introduction. Kevin Sheppard University of Oxford

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

Chapter 17. Orthogonal Matrices and Symmetries of Space

Linear Algebra: Determinants, Inverses, Rank

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

7.4. The Inverse of a Matrix. Introduction. Prerequisites. Learning Style. Learning Outcomes

Orthogonal Projections

Solving simultaneous equations using the inverse matrix

Typical Linear Equation Set and Corresponding Matrices

( ) which must be a vector

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

MAT 242 Test 2 SOLUTIONS, FORM T

8 Square matrices continued: Determinants

Math 312 Homework 1 Solutions

Notes on Orthogonal and Symmetric Matrices MENU, Winter 2013

Solving Linear Systems, Continued and The Inverse of a Matrix

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

160 CHAPTER 4. VECTOR SPACES

DATA ANALYSIS II. Matrix Algorithms

Linear Algebraic Equations, SVD, and the Pseudo-Inverse

Excel supplement: Chapter 7 Matrix and vector algebra

1 Determinants and the Solvability of Linear Systems

The Image Deblurring Problem

Continued Fractions and the Euclidean Algorithm

Fractions to decimals

The Singular Value Decomposition in Symmetric (Löwdin) Orthogonalization and Data Compression

Manifold Learning Examples PCA, LLE and ISOMAP

MAT188H1S Lec0101 Burbulla

University of Lille I PC first year list of exercises n 7. Review

Matrix Differentiation

Recall that two vectors in are perpendicular or orthogonal provided that their dot

Solving Systems of Linear Equations Using Matrices

8.2. Solution by Inverse Matrix Method. Introduction. Prerequisites. Learning Outcomes

6. Cholesky factorization

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

Math 115A HW4 Solutions University of California, Los Angeles. 5 2i 6 + 4i. (5 2i)7i (6 + 4i)( 3 + i) = 35i + 14 ( 22 6i) = i.

Introduction. Chapter 1

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

PGR Computing Programming Skills

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

Quadratic forms Cochran s theorem, degrees of freedom, and all that

CHAPTER 8 FACTOR EXTRACTION BY MATRIX FACTORING TECHNIQUES. From Exploratory Factor Analysis Ledyard R Tucker and Robert C.

NOTES ON LINEAR TRANSFORMATIONS

The Characteristic Polynomial

CS3220 Lecture Notes: QR factorization and orthogonal transformations

Math 2524: Activity 3 (Excel and Matrices, continued) Fall 2002

Vector and Matrix Norms

Normal distributions in SPSS

Mehtap Ergüven Abstract of Ph.D. Dissertation for the degree of PhD of Engineering in Informatics

Solution to Homework 2

A =

Bindel, Spring 2012 Intro to Scientific Computing (CS 3220) Week 3: Wednesday, Feb 8

Alabama Department of Postsecondary Education

Transcription:

Alastair Hall ECG 790: Topics in Advanced Econometrics Fall 2004 MATLAB Handout # 1 Basic linear algebra commands MATLAB is a matrix programming language. Many of the commands are similar to PROC IML in SAS but it provides a much more flexible programming language than SAS. There are also a number of toolboxes in MATLAB which contain certain canned procedures. For example, there is a Numerical Optimization Toolbox which allows us to optimize any specified function; there is also a Statistics Toolbox which contains many useful statistical procedures such as pdf or cdf calculations. We use commands from both these toolboxes in the GMM programs discussed in class. MATLAB programs can either be executed interactively or by running an M file. An M file is a file containing MATLAB code. The name arises because the file must have a.m extension. In this handout, we consider both approaches. We begin by executing commands interactively. All MATLAB commands below are given in bold. (i) Direct entry of matrices: Supposewewishtoenterthematrix: A = [ 3 4 ] This can be done as follows: A=[12;34]; To exectute the statement it is necessary to hit enter key. Notice that there is no output. To see the contents of A. Type: A Alternatively, if we omit the semi colon from a command then its output is printed to the screen at the time of execution. To illustrate use the key to retrieve the command which entered A and then edit out the ; and re-execute the command. (ii) Editing the elements of a matrix: Supposewewishtoreplacethe(1, 2) element of A by 6. This can be done as follows. 1

A(1,2)=6; Sometimes it is desired to replace either and entire row or column of a matrix this can be done as follows. A(1,:)=[2 1]; A(:,1)=[7; 7]; (iii) Dimensions and rank: The dimensions of a matrix can be calculated as follows. [nr,nc]=size(a); This command creates a row vector whose first element nr (you pick the name) is the number of rows of A and whose second element nc is the number of columns of A. Therankofamatrixcanbecalculatedusingthefollowingcommand. r=rank(a); Note that this calculation is based on the singular value decomposition and so involves an implicit decision about rounding off. (iv) Useful functions of a matrix: The transpose of A is calculated by: B=A ; The determinant is calculated by: d=det(a); The inverse is calculated by: C=inv(A); The trace is calculated by: t=trace(a); (v) Eigenvalues and eigenvectors: The eigenvalues of A are calculated using the command: lambda=eig(a); 2

The matrices of eigenvectors, V say, and a diagonal matrix containing the eigenvalues, L say, can be calculated by: [V,L]=eig(A); (v) Particular matrices: MATLAB has certain functions which return matrices of a particular form. Three are worth noting here chosen dimensions are just used for illustration. The 4 4 identity matrix: eye(4) A10 5 matrix of ones can be calculated by: ones(10,4) A6 2 matrix of zeros can be calculated by: zeros(6,2) A 3 2 matrix of normal random variables: randn(3,2) Note that the elements of the last matrix are random draws from a standard normal distribution. It is possible to initialize the seed for these random draws as follows: randn( seed,1); randn(3,2); Note that this initialization only applies if the two commands are executed sequentially. The seed can be initialized as any positive number, in this case we used 1. (vi) Operations: addition: + subtraction: - matrix multiplication: * array multiplication:.* matrix power: ^ 3

array power:.^ array division:./ Kronceker product: kron(a,b) Use these operators to calculate: A+B, A B, A B, A. B, A 2, C = {a 2 }, D = {b ij ij/a ij }, A B, B A for: [ ] [ ] 1 1 A = B = 3 4 1 1 We now consider how to create, edit and execute M files. There are two types of M files: script and function files. A script file contains a set of self contained commands. A function file contains a set of commands which involve some matrix which must be provided. (vii) Creating script M files: An M file can be created using the MATLAB Editor. Todothis,clickonFile at the top of the MATLAB Command window and then select New followed by M file. You will then be in the editor. Enter the commands: C=[5 4; 3 2]; dc=det(c) Now save the program into a file called test1.m on your k drive. (viii) Executing script M files: To execute this program type test1 on the command line in the MATLAB Command window and then hitting enter. At this point, MATLAB searches the PATH to find this file. The PATH contains a list of subdirectories which are recognized by MATLAB as containing M files. So unless we have done so already it is necessary to tell MATLAB to include k :inthepath.thiscanbe done as follows. Click on File at the top of the MATLAB Command window and then select SET PATH.ThenselectPATH followed by ADD PATH. This gives a window in which you can add the desired directory. It can seen that test1.m causes det(c) to be printed to the screen. After the file is executed, we can also print C to the screen by typing C and hitting enter. 4

(ix) Editing M-files: To edit an existing M-file then click on File at the top of the MATLAB Command window and then select Open. Or equivalently, you can click on the folder sign at the top of the MATLAB command window. Follow these steps and include the commands to calculate the commands to calculate the trace of C. Rerun the program. (x) Creating function M files: The basic creation process is the same as script M files but the structure is different. To illustrate, suppose we wish to create a function M file to calculate the determinant and trace of any square matrix. To this end, follow the steps in (vi) to open a new M file. Now type the following commands: function [detc, trc]=dettr(c); detc=det(c); trc=trace(c); Now save these commands in a file called dettr.m. Notice that the file name matches the function name on the first line of the program this is crucial. (xi) Executing function M-files: A function M-file has an argument, a square matrix C in this case. So this must exist before the file can be run. In our case, it already does and so we can execute the function M file by typing [detc,trc]=dettr(c) on the command line in the MATLAB command window and hitting enter. (xii) Executing DOS commands: It is also possible to execute DOS commands in MATLAB. To do this, the command line must be preceded by!. For example, to copy test1.m to test2.m, type the following at the prompt in the MATLAB command window:!copy k:\test1.m k:\ test2.m Then hit enter. Exercises: 1. Replace the first command in (i) by A = [ 1, 2; 3, 4];. What difference does this make? 2. Replace the first command in (i) by A=[12,34];. What difference does this make? 5

3. Replace the command in (iii) by size(a). What difference does it make? 4. Construct the matrix H = I 5 ι 5 (ι 5 ι 5) 1 ι 5 where ι 5 is a 5 1 column of ones. Recall from ECG 751 that this matrix is a projection matrix by construction. Verify this numerically by calculating: (a) H H ;(b)hh H. 5. Given that H is a projection matrix, what are its eigenvalues? Verify your answer numerically. 6. Given that H is a projection matrix what does its trace equal? Verify your answer numerically. 7. Compare the output from the following three sets of commands: (a) C=randn(3,2); D=randn(3,2); (b) randn( seed, 1); C=randn(3,2); D=randn(3,2); (c) randn( seed, 1);C=randn(3,2); randn( seed, 1); D=randn(3,2); 8. Compare the matrices C and D generated by the following commands: randn( seed, 1);C=randn(3,2); randn( seed, 10); D=randn(3,2);. 9. Create a matrix m whose elements are a random draw from a normal distribution with mean 5andvariance4. 10. Execute the command randn(size(a));. What is the output? 11. For A = [ 2 1 calculate the matrix F such that F AF equals a diagonal matrix. Verify that it works. 12. The symbol % at the beginning of a line means that it will be ignored bt MATLAB. Use this feature to put comments into the function M file created above which explains what it does. 13. Use dettr.m to calculate the determinant and trace of I 20. 14. Create a function M file which calculates the least squares solution to a set of linear equations, Ax = b. Run the program using: 1 A = 3 4 b = 2 5 6 7 What is the least squares solution for x? Verify numerically that the least squares error is in the left null space of A. ] 6