0% found this document useful (0 votes)
3 views1 page

Assignment 6 A

Assignment for MATLAB students
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

Assignment 6 A

Assignment for MATLAB students
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Assignment-6

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

2. We define, for a given integer m, h =1/(m


− 1); xi =(i − 1)h,1 ≤ i ≤ m; the m × 2 matrix X,

and the m × 1 vector Y ,

Note Xi1 =1, 1 ≤ i ≤ m, Xi2 = xi,1 ≤ i ≤ m, and Yi =0.1 sin(πxi), 1 ≤ i ≤ m.


(i) Create a function function [res_sq] = eval_res_sq(m,v) which for inputs m (a scalar) and v = v = [v1; v2] (a
column 2-vector) returns the scalar res_sq given by[res_sq] =Y TY − 2 vTXTY + vTXTXv for X and Y defined
(within your function) by equations (1) and (2), respectively.
Note: You should only need a single Matlab line to define each of [xi, 1 ≤ i ≤ m], X, and Y .
(ii) Write a script which evaluates your function eval_res_sq for inputs m = 20 and v = [1;1].

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.

You might also like