# Intermediate Mathematics for Economists ECON3272

Intermediate Mathematics for Economists ECON3272

Computer Assignment 1. Matrix Algebra

In this course the open-source software R is used for numerical exercises. R includes a line editor but it is more convenient to use R with RStudio.

R is available in the computer labs and you may download it freely from the web onto your PC (http://www.r-project.org/).

For the installation of R and RStudio see:

Torfs, P. and Brauer, C. “A (very) short introduction to R”, 3 March 2014.

(http://cran.r-project.org/doc/contrib/Torfs+Brauer-Short-R-Intro.pdf)

RStudio splits the computer screen into four windows:

Editor window

• This window provides a text editor.

• Click Run or press CTRL+ENTER to send a line to the consol window where it is executed.

• You may edit and save the programs that you write in this window. Workspace/history window

• The workspace window shows the objects R has in its memory. You can edit their values by clicking on them.

• The history window provides a protocol of what has been typed.

Consol (command) window

• Commands are executed in this window.

• You may write directly into this window using the line editor. Plots/help/files/packages window

• R sends plots to this window.

• You may also use the help function, view files and install packages in this window.

A more detailed introduction to R can be found in:

Venables W.N. and Smith D.M. “An Introduction to R”, 16 April 2015.

(http://cran.r-project.org/doc/manuals/R-intro.pdf)

Data Input

Lines that start with # are comments that are ignored by R. Only copy the command lines without the comments. R is case sensitive; that is, x and X or gdp and Gdp indicate different objects.

# Start R.

# Input the vectors x and h.

x <- c(4, 3, 2, 7)

h <- c(5, -3, 1, 7, -6, 8, 1, 9, -6, -4, 0, 2)

# The assignment operator <-, which looks like an arrow, assigns

# the value to the right of it to the name on the left. The two

# vectors are now objects in the workspace of R.

# Rearrange the vector h into the 4×3 matrix A.

A <- array(h, dim=c(4,3))

# There are now three objects in the workspace. The following

# command shows the names of those objects.

objects()

# The output is

# [1] “A” “h” “x”

# Remove the vector h from the workspace.

rm(h)

objects()

# The output is

# [1] “A” “x”

# Show the two objects.

x; A

# The ouput is

# [1] 4 3 2 7

# [,1] [,2] [,3]

# [1,] 5 -6 -6

# [2,] -3 8 -4

# [3,] 1 1 0

# [4,] 7 9 2

# Note that matrix A is filled with numbers column by column. The

# elements of a vector are always listed in a row.

# Similarly, input the matrices B, C, D, E, F.

h <- c(3, 5, 5, -7, 4, -9, 8, 4, 0, 1, -7, 9)

B <- array(h, dim=c(3,4))

rm(h)

h <- c(1, 2, 4, -5, -7, 3, 8, 1, -6, 1, 1, 3)

C <- array(h, dim=c(3,4))

rm(h)

h <- c(7, -4, 2, 1, 9, -5, 0, 3, 6, 1, 4, 7, 0, 2, 1, -2)

D <- array(h, dim=c(4,4))

rm(h)

h <- c(3, 2, -1, 4, 5, -2, 7, -9, 6, 4, -2, 8, 0, 1, 9, 3)

E <- array(h, dim=c(4,4))

rm(h)

h <- c(-4, 6, -1, 3, 6, 2, 4, -8, -1, 4, 3, 0, 3, -8, 0, 7)

F <- array(h, dim=c(4,4))

rm(h)

# There are now six matrices and one vector in the workspace.

objects()

# Show all objects.

x; A; B; C; D; E; F

# Exercise 1

# Compute K=AB, L=BA, M=CD, N=DC, P=A(B+C), R=AB+AC. Display the

# results and provide comments where appropriate.

# For example, the R code for K and P is:

K <- A%*%B

K

P <- A%*%(B+C)

P

# Exercise 2

# Compute S=(AB)’-B’A’. The R function t produces the transpose of # a matrix. Comment.

S <- t(A%*%B)-t(B)%*%t(A)

S

# Exercise 3

# Find the determinants of matrices A, D and E. Call the

# determinants a, b, c. Comment where appropriate.

# Hint: One of these determinants does not exist. Also inspect the # columns of matrix E.

# Example:

a <- det(A)

a

# Exercise 4

# Compute the inverses of matrices A, D and E. Call them U, V, W. # Comment where appropriate.

# Hint: See the preceding question.

Example:

U <- solve(A)

U

# Exercise 5

# a) Compute the quadratic form x’Fx.

d <- t(x)%*%F%*%x

d

# The following code also works because R uses vectors in whatever

# way is multiplicatively coherent. Therefore, there is no need to

# transpose x; R does it automatically.

e <- x%*%F%*%x

e

# b) Compute the eigenvalues and eigenvectors of the symmetric

# matrix F.

ev <- eigen(F)

ev

# The eigenvalues are the row vector and the eigenvectors are the # columns of the matrix.

# c) Retrieve the matrix of eigenvectors.

Q <- ev$vec

# Compute J = Q’Q. Comment.

J = t(Q)%*%Q

J

# Hint: See the matrix whose columns are eigenvectors in the

# course handbook.

# d) What is the definiteness of the quadratic form x’Fx and

# matrix F?

# Exercise 6

objects()

# Remove all objects from the workspace.

rm(list=ls(all=TRUE))

# Check whether the workspace is now empty.

objects()

# a) Consider the linear equation system given in Bretscher

# (2009), Exercises 1.2, 17.

#

# Using matrix notations, the equation system is:

# Input the vector b and matrix A and display them. How to do this # is shown at the beginning of this assignment.

# b) Does the system have a unique solution?

# Hint: Compute the determinant of A. Is A invertible? For the R

# code see Exercise 3.

# c) Solve the system of equations.

# The following R code uses the inverse of A:

x <- solve(A)%*%b

# This code is, however, numerically inefficient and potentially

# unstable. A safer and more efficient code is:

solve(A,b)

# Note: In numerical linear algebra matrix inversion is avoided.

# There are other, more efficient ways to solve a linear equation

# system.

# Finally, do some housekeeping and remove all objects.

rm(list=ls(all=TRUE))

**20 %**discount on an order above

**$ 120**

Use the following coupon code :

today2015

**Category**: Essays