Professional Documents
Culture Documents
Wasn't that easy? Now we'll try something a little harder. First we define a matrix
b:
>> b = [ 1 2; 0 1 ]
Then we compute the product ab:
>> a*b _______________
_______________
Notice that the two products are different: matrix multiplication is non
commutative.
Of course, we can also add matrices:
>> a + b _______________
_______________
Now let's store the result of this addition so that we can use it later:
>> s = a + b
Matrices can sometimes be inverted:
>> inv(s) _______________
_______________
To check that this is correct, we compute the product of s and its inverse:
>> s * inv(s) _______________
_______________
The result is the unit, or identity matrix. We can also write the computation as
>> s/s _______________
_______________
We can also write
>> s\s _______________
_______________
Not all matrices can be inverted, or used as the denominator in matrix division:
>> c = [1 2; 1 2]
>> inv(c) _______________
_______________
A transpose of a matrix
>> a’ _______________
_______________
Building Matrices
Matlab has many types of matrices which are built into the system. A 2 by 2 matrix
with random entries is produced by typing
rand(2) _______________
_______________
You can generate random matrices of other sizes and get help on the rand
command within matlab:
rand(2,5) _______________
_______________
Some of the standard matrices from linear algebra are easily produced:
eye(6) is an identity matrix of 6 rows and 6 columns.
zeros(4, 7) will return a matrix having all entries equals to zero.
ones(5) will return a matrix having all entries equals to one.
You can also build matrices of your own with any entries that you may want
You may have discovered by now that MATLAB is case sensitive, that is "a" is not
the same as "A." If this proves to be an annoyance, the command
casesen
will toggle the case sensitivity off and on.
Sometimes you will have spent much time creating matrices in the course of your
MATLAB session and you would like to use these same matrices in your next
session. You can save these values in a file by typing
save filename
This creates a file
filename.mat
what
MATLAB replies with the current directory and MATLAB files in the directory
Colon Notation:
Now we will use the colon notation to select a column of a.
To select rows or columns of a matrix, use
a= 12 5
-2 13
>> d=a(1,:); d=[12 5];
>>e=a(:,2); e=[5;13];
>>a(2,:)=[3 6]; replaces second row of a
>>a(:,[1 2])
>>a([2 5],[2 4])
Left Division:
if an equation is of the form a*x=b with a as a square matrix, x=a\b is the same as
x=inv(a)*b
i-e [x] = [a]\[b] is same as [x] = [a-1][b]
For example
a = [ 1 -4 3; 3 1 -2; 2 1 1];
b = [ -7; 14; 5];
Now enter the command
x = a\b
result: x1 = [ 3 1 -2 ]
Which i s the same result found earlier for x when solving by matrix inversion.
Right Division:
The matrix format in which right division is employed is less common but no less
successful in solving the problem of simultaneous equations. Right Division is
invoked when the equations are written in the form
[x][a] = [b]
x1 - 4x2 + 3x3 = -7
3x1 + x2 - 2x3 = 14
2x1 + x2 +1x3 = 5
These equations can be written in matrix format
[x][a] =[b]
if
x = [x1 x2 x3] B = [ -7 14 5]
And
132
[a] = -4 1 1
3 -2 1
Note that [a] is equal to the transpose of previously used [a].
A = a'
Having so defined A and B, the solution for x can be obtained by right division.
x = b/a
results in
x = [ 3 1 -2 ]
LAB#01 EXCERCISE
TASK 01. Find the solution to
r+s+t+w=4
2r - s + w = 2
3r + s - t - w = 2
r - 2s - 3t + w = -3
Using the matrix inverse and left and right division. write down the matlab code
here:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
______________