Question
Basic Linear Algebra: Matrices Through the course of this exercise, you will create a text file called MATLABsession.txt. Paste the contents of that text file
Basic Linear Algebra: Matrices
Through the course of this exercise, you will create a text file called "MATLABsession.txt". Paste the contents of that text file into the space below.
Step 0: Video
Watch (again?) the MATLAB video, "Working with Arrays"
Step 1: Diary
Turn on the diary function which will record everything you are about to do in this MATLAB session in a file named MATLABsession.txt
>> diary MATLABsession.txt
Your entries and the results will be saved in the MATLABsession.txt diary.
Step 1: Introduction
This part will introduce you to some basic linear Algebra functions available to you in MATLAB. MATLAB allows for very easy computations with vectors and matrices. First perform the following matrix and vector operations in MATLAB and make sure you understand the output. m' represents the transpose of m. The : is a wildcard character that matches all values.
>> m = [1 2 5; 4 7 8; 2 4 6] >> sum(m) >> diag(m) >> sum(m') >> diag(m') >> m(:,1) >> m(1,:) >> m(:,1) == 1 >> m(1,:) < 5 >> any(m(1,:)) < 5 >> all(m(:,1) == 1) >> all(m(:,1) > 0)
Step 2: Basic Vector Manipulation.
Create the following matrices:
>> a = [1 3 5] a =
1 3 5
>> b = [2 ; 4 ; 6] b =
2 4 6
>> c = [5 3 6] c =
5 3 6
Create a few other matrices of your own.
The function length returns the number of components of a vector
>> length (a) ans =
3
The dot operator " ." in MATLAB is used for the component-wise application of the operator that follows the dot. In the following example, a .* c goes through the vectors a and c systematically multiplie the first value in a by the first value in c, the second by the second, etc. Try it:
>> a .* c ans =
??
Apply the dot operator to the power operator ^ to the vector a :
>> a .^ 2 ans =
??
Consider multiplying the two vectors a times b: 1X3 * 3X1 results in a 1X1 vector (also called a scalar): a*b is 1*2+3*4+5*6. Try it:
>> a * b ans =
44
Consider multiplying b times a: 3X1 * 1X3 results in a 3X3
>> b*a ans =
2 6 10
4 12 20
6 18 30
Step 4: Matrices
We will now deal with operations on matrices. Create a 3-by-3 matrix:
>> A = [1 2 4; 5 6 7; 8 10 11] A = 1 2 4 5 6 7 8 10 11
To extract a submatrix B consisting of rows 2 and 3 and columns 1 and 2 of the matrix A do the following (remember, while Python indices start with 0, MATLAB indices start with 1):
>> B = A([2 3],[1 2]) B = 5 6 8 10
Step 5: Dot Operator on Matrices
The dot operator "." works for matrices too. Let now create a 2x3 matrix:
>> A = [2 5 7; 3 5 6] A =
2 5 7 3 5 6
>> B = [1 4 2; 2 3 5]
B=
1 4 2 2 3 5
The following command:
>> A.*B
ans =
2 20 14
6 15 30
computes the entry-by-entry product of A with B. This is called a component-wise operation. However, the following command generates an error message:
>> A*B
Error using *
Inner matrix dimensions must agree.
Why? Because A*B is attempting to do matrix multiplication, not component-wise multiplication! Make sure you understand this distinction! Matrix multiplication is a key aspect of Linear Algebra, but generally beyond the scope of this course.
Step 6: Matrix Transpose
To transpose a matrix is to reverse the rows and columns. The ' operator is MATLAB's transpose operator (it is standard notation to say that A' is the transpose of A):
>> A = [1 2 3;4 5 6;7 8 10] A =
1 2 3 4 5 6 7 8 10
>> B = A B =
1 4 7 2 5 8 3 6 10
Step 7: Matrix Inverse and the Identity Matrix
Just as 1 is the multiplicative identity of the numbers so that 1*N=N for any number N, an Identity Matrix I is one such A*I=A or I*A=A. (In matrix multiplication, A*B may not equal B*A. In fact, it may not even be possible to multiply one way or the other!) Identity matrices are always square (i.e., they have the same number of rows as columns), and have ones on the diagonal and zeros elsewhere. MATLAB's function for generating an NxN identity matrix is eye(N).
>> I3 = eye(3)
I3 =
1 0 0
0 1 0
0 0 1
A matrix M is invertable if there's another matrix M-1 such that M*M-1=I for the appropriate identity matrix I, and M-1 is called the inverse of M. MATLAB's function inv is used to compute an inverse matrix.
Let the matrix A be defined as follows:
>> A = [1 2 4; 5 6 7; 8 10 11]
A =
1 2 4
5 6 7
8 10 11
>> B=inv(A)
B =
-0.6667 3.0000 -1.6667
0.1667 -3.5000 2.1667
0.3333 1.0000 -0.6667
To verify that B is the inverse matrix of A, one must show that A*B = I3 and B*A = I3, where I3 is the 3-by-3 identity matrix. We have
>> A*B
ans = 1.0000 0 -0.0000 0 1.0000 0 0 0 1.0000
In a similar way, check that B*A = I.
Some matrices are not invertable: only square matrices have inverses, and not all of them!
>> A=ones(4) A = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 >> inv(A) Warning: Matrix is singular to working precision. ans = Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
Step 8: Write everything to the file and close it with: <---- THIS IS THE QUESTION
>> diary off
Don't forget to paste the contents of that file below! It will be attached to the assignment as soon as you "check" another question.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started