Assignment 6 A
Assignment 6 A
06 th
September 2024
1. Write a script which (i) creates the 2 × 2 matrices (ii) creates the 2 × 2 matrices
iii.prod_matr = ABC , iv.prod_matr_rev = BAC , v. diff_prod_matr = prod_matr − prod_matr_rev ;
vi. transp_prod =(ABC)T , vii.prod_transp_rev = CT BT AT , viii. diff_transp_rule = transp_prod −
prod_transp_rev . Note that you should need only a single line of Matlab to create each of the 2 × 2 matrices
above
3. (i) Write a function with signature function [Ainv] = my2by2inv(A) which takes as input A a 2 × 2 matrix and
returns an output Ainv, the inverse of this matrix. (You may assume that the input A is indeed a 2×2 matrix and
that furthermore A is a non-singular matrix and hence the inverse exists.) Note: You should not use the Matlab
built-in inv but rather write your own code based on the explicit formula for the inverse of a 2 × 2 matrix. (ii)
Write a script which calls your function my2by2inv with input matrix A = [3, -1; 1, 1] and displays the output
(i.e., the inverse of A as computed by your function).
4. For X and Y (with m = 20) as defined in Matlab Exercises Recitation 6 write a script which finds the least
squares solution ˆβ— which we recall minimizes llY – Xβll 2— in the four fashions below. In each case, have the
script display the result.
(i) betahat_a = my2by2inv(X'*X)*(X'*Y) (ii) betahat_b = inv(X'*X)*(X'*Y) (iii) betahat_c = (X'*X) \ (X'*Y)
(iv) betahat_d = X \ Y Note option (iv) is in general the best way — the Matlab backslash (and underlying
numerical approaches) is the fastest and most stable option for solution of linear systems and of least squares
problems. In general, you should avoid the direct computation of the matrix inverse and the Matlab built-in inv,
however for small problems inv works just fine.