Unformatted text preview: Lecture 2. Matlab as a Matrix Calculator In the previous lecture, we explored the capabilities of Matlab as a scalar calculator. The true power of Matlab lies in its ability to manipulate matrices. This lecture will cover the following topics:
understanding a matrix
building matrices in Matlab
generating special matrices
matrix operations
element by element operations
solving simultaneous equations
solving an over constrained system Understanding a matrix: Last lecture we dealt with scalars, which are identified by a single value or magnitude. Temperature is an example of a scalar. The power of Matlab comes from its ability to manipulate matrices. An m x n matrix is a regular scheme of number that are arranged in the form of ! a11 a12 ! a1n $
#
&
# a21 a22 ! a2 n & #!
&
!
"!
#
&
" am1 am 2 " amn %
where m is the number of rows and n is the number of columns. (If you have issues remembering whether a column is going across or going down, try imagining yourself at a coliseum, and a row would be going across the entire coliseum and a column would be going down from the top to the bottom.) The numbers a11, a12, …aij….amn are the elements of the matrix, where 1<i<m, and 1 < j<m. The i is called the row index and the j is called the column index. We can refer to any element in the matrix by stating its row index and column index. The size of the matrix is m x n. A matrix is called a square matrix if it’s size is n x n, where the number of rows is equal to the number of columns. In Matlab, everything is treated as a matrix. The scalars we used during the first lecture were treated as 1 x 1 matrices. A matrix in the form of m x 1 is called a column matrix and a 1 x n matrix is called a row matrix. Row matrix and column matrix play a special role when dealing with vectors. Example: A row matrix: A = [ 1 2 3 !1
#
A column matrix: B = # 2
#3
" $
&
& &
% An example where we would use a matrix is to describe the location of a point. Take the following graph as an example: On the xy plane (2D), to describe the location of any point, we would use its x
coordinate and its y
coordinate. The green point is located at (2,3). The purple point is located at the origin (0, 0). The red point is located at (
3, 1). And the blue point is located at (
1.5,
2.5). To better organize the points, we can write a matrix to represent these points. The matrix would have the form of [x y]. The four points above can be written as one matrix in the form of ! $3
%
1
#
'
0
#0
' # $3
'
1
#
'
" $1.5 $2.5 &
Similarly, in a 3D space, we would need three values to describe the location of an object: a x
coordinate, a y
coordinate, and a z
coordinate. In the graph above, the black point is located at (x ,y ,z). We can represent s series of points in 3
D using a matrix Example: !123$
#
&
# 4 5 6 & #789&
"
%
Would represent three points in space with (x, y, z). Building Matrices: To create a matrix in Matlab, we use the “[ “ and “]” (which are called the concatenation operator) to enclose the elements. Each element is separated by a comma “,”, and each row is separated from the next by a semicolon “;”. Example: >> [ 1, 2, 3; 4, 5, 6; 7, 8, 9] ans = 1 2 3 4 5 6 7 8 9 Exercise: !58%
#
'
Create the following matrix in Matlab: # 3 $6 ' # 2 $3 '
"
&
The colon operator “:” allows rapid construction of a matrix with elements that differ by a constant value. The colon operator takes the form of: >>start : end Example: If you want to create a row matrix with values from 1 to 10, enter: >> a = [ 1:10] a = 1 2 3 4 5 6 7 8 9 10 >> b = [1:5; 2:6] b = 1 2 3 4 5 2 3 4 5 6 Notice that the values are increasing by 1. To change the increment, use an alternative form of the colon operator: >>start : increment :end Example: To decrease the value by 1 for each element, we would use: >> a = [ 10:
1 : 1] a = 10 9 8 7 6 5 4 3 2 1 >> b = [ 3: 0.1: 3.5] b = 3.0000 3.1000 3.2000 3.3000 3.4000 3.5000 Exercise: Create the following row matrix in Matlab using the colon operator: ! $6 $1 4 9 14 %
E =#
' " 15 12 9 6 3 &
The brackets “[ ]” also serve as the concatenation operator in Matlab. If we want to add values into an existing matrix, we would enter: [a : values to be added] Example: >> a = [ 1 2] a = 1 2 >> [a, 3 4] ans = 1 2 3 4 >> [a; 3 4] ans = 1 2 3 4 Generating Special Matrices: Matlab provides some special functions that generates prefilled matrix for us.
ones(m,n): generate a m x n matrix that is filled with 1’s >> ones(3,2) ans = 1 1 1 1 1 1
zeros(m,n): generate a m x n matrix that is filled with 0’s >> zeros (2, 4) ans = 0 0 0 0 0 0 0 0
rand(m,n): generate a m x n matrix that is filled with elements randomly selected from 0 to 1 >> rand(2, 2) ans = 0.8147 0.1270 0.9058 0.9134 Accessing Elements in a Matrix: To access an element in a matrix, enter its row number and column number. Example: >> a = [ 1, 2, 3; 4, 5, 6; 7, 8, 9] a = 1 2 3 4 5 6 7 8 9 >> a(1,3) ans = 3 >> a(3, 2) ans = 8 Note: Matlab starts counting the row and column at 1, not 0! To access the values in a particular row, we use the colon operator: a(row, :) To access the values in a particular column: a(:, column) Example: >> a = [ 1, 2, 3; 4, 5, 6; 7, 8, 9] a = 1 2 3 4 5 6 7 8 9 >> a(1, :) ans = 1 2 3 >> a(:, 1) ans = 1 4 7 Matrix Operations: Two matrices can be added together and subtracted from each other as long as they are of the same size. (Same number of rows and same number of columns.) Matrix addition: ! 9 6 $ ! 6 3 $ ! 9 + 6 6 + 3 $ ! 15 9 $
#
&#
&#
&#
&
# 8 5 & + # 5 2 & = # 8 + 5 5 + 2 & = # 13 7 & # 7 4 & # 4 1 & # 7 + 4 4 + 1 & # 11 5 &
"
%"
%"
%"
%
Matrix subtraction: ! 9 6 $ ! 6 3 $ ! 9'6 6'3 $ ! 3 3 $
#
&#
&#
&#
&
# 8 5 & ' # 5 2 & = # 8 ' 5 5 ' 2 & = # 3 3 & # 7 4 & # 4 1 & # 7 ' 4 4 '1 & # 3 3 &
"
%"
%"
%"
%
Exercise: !26$
!45$
A =#
& B = #
& "53%
"29%
What is A+B? What is A
B? Matrix multiplication: Given the matrix A of size m x n and another matrix B of size n x p, to calculate A * B, we would multiple each element in a row by the corresponding elements in a column of B and add the sum. A * B would produce a matrix of size m x p. !ef$
!
$
If A = # a b & and B = #
& then "gh%
"cd% !
A*B =#
" ab
cd $!
&#
%" e f g h $!
&=#
%" ae + bg af + bh
ce + dg cf + dh $
& % Example: !123
If A = #
"456 !
$
#
& and B = #
%
#
" 7 10
8 11
9 12 $
&
& , calculate A*B &
% We would first start with the first row in A: 1*7 + 2*8 + 3 *9 = 7 + 16 + 27 = 50 1* 10 + 2*11 + 3 * 12 = 68 then we move on to the second row in A: 4 * 7 + 5 * 8 + 6 *9 = 122 4* 10 + 5*11 + 6 * 12 = 167 our result is: !
1*7 + 2*8 + 3 *9
1* 10 + 2*11 + 3 * 12
A*B =#
" 4 * 7 + 5 * 8 + 6 *9 4* 10 + 5*11 + 6 * 12 = 167 ! 50 68
=#
" 122 167 $
&
% $
&
% To multiply a matrix by a scalar, we would multiply ALL of the elements in the matrix by the given scalar. Example: !13$
A =#
& "24%
! 1 3 $ ! 2 *1 2 * 3 $ ! 2 6 $
2 A = 2 *#
&=#
&=#
& " 2 4 % " 2*2 2*4 % " 4 8 % The division operation involving matrix “ /” and “\” are unique and will be further explored later in the chapter. The transpose operator ‘ turns the column vectors into row vectors and vice
versa. Example: >> A = [1,2; 3,4;] A = 1 2 3 4 >> A' ans = 1 3 2 4 Exercise: Create the following column matrix in Matlab using the colon operator: (hint: use the transpose operator on the matrix E that you created earlier!) ! $6 %
#
'
# $1 '
F = # 4 ' #
'
#9'
# 14 '
"
&
Element
by
Element Operations: Matlab also provides a way to multiply and divide each individual element in a matrix by another element in a corresponding matrix. The element
by
element multiplication operator is “.*” and the element
by
element division operator is “./”. Matlab also provide an exponentiation operator “.^”. These operators can only be used on matrices with the same size. Example: !26$
!45$
A =#
& and B = #
& "53%
"29%
!
A. * B = #
" 2*4 6*5
5* 2 3* 9
! 2/4 6/5
A. / B = #
" 5/2 3/9
! 2^4 6^5
A. ^ B = #
" 5^ 2 3^ 9 $ ! 8 30
&=#
% " 10 27
$!
&=#
%" 0.5 1.2
2.5 0.3 $!
&=#
%" 16 1776
25 19683 $
& %
$
& %
$
& % Solving Simultaneous Equations: One of the most useful computations Matlab can perform is solving simultaneous equations. Here is an example you probably have seen before: Solve for x and y that satisfy the following equations: x + y = 4 2 x – 3y = 3 This problem is easy enough to solve by hand. We rewrite x + y = 4 into y = 4 – x, and substituting in for y in the second equation to obtain 2x
3(4 – x) = 3, and solve for x. The solution is x = 3 and y = 1. Now try the following question: Solve of x, y, and z that satisfy the following equations: x + y + z = 15 2x + 3y – z = 21 3x – y + 2z =
5 The equation is more difficult to solve by hand because now we have to deal with three equations and three unknowns. Fortunately, Matlab can quickly find the solutions for us. But first, we would need to rewrite the equations in a form that Matlab can understand. !!
We need to rewrite the three equations in the form of A * x = b where A is a matrix !
!
of the coefficients of the equations, x is a column vector of the unknowns, and b is a column vector of the results. (We add the arrow on top of the variable x and b to denote that they are matrices, not scalars.) We can rewrite x + y = 4 2 x – 3y = 3 into: ! 1 1 %! ! x $! ! 4 $
A =#
& b = #
' x = #
& "y%
" 2 $3 &
"3%
!!
Observe that A * x = b is similar to a scalar equation such as 3x=6, you can isolate x, by “dividing” both side by A. Instead of dividing both sides by A however, we !
!!
multiple both sides by the inverse of A, inv(A), to obtain inv(a ) * A * x = x = inv(a ) * b So, to obtain the values for x, we just need to multiply the inverse of A with the !
!
matrix b . (Note: The order of operation matters! inv(a ) * b is not the same as !
b * inv(a ) !) To solve this problem in Matlab, enter the following in the Command Window: >> A = [1, 1; 2,
3] A = 1 1 2
3 >> b = [4; 3] b = 4 3 >> inv(A)*b ans = 3 1 >> A\b ans = 3 1 Instead of using inv(A)*b, Matlab also provides another shortcut for solving simultaneous equations in the form of A\b, which is equivalent to inv(A)*b. Note: Calculating the inverse of a matrix by hand is beyond the scope of this lecture. Over constrained system/Least Square: (Adapted from Paul Dawkins’ Lecture Notes) Least square is used to approximate an answer. Consider the following problem: Find the equation of a line that runs through the following points: (
3, 70), (1, 21), (
7, 110), and (5,
35). Recall that the equation of a line can be written in the form of y = mx +b. Therefore, the solution must satisfy the following equations:
3m + b = 70 1m + b = 21
7m + b = 110 5m + b =
35 which we can rewrite into the following form: Now if we try to use the inv(A)*y method: >> a = [
3, 1; 1, 1;
7, 1; 5, 1]; >> b = [70; 21; 110;
35]; >> inv(a) ??? Error using ==> inv Matrix must be square. This would not work for two reasons: 1) The matrix A is not a square matrix, therefore we cannot find an inverse of it 2) The is no line that can go through all of the points at the same time Observe the following graph: However, we can find a line that tries to go all the points as closely as possible using least square. To do so, we would use the left divide “\” operator, which doubles as the least square operator: >> a\b ans =
12.1000 29.4000 (http://tutorial.math.lamar.edu/Classes/LinAlg/LeastSquares.aspx) More on Indexing Vectors (Optional, From Mathworks’ Website): Let's start with the simple case of a vector and a single subscript. The vector is >> v = [16 5 9 4 2 11 7 14]; The subscript can be a single value. >> v(3) %Extract the third element ans = 9 Or the subscript can itself be another vector. >> v([1 5 6]) % Extract the first, fifth, and sixth elements ans = 16 2 11 The colon notation in MATLAB provides an easy way to extract a range of elements from v. >> v(3:7) % Extract the third through the seventh elements ans = 9 4 2 11 7 Swap the two halves of v to make a new vector. >>v2 = v([5:8 1:4]) % Extract and swap the halves of v v2 = 2 11 7 14 16 5 9 4 The special end operator is an easy short
hand way to refer to the last element of v. >> v(end) % Extract the last element ans = 14 The end operator can be used in a range. >> v(5:end) % Extract the fifth through the last elements ans = 2 11 7 14 You can even do arithmetic using end. >> v(2:end
1) % Extract the second through the next
to
last elements ans = 5 9 4 2 11 7 Combine the colon operator and end to achieve a variety of effects, such as extracting every k
th element or flipping the entire vector. >> v(1:2:end) % Extract all the odd elements ans = 16 9 2 7 >> v(end:
1:1) % Reverse the order of elements ans = 14 7 11 2 20 15 10 16 By using an indexing expression on the left side of the equal sign, you can replace certain elements of the vector. >> v([2 3 4]) = [10 15 20] % Replace some elements of v v = 16 10 15 20 2 11 7 14 Usually the number of elements on the right must be the same as the number of elements referred to by the indexing expression on the left. You can always, however, use a scalar on the right side. >> v([2 3]) = 30 % Replace second and third elements by 30 v = 16 30 30 20 2 11 7 14 This form of indexed assignment is called scalar expansion. (http://www.mathworks.com/company/newsletters/articles/Matrix
Indexing
in
MATLAB/matrix.html) Further Reading: Building Matrices “[ ]” a. http://www.imc.tue.nl/IMC
main/IMC
main
node32.html b. http://www.imc.tue.nl/IMC
main/IMC
main
node33.html#Ch4.2.1 c. http://www.imc.tue.nl/IMC
main/IMC
main
node35.html#Ch4.2.3 Generating special matrices d. http://www.imc.tue.nl/IMC
main/IMC
main
node36.html#Ch4.2.4 Accessing elements in a matrix e. http://www.imc.tue.nl/IMC
main/IMC
main
node37.html#Ch4.2.5 Matrix operations: “*”, “/” f. http://www.purplemath.com/modules/mtrxmult.htm g. http://www.imc.tue.nl/IMC
main/IMC
main
node38.html#Ch4.3 Element
by
element operations: “.*”, “./” Solving simultaneous equations h. rewriting simultaneous equations in the form of Ax=b i. explaining inverse of a matrix i. http://www.imc.tue.nl/IMC
main/IMC
main
node39.html#Ch4.4 ii. Over constrained systems/least square j. http://tutorial.math.lamar.edu/Classes/LinAlg/LeastSquares.aspx The images of the Coordinate Systems are courtesy of Wikipedia. ...
View
Full Document
 Spring '08
 greenwood
 Calculus, matlab, Matrices, Scalar

Click to edit the document details