Home » University of California Berkeley Matrix Algebra Using MATLAB Exercises

University of California Berkeley Matrix Algebra Using MATLAB Exercises

Use the MATLAB to do.

For each excerise, you need use MATLAB, type code and copy your input and output, then paste into the Word doc.

3 ︱ Matrix Algebra
8/19/2021
3 ︱ Matrix Algebra
Main
FAQ
Quiz
Times
Tutor
Hours
We’ve seen how to use MATLAB to row-reduce matrices and solve linear
systems; now we’re going to use it to perform other basic matrix operations.
In a sense, matrices are a bit like real numbers: we can add, subtract, and
multiply them, provided they have appropriate dimensions. Some square
matrices also have a multiplicative inverse. But there are a handful of striking
differences. For example, when multiplying matrices, it is not generally true
that AB = BA, and A2 may be zero even when A is not the zero matrix. Keep
these things in mind as you work through this lab.
3.1 ︱ Basic Matrix Operations
You’ve learned in class by now that we can add two matrices of the same
dimensions by just adding the corresponding entries. The operator MATLAB
uses for matrix addition is the simple plus sign +, the same one used for
regular addition of numbers. If we enter the matrices
>> A = [1 2 3 4; 5 6 7 8]
>> B = [8 7 6 5; 4 3 2 1]
into MATLAB, we can then add them by typing
>> A+B
ans =
9
9
9
9
9
9
9
9
Matrix multiplication works as you would expect—you simply need the
number of columns of the first matrix to be equal to the number of rows of
the second one. For example, we could enter the following matrices into
MATLAB:
>> C = [1 2; 1 0]
>> D = [0 1; 1 1]
Now we can multiply them:
>> C*D
ans =
2
0
3
1
We can also exponentiate matrices (as in A^3). Lastly, we can ask MATLAB
to compute the tranpose of a matrix with an apostrophe (‘):
https://www.math.ucsd.edu/~math18/Lab3.shtml
1/6
3 ︱ Matrix Algebra
8/19/2021
>> C’
ans =
1
2
1
0
3.2 ︱ Matrix Inversion
One of the key differences between matrix multiplication and multiplication
of real numbers is in the existence of multiplicative inverses. If x is a nonzero
real number, then there’s always another number (namely 1⁄x) that you can
multiply by x to get the multiplicative identity (1). With matrices, we’d like to
do the same thing: given a matrix A, we want to find another matrix B such
that AB = BA = I, where I is an identity matrix.
Just by looking at the condition AB = BA = I, we can see that A and its
inverse B have to be square matrices of the same dimension. Unfortunately,
even square nonzero matrices can fail to have an inverse. If an inverse
exists, we say that A is invertible (or sometimes non-singular); otherwise, we
say it is non-invertible (singular).
The MATLAB command inv finds the inverse of a matrix if it exists.
Exercise 3.1
a. Try using the inv command to find the inverse of the matrix
Notice the strange output. Include your command and the output
in your write-up.
b. Now enter the following matrix A into MATLAB:
>> A = [5 3; 7 4]
Define B to be its inverse in MATLAB. Then run the commands
>> A*B
>> B*A
to check that it satisfies the definition of inverse. Include your
commands and their output in your write-up. (Note that MATLAB
may give you entries like -0.0000 in your results. This still
counts as 0 for our purposes.)
c. Enter the following column vector x:
>> x = [2; 3]
Use the following command to multiply A by x:
https://www.math.ucsd.edu/~math18/Lab3.shtml
2/6
3 ︱ Matrix Algebra
8/19/2021
>> y = A*x
As usual, include your input and output in your write-up.
d. Without entering anything into MATLAB, what do you think you’ll
get if you multiply B by y? Explain your answer.
e. Use MATLAB to check your answer to the last question, and
include your input and output in your document.
3.3 ︱ Logistics: Route Planning
Suppose we have a business operating in five cities around the Pacific Rim:
San Diego, Los Angeles, Tokyo, Shanghai, Manila, and Seattle. We are
interested in counting the number of ways we can travel from one city to
another with at most n stopovers. We look up all the direct flights and put
them in a table:
Destination
San Diego Los Angeles, Seattle
Los Angeles San Diego, Tokyo, Shanghai, Manila, Seattle
Tokyo Los Angeles, Shanghai, Manila, Seattle
Shanghai Los Angeles, Tokyo, Manila, Seattle
Manila Tokyo, Shanghai
Seattle San Diego, Los Angeles, Tokyo, Shanghai
Exercise 3.2
List all possible ways to get from San Diego to Manila with exactly two
stops.
[For example, the trip going from Manila through Tokyo, then LA, then
Seattle is a trip with exactly two stops from Manila to Seattle.]
Doing that last exercise by hand is a pain because there are so many cases
to check, and this is a relatively simple example. If we want to do this
efficiently, linear algebra is the perfect tool. We’ll start by encoding the data
from our table into what’s called an adjacency matrix.
The first step is to number our cities in the order they are listed: San Diego is
1, LA is 2, and so on. We now determine the entries of our adjacency matrix,
which we will call A, using the following rule: if there is a flight from city i to
city j, then the entry Aij is set to be 1. Otherwise, we set that entry to be 0.
We will also set all of the diagonal entries to be 0, since you can’t take a
flight from a city to itself. This procedure gives us the following matrix A:
https://www.math.ucsd.edu/~math18/Lab3.shtml
3/6
3 ︱ Matrix Algebra
8/19/2021
What’s neat about this is that the powers of A have useful information too.
For example, take the entry (A2)36 (i.e., the entry of A2 in the third row and
sixth column). We compute this as
(A2)36 = A31A16 + A32A26 + A33A36 + A34A46 + A35A56 + A36A66
Let’s look at the terms here: A3kAk6 is 1 if and only if both A3k and Ak6 are 1.
This means we get a 1 for the kth term if and only if we can fly from Tokyo
(city 3) to city k and from city k to Honolulu (city 6). Thus, (A2)36 counts the
number of ways to fly from Tokyo to Honolulu with exactly one stop. Similar
reasoning shows that the number of ways of flying from city i to city j with
exactly n stops is just (An+1)ij .
Exercise 3.3
a. Enter the above adjacency matrix A into MATLAB. By looking at
the entry of A3 in the first row and the fifth column, find the
number of ways to get from San Diego to Manila with exactly two
stops. Include your input and output in your document. Does
your answer here agree with your explicit count in the previous
exercise? If not, add the missing trips to your answers of the
previous exercise.
b. Now use MATLAB find the number of ways to get from Manila to
Seattle with at most four stops. Note that this is not the same as
finding the number of ways with exactly four stops! Include all of
your commands and output in your write-up.
Note 3.1: As you may have realized, the method we’ve just used counts silly
trips like Manila
Shanghai
Seattle
Shanghai
Seattle as a trip with
three stops although few people would do that in real life. So the number
may seem large. User beware!
https://www.math.ucsd.edu/~math18/Lab3.shtml
4/6
3 ︱ Matrix Algebra
8/19/2021
3.4 ︱ Political Science: Electoral Trends
We’ve already seen how linear algebra can be used to evaluate simple social
networks. Now we’re going to look at another kind of sociological situation,
where we’ll try to use matrices to model future population dynamics.
In California, when you register to vote, you declare a party affiliation.
Suppose we have just four political parties: Democrats, Republicans,
Independents, and Libertarians. Party registration data is public, so we can
track what fraction of the voters in each party switch to a different party from
one election to the next. Let’s say we look up Democratic Party registration
data and discover the following: 81% of Democrats remain Democrats, 9%
become Republicans, 6% re-register as Independents, and 4% become
Libertarians. We can do the same kind of calculation for the other three
parties and then organize our data into a table:
Democrats Republicans Independents Libertarians
Democrats
0.81
0.08
0.16
0.10
Republicans
0.09
0.84
0.05
0.08
Independents
0.06
0.04
0.74
0.04
Libertarians
0.04
0.04
0.05
0.78
In this table, we’ve put our results in the columns, so the numbers reflect the
proportion of voters in that column’s political party who switch to the party
listed to the left. For example, the entry in the “Republicans” row and the
“Independents” column tells us that 5% of Independents become
Republicans each electoral cycle. We’re going to assume that these
numbers do not change from one election to the next—not a very realistic
assumption, but good enough for our simple model.
Naturally, we want to use this data to predict the outcomes of future
elections and the long-term composition of the electorate. Think of the table
above as a matrix, which we will call P. Let D0, R0, I0, and L0 denote the
current shares of the electorate held by Democrats, Republicans,
Independents, and Libertarians, respectively. In the next election, these
numbers will change according to P, as follows:
D1 = 0.81 D0 + 0.08 R0 + 0.16 I0 + 0.10 L0
R1 = 0.09 D0 + 0.84 R0 + 0.05 I0 + 0.08 L0
I1 = 0.06 D0 + 0.04 R0 + 0.74 I0 + 0.04 L0
L1 = 0.04 D0 + 0.04 R0 + 0.05 I0 + 0.78 L0
Let xn be the vector (Dn, Rn, In, Ln)T. This vector represents the party
distribution after n electoral cycles; the first entry is the portion who are
Democrats, the second the portion who are Republicans, and so on. The
equations we just wrote out above show us that x1 = Px0. In general, xn =
Pnx0.
https://www.math.ucsd.edu/~math18/Lab3.shtml
5/6
3 ︱ Matrix Algebra
8/19/2021
Exercise 3.4
a. Let’s use the results of the 2012 presidential election as our x0.
Looking up the popular vote totals, we find that our initial
distribution vector should be (0.5106, 0.4720, 0.0075, 0.0099)T.
Enter the matrix P and this vector x0 in MATLAB:
>> P =
0.0900
0.0600
0.0400
[0.8100 0.0800 0.1600 0.1000;
0.8400 0.0500 0.0800;
0.0400 0.7400 0.0400;
0.0400 0.0500 0.7800]
>> x0 = [0.5106; 0.4720; 0.0075; 0.0099]
According to our model, what should the party distribution vector
be after three, six, and ten elections?
b. What about 30, 60, and 100 elections from now? How different
are these three results from each other? Summarize what is
happening with xk as k gets big.
3.5 ︱ Conclusion
Hopefully, this lab has given you a little more intuition for the properties of
matrices and their differences from real numbers. In the last exercise, you’ve
also made some interesting observations about what happens when we
apply the same matrix to a vector over and over again. In the next lab, we’ll
develop more mathematical tools to help explain this behavior.
Last Modified: 8 January 2017
https://www.math.ucsd.edu/~math18/Lab3.shtml
6/6
Math Lab Assignment #2
Exercise 2.1:
a)
>> C=[2 1 6;1 0 3;-3 2 -7]
C=
2
1
6
1
0
3
-3
2 -7
>> d=[1;2;3]
d=
1
2
3
>> x=C\d
x=
-20.5000
-3.0000
7.5000
b:
>> C*x
ans =
1
2
3
>> C*x-d
ans =
0
0
0
Ex 2.2
>> C = [-10 5; 6 -3]
>> d = [0 ; 0]
‫ >>܀‬x = C\d
warning: matrix singular to machine precision, rcond = 1.85037e-17
x=
0
0
There exists only 1 free variable.
The answer is an infinity of points (x1, x2) corresponding to x1 = (0.5)*x2
This is why MATLAB gave us an error message when trying to use x
Ex 2.3
>> C = [1 -3 2 0; -2 6 -4 0; 4 -12 8 0]
>> rref(C)
ans =
1 -3 2 0
0 0 0 0
0 0 0 0
To solve this equation, 3 free variables are required. Here, we only have one free variable x 1 =
3×2 – 2×3
The solutions are infinite.
Ex 2.4
In this table, each column represents a category, and each row represents a resident. Residents
can only share the available resources between them. Each resident takes a fraction of each
available resource. The sum of those fractions add up to one because together, residents use
100% = 100/100 = 1 of each resource (column).
Ex 2.5
The table from Ex 2.4 is translated into equations. Each row is an equation of its own:
The variables are the residents, and the coefficients are the fractions of each respective
category of product they consume. The right-hand side of the equation represent the income of
each of the residents respectively. This ensures that each resident earns enough to pay for all
their goods.
Ex 2.6
a)
>> C = [0.25 0.15 0.25 0.18 0.2;
0.17 0.28 0.18 0.17 0.1;
0.22 0.19 0.22 0.22 0.1 ;
0.2 0.15 0.2 0.28 0.15;
0.16 0.23 0.15 0.15 0.45]
>> I = eye(5)
>> A = C – I;
>> B = [0;0;0;0;0]
>> D = [A B]
>> rref(D)
ans =
1.00000 0.00000 0.00000 0.00000 -0.87294 -0.00000
0.00000 1.00000 0.00000 0.00000 -0.73551 -0.00000
0.00000 0.00000 1.00000 0.00000 -0.78550 -0.00000
0.00000 0.00000 0.00000 1.00000 -0.82224 -0.00000
0.00000 0.00000 0.00000 0.00000 0.00000 0.00000
f – 0.872b = 0
t – 0.7355b = 0
c –0.7855 b = 0
m –0.8222 b = 0
b)
=> b is the highest priced commodity because all other commodities are a product of a number
(> L = [0 1/2 0 0 1/3; 1/2 0 0 1/3 1/3; 0 1/2 0 1/3 0; 1/2 0 0 0 1/3; 0 0 1 1/3 0]
L=
0.00000
0.50000
0.00000
0.50000
0.00000
0.50000
0.00000
0.50000
0.00000
0.00000
0.00000
0.00000
0.00000
0.00000
1.00000
0.00000
0.33333
0.33333
0.00000
0.33333
0.33333
0.33333
0.00000
0.33333
0.00000
-0.00000
-0.00000
1.00000
0.00000
0.00000
0.00000
0.00000
0.00000
1.00000
0.00000
-0.83333
-1.00000
-0.75000
-0.75000
0.00000
b)
>> I = eye(5)
>> A = L – I;
>> B = [0;0;0;0;0];
>> C = [A B];
>> rref(C)
ans =
1.00000
0.00000
0.00000
0.00000
0.00000
A = 0.833E
B=E
C = 0.75 E
D = 0.75 E
0.00000
1.00000
0.00000
0.00000
0.00000
-0.00000
-0.00000
-0.00000
-0.00000
0.00000
c) Highest page rank: B and E.
Ranking of remaining websites in decreasing order:
A, C, D
Icon
Item
Comments
Commands entered You should copy relevant input and output from MATLAB
in MATLAB & and paste it into your Word document. You need only
resulting output include commands that worked.
Plots & graphs
Include all graphs generated in an exercise unless the
problem specifically tells you which/how many to include.
Answer questions with at least one or two complete
Full sentence
sentences. Even if you’re stuck, write down any
response
reasoning or ideas you’ve had.
Do scratch work without using MATLAB. Record this
Requires manual
work in your Word document, either by typing it or
work
scanning it.

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more

Order your essay today and save 30% with the discount code ESSAYHELP