Mathematica Lab 2

MATH 220E Spring 2025

Instructions

First remember that you must press Shift-Enter to run a code cell in Mathematica.

This notebook contains some instructions and demonstrations about working with matrices in Mathematica. It also contains some problems for you to solve.

Create a new Mathematica notebook where you will solve the problems.

Inputting Matrices and Row Reduction

Matrices are always entered row-by-row.  Each row is a list of numbers between curly brackets, and the whole matrix is then put between brackets.  Be careful with the syntax below.

A={{1,2,3},{4,5,6},{7,8,9}}

This is what Mathematica works with, but to us it doesn’t always look like a matrix yet (sometimes it will right away).  In order to see the “nice matrix form” we use one of the commands below:

MatrixForm[A]

A//MatrixForm

NOTE: Be careful with MatrixForm -- Mathematica will NOT do algebra with MatrixForm. This command is ONLY used show us what the matrix looks like. If you need to “do stuff” with the matrix, use the variable, such as A, where you stored the matrix.

The RowReduce command will put a matrix into reduced row echelon form (RREF).

RowReduce[A]

Problem 1

Enter any 2 x 2 matrix of your choice and call it B2.  Also enter any 4 x 4 matrix and call it B4. Show both B2 and B4 in nice form, then row reduce B2 and B4, and finally show the row-reduced versions of B2 and B4 in nice matrix form.

Manipulating matrix entries

Sometimes it is necessary to isolate a column, a row, or a specific entry of a matrix so that you can change an input error or perform a mathematical calculation.

First we’ll make sure the matrix A from above is still in Mathematica’s memory.

A

A//MatrixForm

To get an entry of a matrix, you reference its position using row number and column number.

For example, the entry in the 2nd row, 3rd column (the “2-3 position” or “2-3 entry”) is accessed via:

A[[2,3]]

Problem 2

Get the 3-3 entry of the matrix A. Then, for your two matrices B2 and B4 from Problem 1, isolate one entry from each and make sure to write a sentence explaining which entry you isolated.

Now, to get an entire row of a matrix, we can use two different methods. Here are two ways to get “row 2” of matrix A:

A[[2,All]]

A[[2]]

To get an entire column of a matrix, you must use the “All” command as seen below for column 3:

A[[All,3]]

Problem 3

Use two different methods to get row 3 of matrix A.  Then, isolate your favorite row of your matrix B4 from Problem 1.

Problem 4

Get the first column of matrix A. Then, isolate your favorite column of your matrix B4 from Problem 1.

You can change a single entry, a row, or a column by setting it equal to whatever you want. For example, if I want to change the 2-3 entry of A from 6 to 10, I simply set the entry equal to 10 as follows.

A[[2,3]]=10

A//MatrixForm

Similarly, I can change whole rows and columns. I’ll do this for row 2 and column 3 here:

A[[2]]={11,12,13}

A//MatrixForm

A[[All,3]]={14,15,16}

A//MatrixForm

Problem 5

For the original matrix A, change row 3 to {1,2,3} and column 1 to {a,b,c}, then show the matrix in matrix form.

Solving systems using row reduction

Suppose we want to solve the following system of equations:

3(Subscript[x, 1])-   (Subscript[x, 2])+   (Subscript[x, 3])-2(Subscript[x, 4])=-5
   (Subscript[x, 1])+   (Subscript[x, 2])-2(Subscript[x, 3])+   (Subscript[x, 4])=1
2(Subscript[x, 1])+2(Subscript[x, 2])+   (Subscript[x, 3])-   (Subscript[x, 4])=5
4(Subscript[x, 1])+           4(Subscript[x, 3])-4(Subscript[x, 4])=-1

We'll input the augmented matrix and row reduce it:

Aug={{3,-1,1,-2,-5},{1,1,-2,1,1},{2,2,1,-1,5},{4,0,4,-4,-1}}

MatrixForm[RowReduce[Aug]]

Now on the homework, we would finish the solution by naming (by convention) (Subscript[x, 4]) as a free variable.  Then we solve for the other variables to get:

(Subscript[x, 4])=free variable = s
(Subscript[x, 1])=(2/5)s-(17/20)
(Subscript[x, 2])=((-1)/5)s+(61/20)
(Subscript[x, 3])=(3/5)s+(3/5)

This is often much easier than inputting all the equations and using the Solve command as in the first Mathematica assignment.

Problem 6

From Section 1.2 of the textbook, do Exercise 21 by having Mathematica row-reduce an augmented matrix, as demonstrated above. Make sure your answer clearly shows all solutions to the system.

Problem 7

From Section 1.2 of the textbook, do Exercise 23 by having Mathematica row-reduce an augmented matrix, as demonstrated above. Make sure your answer clearly shows all solutions to the system.

What do I hand in?

Hand in your Mathematica notebook containing your solutions to Problems 1-7 above. Upload your notebook to the Mathematica Lab 2 assignment on Moodle.