Slightly Advanced Introduction to Mathematica.

Below is a slightly advanced introduction to some concepts in Mathematica.  You may be familiar with most of them.

On the right hand side there are a number of brackets,  Click on the one that is beside the expression "2+2".  Then press the return key and the shift key simultaneously.  This will start  up the Mathematica "kernel" or central processing program.  It  may take a few seconds to respond.

2+2

Mathematica can do more than evaluate 2+2.  Click on the bracket to the right of each of the following expressions and  again press "shift-return".

4 (3^6)-28 (7^2)

((234 128-48 632)/(54^2))

((Sin[(π/6)])^4)+((Cos[(π/4)])^2)

100!

(Sqrt[15241578750190521])

In Mathematica adjacent terms are multiplied (you don't have to explicitly put in a multiplication symbol).  To raise something to a power, you can either use the caret "^" symbol, or use the BasicInput palette and typeset an exponent.  (Select the "Palette" submenu from the "File" menu).   Thus:

                            x^2 = (x^2)

   Also note that we have functions like Sqrt[...] for taking the square root, and sines and cosines are Sin[] and Cos[].  The capital letters are important, as are the square brackets.  All predefined functions in Mathematica are capitalized, and take square brackets.  The curly brackets {} are used to separate  parameters, or to mark elements as belonging to a list.  The  program will whine petulantly and refuse to work if you use  the wrong brackets in the wrong place, or forget a comma when  it wants one there.

The most important point in this whole presentation:

One extremely important point: highlight the word "Sin" above in the input box.  Now pull down the help menu and select "Find Selected Function".  This will tell you all about the sine function in Mathematica.  The help browser in Mathematica is extensive and extremely helpful.  You can look up functions alphabetically in its index or browse them by subject. You can take a tour of Mathematica and see what it can do.  You can see thousands of examples.  Learn to use the browser.

Mathematica is a Symbolic manipulator:

Mathematica is a symbolic manipulator.  It does not always evaluate things in a way you might expect!  Below are two  examples.  In each case we as Mathematica to evaluate an  expression.  If we want a decimal or NUMERICAL answer, we have  to use the  function N[...], which calculates its argumument in a numerical fashion.

(14/6)

N[(14/6)]

(Sqrt[68])

N[(Sqrt[68])]

(Sqrt[68.])

Mathematica is more than just a fancy calculator.  It can perform algebraic operations as well.  The form or "syntax"   is:

Solve[{eq1==rhs1,eq2==rhs2,...},{var1,var2..}]

Note the funny double equal signs (==).  This is used because we are specifying a symbolic relationship, we're not just renaming the eq1 by rhs1.   There are a few examples below.  Again, click on the bracket  on the RHS and hit shift-enter.
  
  A quadratic equation:

Solve[{(x^2)+x-12==0},{x}]

A cubic equation:

Solve[{(x^3)-3 (x^2)+x+1==0},{x}]

Even a quartic equation!

Solve[{(x^4)+(x^3)-4 (x^2)-x+1==0},{x}]

You can also solve systems of equations this way:

Solve[{x+y == 12, 4x-2y== 24},{x, y}]

However this is not the recommended way for large systems of equations - use

Integrals:

Mathematica can do integrals symbolically.   Here's an integral you probably don't want to do by hand:

Integrate[Sin[x]/x,{x,0,Infinity}]

Mathematica can do this integral numerically.

NIntegrate[Sin[x]/x,{x,0,Infinity}]

The point is that Mathematica can also do integrals that have no hope of being done analytically:

NIntegrate[((BesselJ[0,x])^2) Sqrt[(1/(1+(x^2)))] ,{x,0,1}]

Vectors and Matrices:

The fundamental item in Mathematica is the "list."   This is much like a vector. It consists of a set of elements  enclosed in curly brackets:  "{a1, a2, a3...}".    We can construct a list either by hand, or by having Mathematica perform some action repeatedly using the Table command.  The latter is much like a "do-loop" in Fortran or a "for-next" loop in Basic.

vector1={1,2,3,4}
vector2=Table[n!,{n,1,20}]

When working with vectors and matrices, it is important to know when to tell Mathematica to shut up.  If you put a semicolon on the end of a line, then the calculation is performed, but the output is suppressed.

vector1={1,2,3,4};
vector2=Table[n!,{n,1,20}];

If you want a pretty version of the vector, then use MatrixForm.

MatrixForm[vector2]

To refer to an element in the list, we just double square brackets.  For example, the 10th element in vector2 is vector2[[10]]:

vector2[[10]]

A matrix is just a list of lists.  Again, we can write it out by hand or have Mathematica create it using a Table command.

matrix1={{1,2,3,4},{5,0,7,8},{5,4,0,2},{7,6,5,4}};
matrix2= Table[  Sin[i*j],{i,1,4},{j,1,4}];
MatrixForm[matrix1]
MatrixForm[matrix2]

Again, we can address an individual element using the double square brackets, but now we have to have two indices:

matrix1[[2,3]]

Mathematica can handle lots of matrix manipulations.  Here’s the inverse of a matrix:

Inverse[matrix1]

Mathematica will find exact solutions, often when we don't really want them.  Please note the difference between the two commands below and in their output.  Students often write slow Mathematica code because they forget to tell Mathematica to work numerically and not symbolically.

Inverse[matrix2]
Inverse[N[matrix2]]

Mathematica can solve linear sets of equations, and quickly find eigenvalues and eigenvectors.

LinearSolve[matrix1,vector1]

matrix3=N[matrix1];
Eigenvalues[matrix3]
Eigenvectors[matrix3]

Are the eigenvectors normalized?  Let's check:

eigvec=Eigenvectors[matrix3];
Table[eigvec[[i]].Conjugate[eigvec[[i]] ],{i,1,4}]

Pretty darn close!  Let's chop off anything less that (10^(-10)).

Table[Chop[eigvec[[i]].Conjugate[eigvec[[i]] ]],{i,1,4}]

Combining Plots

The simplest way to combine 2D plots is to give them as a list to the plot command.

Plot[{x,(x^2),(x^3),(x^4)},{x,0,1}]

If you name individual plots, you can also combine them using Show.

plt1=Plot[x,{x,0,1}];
plt2=Plot[(x^2),{x,0,1}];
Show[plt1,plt2]

plt3=Plot3D[Sin[x y],{x,-π,π},{y,-π,π},PlotPoints->40]
plt4=Plot3D[Cos[x y],{x,-π,π},{y,-π,π},PlotPoints->40]
Show[plt3,plt4]