Tutorial 2 (Matlab)
1. Matrix / > M = [1 2 3; 4 5 6; 7 8 9] / M =1 2 3
4 5 6
7 8 9
2. Transpose / > MT = M' / MT =
1 4 7
2 5 8
3 6 9
3. Identity Matrix / > I = eye(3)
> I = eye(3,3)
> eye(2,3) / I =
1 0 0
0 1 0
0 0 1
ans =
1 0 0
0 1 0
4. Diagonal Matrix / > D = diag([1 3 5]) / D =
1 0 0
0 3 0
0 0 5
5. Ones & Zeros / > O = ones(3,3)
> Z = zeros(2,3) / O =
1 1 1
1 1 1
1 1 1
Z =
0 0 0
0 0 0
6. Solving Systems of Linear Equations Mx = b / > M = [4 2; 8 3];
b = [2 5]';
x = M\b / x =
1
-1
- Give the matrix A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]. Which entries or sub-matrices do the following return?
- A(2,1)
- A(2:4,2:4)
- A(:,1)
- A([3 1],:)
- A(1:2,:)
- What is the result of evaluating each of these Matlab statements?
- A = [5 3; 4 5]; diag(diag(A).^-1)
- A = [1 2; 3 4]; sum(A+A’)
- Write a Matlab command or commands that sums the values between 3 and 299 taking a step of 0.5. That is, calculate, 3 + 3.5+ 4 + … + 298.5 + 299.
- Write a function x = myfunc(A, b) which performs forward substation on the square input matrix A which has the form:
and a vector b of length n. You may assume that the arguments are of the appropriate form.
- Write a Matlab function myfunc which:
- Uses the rand(N) function call to create an NxN matrix, A, of random values
- Subtract 0.5 from each entry of the matrix
- Each entry on the diagonal is swapped with the largest (in abs value) off-diagonal entry in the same row containing the diagonal entry.
- Your function should probably use the functions max, abs, and ones (as described in class) and it should start with function A = myfunc(N).