Learning Mathematica Basics—Vectors and Matrices

Solve each of the following problems using Mathematica exclusively.

1.  Form the list {1,2,3,4,5} and assign it to a variable named vector1.

2.   Ask Mathematica for the syntax of the command MatrixForm.

3. When reading the description of MatrixForm it is important to note that it is not a command that achieves anything at all mathematically.  It is merely a formatting option that is used to make your output look a little prettier.  Indeed, you should make certain never to use MatrixForm as part of a calculation.

Go ahead and apply MatrixForm to vector1.

4.   Your output should have been oriented vertically, unlike the output in problem 1.

Let's now form a 3x3 matrix with rows of {1,2,3}, {4,5,6}, and {7,8,9}, and read it into the variable matrix1.  Since you need to form a "list of lists" the command you will need is:  matrix1={{1,2,3},{4,5,6},{7,8,9}}.

5.   Now apply MatrixForm to matrix1.

6.  Your output should now look a little more like what you are used to calling a matrix.

Now, let's do a little bit of matrix arithmetic.  Multiply vector1 by 5, treating it as would an ordinary variable.

7.   You have just done a scalar multiplication.  Mathematica took your multiplier of 5 and applied it to every element of vector1 separately.  Try doing the same thing to matrix1, i.e. multiply it by 5.

8.  The same effect!  Now let's try the same thing with an exponent.  Apply a square to vector1.

9.   Each element should have been squared.  Now do the same thing to matrix1 (square it), but also apply MatrixForm to the result (in the same command), and read the answer into a new variable, matrix2.  (Only one line of code is required here.)

10.  So far, so good.  (You should have produced a square matrix with an 81 in the lower right corner.)  Now multiply matrix2 by the number 3.

11.  Did you get what you expected?  Probably not!  I just got matrix2 back with a 3 sitting next to the first row.  So why didn't we get the scalar multiplication we saw in problem 7?  Recall what we said in problem 3 about the MatrixForm command!  We said that it is merely a formatting command—it should never be used in a calculation.  But look at what we did in problem 9!  matrix2 is not just the square of matrix1—it's also got a MatrixForm formatting option as part of it's definition.  This means that matrix2 is not a mathematical object that can be manipulated as we did previously.  matrix2 is essentially just formatted text.  That's why problem 10 behaved strangely.

Now form a new vector, call it vector2, and read {2,4,5,9,11} into it.

12.   Combine vector1 and vector2 by adding them.

13.   Looks like corresponding elements are added, right?  Now multiply the two vectors together.

14.   Create vector3 with elements {6,4,2}.

15.   Try adding vector1 to vector3.

16.   You should have received an error message.  It might even make sense to you.  Vectors can only combined in this way if they have exactly the same dimensions.  The same applies to matrices, but we won't spend the time checking them out right now.

If you've had some experience with Linear Algebra you will have noticed that what we did in problem 12 was not true matrix multiplication.  We'll learn how to handle that, along with other Linear Algebra matrix operations, in a later notebook.

In PrepQuiz3 you were introduced to the Table command which we can use to build lists whose elements have a recognizable formula defining them.  Just to jog your memory ask Mathematica for the syntax of the Table command again.

17.   As an example, if we wished to construct a list of the cubes of the natural numbers 7 through 12, inclusive, we would use the command:  Table[k^3,{k,7,12}].  Go ahead and try it.

18.  Notice that the latter part of the syntax description you asked Mathematica for above mentions how the command may be used to form "nested lists."  But a nested list is really just another name for a matrix.  i.e. we can use the Table command to build matrices, provided they have a recognizable structure.

First we'll do an example, then you can try one of these on your own.  To start with you need to be familiar with a little terminology.  Go ahead and enter the following command:  MatrixForm[{{a,b,c},{d,e,f},{g,h,i},{j,k,l}}].

19.   What you have just displayed is called a 4x3 matrix.  (The number of rows is given first, and the number of columns is given second.)  Each element in the matrix can be thought of as having coordinates specified by the row and column in which it appears.  For example, the element k is in the (4,2) position, and the element c is in the (1,3) position.  Make sure you understand the terminology just discussed.

Now, let's use the Table command to construct a 4x6 matrix where the value of each element is simply the sum of the element's own coordinates within the matrix.  For example, the element in the (2,5) position would be 7, and the element in the (4,2) position would be 6.  (Get it?)

The command to do this follows.  Study it very carefully before you go on:

MatrixForm[Table[m+n,{m,1,4},{n,1,6}]]

20.   Look at the answer you produced.  Do the elements satisfy the requirements we made?  As an example look at the (2,6) position.  There's an 8 there, right?  And 8 is the sum of 2 and 6.  Making sense?

Your turn!  Construct a 5x3 matrix where the value of each element is the sum of the squares of its coordinates within the matrix.  For example, the element in the (3,2) position should be 3^2 + 2^2 = 9 + 4 = 13.

Your command should be similar to that used in problem 19.