Matrix Lab Manual
Matrix Lab Manual
Matrix Lab Manual
MAT188: Laboratory #6
Matrices and For Loops
Engineering Problem Solving Labs MAT188: Laboratory #6
Review
A matrix of size m × n is a rectangular array of numbers with m rows and n columns. A row vector
is a 1 × n matrix, while a column vector is an m × 1 matrix. A square matrix has the same number
of rows as columns, for example:
In MATLAB, commas or spaces are used to separate elements within a row and semicolons are
used to separate rows. For example, define the above matrix:
>> A = [1 2 3; 4 5 6; 7 8 9] % row by row input
Notice how the semicolon separates the rows, and the colon (:) is used to specify a range of numbers
as elements. 1:4 means from 1 to 4 in increments of 1, while -1:2:5 means from -1 to 5 in
increments of 2.
Most of the basic operators defined for scalars are defined in MATLAB for matrices. For example,
to add two matrices:
>> B = [1 1 1; 2 2 2; 3 3 3]
>> C = [1 1 1; 2 2 2; 3 3 3]
>> B + C
Like with vectors, standard multiplication and division do not operate on the individual components.
For example,
>> B*C
does not multiply the individual components of B and C. It performs a different operation called
matrix multiplication, which you will learn about later this term in linear algebra (MAT188).
Like with vectors, to do element by element multiplication (multiply each element in one matrix by
the corresponding element in the other matrix), we must prefix the operator with the dot operator
(.), for example:
>> B.*C
You must also use the dot operator for element by element division (./) and exponentiation (.^).
Exercises:
Evaluate the following expressions using MATLAB and fill in the table.
313 111
First, define the matrices 𝐴 = [1 2 1] and 𝐵 = [2 2 2].
341 333
Expression Result
A./B
A.*B
A.^2
rank(A)
rank(B)
Matrix Indexing
Specific elements of a matrix can be accessed using indexing, in which index 1 represents the first
element. For example, consider the matrix A:
−1 0 5
𝐴 = [ 6 2 −3 ]
10 −4 8
>> A=[-1 0 5;6 2 -3;10 -4 8];
>> A(1,2)
>> A(1,:)
>> A(:,2)
>> A(1:2,2)
>> A(3,2)=cos(pi)
Or make use of the colon operator to assign new values to a part of a matrix:
Or entire rows:
>> x=[-2:2:2];
>> A(2,:) = x.*exp(-2*x)
Now, assign the value 0 to all the elements in the second column of A.
Optional – if you need some additional exercise in flexing your matrix muscle, try the following:
for i=1:5
x(i)=i*2
end
This for loop iterates over the range of numbers 1:5 and runs the code in the loop (until end) for
each value. In this example, the loop defines the variable (also called the argument) i=1, runs the
code in the loop, returns to the beginning, defines i=2, runs the code in the loop, returns to the
beginning, and so on, with the last iteration of the loop running with i=5.
Consider various populations of bacteria whose populations are modelled by the function 𝑃(𝑡) =
1000 0.5𝑎𝑡
𝑒 , where 𝑃(𝑡) is the population, 𝑡 is the time in hours, and 𝑎 is some positive constant.
𝑎
Your job is to create a plot of how different populations behave for different values of 𝑎. Use a for
loop to go through values of 𝑎 in the interval [1, 2] in steps of 0.1 (𝑎 = 1, 𝑎 = 1.1, 𝑎 = 1.2 … 𝑎 =
2), and plot all of them on the same figure (11 different functions). Hint: Before the for loop, use a
figure command to create a new figure window and hold on to keep all plots on the same
figure.
Use your figure to answer the following questions:
1. Using your understanding of exponential functions, identify the general order or pattern of
which function (𝑎 value) corresponds to which line.
2. Which population grows the fastest as 𝑡 → ∞? At what value of 𝑡 does it becomes the biggest
population? (Similar to previous labs, you may need to use the axis function and the zoom
tool in the figure window to see this. You may also want to adjust the domain of 𝑡 values you
are using.)
3. Choose three of the populations and read the figure to estimate the doubling time (amount of
time it takes for the population to double) for each. Does this match what you can find
analytically using the function expression? Can you find a relationship between the doubling
time and 𝑎 value?
4. In a paragraph or two, please discuss similarities/differences in doing matrix math by-hand
versus via Matlab. When is one more appropriate than another? If so, when? What are some
important considerations an engineer should think about when using “for” loops?
For submission by Wednesday October 25th 11:59pm via MAT188 PRA website: 1-page PDF file
with appropriate screenshots and clear answers to the questions in the lab, including completion of
Table 1. Please write your name and student number on your submission for credit.