Math 272: Introduction to Mathematica and Linear Algebra
Names: 

Our goal today is to learn a few basics of Mathematica and linear algebra.  Work through the examples and exercises, and let me know when you have questions.  When you have finished, save your file with the last names of everyone in your group, followed by Lab 1. 
For example: LevinKareninaOblonsky1.nb 

Then one member of your group should email the file to me (npflueger@amherst.edu). Please CC all other group members.

If you finish this lab before the end of class, let me know so I can briefly check it over and then you will be free to go. If you do not complete this during class time, you may submit it any time until Wednesday night.

To evaluate commands in Mathematica, put the cursor at the end of the command and press shift-enter.

Feel free to explore Mathematica during the lab--check out the menu options and try variations of the examples so that you get a good feel for what Mathematica can do.

Look closely at the following plot command: note the syntax, with the command capitalized and using square brackets.  This is typical Mathematica syntax.

Plot[3 x+1,{x,-2,3}]

For multiple graphs on the same plot, make a list of expressions in curly braces:

Plot[{3 x+1,-2x+4},{x,-2,3}]

It will be more convenient to use implicit equations, so here is a way to plot them. Note that the plots from the example above and the example below are the same except for the scaling of the x-axis.  Also, be aware that the axes do not always go through the origin in Mathematica plots.

ContourPlot[{3x-y==-1,2x+y==4},{x,-2,3},{y,-1,6}]

ContourPlot[x^2+y^2==1,{x,-1,1},{y,-1,1}]

ContourPlot[{((((x^2)+(y^2)))^2)==(x^2)-(y^2),((((x^2)+(y^2)))^2)==2 x y},{x,-1,1},{y,-1,1}]

To make a 3 dimensional plot, we need to use the command ContourPlot3D:

ContourPlot3D[{(x^2)+(y^2)==4,x+y-2z==1},{x,-3,3},{y,-3,3},{z,-3,3}]

Please note that we have used double equal signs to denote an equation.  Single equal signs are used in Mathematica to assign a value or expression to a variable name (for example, the command x=4 sets x to have the value 4 whenever you use it again, while x==4 is an equation that doesn't change the value of x).

A linear equation in 2 variables has form ax+by=c, while a linear equation in 3 variables looks like ax+by+cz=d.

Exercise 1.  Make up two equations for lines that intersect but are not parallel and plot them so that the point of intersection appears in the plot.  To create a new cell for commands, put your cursor just below this text and click so that a long horizontal line appears; then start typing.

Exercise 2.  Make up two equations for lines that are parallel and plot them.

Exercise 3.  Make up two different equations for the same line and plot them.

Now we're ready to think about solving systems of equations (solutions that satisfy multiple equations simultaneously).  In Mathematica, we can do this through the Solve command (to solve nonlinear equations you often have to use the FindRoot command, but this is *linear* algebra, so we won't have to worry about it!):

Solve[3 x+1==0,x]

Once again, note the use of double equal signs to denote an equation!

To solve multiple equations (a "linear system of equations"), make lists in curly braces of the equations and the variables you want to solve for:

Solve[{3x-y==-1,2x+y==4},{x,y}]

Solve[{3x-y+z==-1,2x+y-3z==4,x+z==0},{x,y,z}]

Exercise 4.  Use Solve to find the point of intersection for your two intersecting linear equations (from Exercise 1).

Exercise 5.  See what Solve says for your two parallel linear equations (from Exercise 2).

Exercise 6.  Use Solve on your two linear equations for the same line (from Exercise 3).

Exercise 7. The following linear systems involve equations of planes. Plot each of the following systems (remember to use ContourPlot3D instead of ContourPlot) and use this to guess how many solutions the system has (i.e., none, one, or infinitely many). Then confirm your answer using the Solve command.

(a) x+y==1 and x-y==1 and z==1

(b) x-y+z==2 and x-y==1 and z==1

(c) x-2y+z==1  and 2x-4y+2z==10 and  x+y+z==1

(d) 2x-y+z==2  and 4x-2y+2z==4 and -6x+3y-3z==-6

(e) x+y==1  and x-y==1 and x==-1

You can type in matrices in Mathematica using curly braces, as in the following syntax.

A = {{3,0,-1,0},{8,0,0,-2},{0,2,-1,-1}}

To display the matrix in the usual way, use the MatrixForm command, as follows.

A // MatrixForm

To reduce a matrix to reduced row echelon form (RREF), use the RowReduce command. The following line row-reduces the matrix A defined above, then converts it to MatrixForm to display it.

RowReduce[A] // MatrixForm

Exercise 8. Enter the augmented matrix corresponding to the linear system in Exercise 7(c) into Mathematica (note: Mathematica will not display the vertical line we usually draw in class before the last column), and reduce it to RREF. Use this to check your answer from 7(c).

Exercise 9.  Classify each of the following linear systems as consistent (has at least one solution) or inconsistent (has no solution).  If the system is consistent, determine whether there is a unique solution or infinitely many solutions (involving a free variable). You may do this however you wish: using Solve, using RowReduce, or by graphing.

(a) 2x-y+z==2  and 2x-y+z==6

(b) 2x-y+z==2 and x-3y+z==1 and 2x+y-z==6

(c) 2x+3y==4 and x-y==1 and x+y==3

(d) 2x+3y-z==4 and x-y==1