Intro to Mathematica: PHYS-1205

Copyright Kieran Mullen 2006

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])]

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}]

We can define vectors as a list of elements.  A list is a set enclosed in curly brackets.

vectorA={Ax,Ay,Az}
vectorB={Bx,By,Bz}

The vectors above are symbolic.  We could just as easily defined a vector as a list of numbers.  Once we have a pair of vectors we can take dot products:

Dot[vectorA,vectorB]

We can take cross products:

vectorC=Cross[vectorA,vectorB]

foo=Dot[vectorA,vectorC]

Hmm.... Mathematica didn't simplify this for us.  Let's tell it to simplify the result.

Simplify[foo]

That's what we expect!

Mathematica and derivatives

Mathematica can take derivatives in a symbolic fashion.  Taking  a derivative is a `function'  just like taking a sine or cosine of  a number.  The difference is that the argument is not a simple  number but a function itself.  In addition, we have to tell the program the variable we are `deriving' - are we taking a derivative with respect to time, position or what?  Given that the symbol "f" has been assigned to a function, for example:

f =  ((Sin[x])/(1+(x^2)))

You can take the derivative of f either by:

(∂_x)f

or one can write:

D[f,x]

which makes it look more like a function similar to Sin[x] or Cos[x].  Click on each of the following examples, and hit shift-return.  (Can you state the answer before having Mathematica tell you its  solution?)

D[(x^3),x]

(∂_x) ((x^3)+y)

D[(x^n),x]

(∂_x) (Sqrt[(x^2)+(y^2)+(z^2)])

(∂_x) (((Sqrt[(x^2)+(y^2)])-(Sqrt[(x^2)-(y^2)]))/((Sqrt[(x^2)+(y^2)])+(Sqrt[(x^2)-(y^2)])))

Simple 2D plots

Mathematica is great for plotting lines, curves and surfaces.  Again, the command will be a function called `Plot' (logically  enough).  It will take as arguments the function to be plotted,  the dependent variable, and the range to be plotted.
 
    Warning:  Mathematica is very handy at symbolic manipulation.  But when it comes to making a plot, it wants to plot numbers, not symbols!  This means you cannot plot the functions   k  (x^2)  or Sin[k x]  as a  function of x without specifying the value of k.
 
     Here are a few fun plots:

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

Plot[(x^3)-2 (x^2)+x,{x,0,2}]

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

Plot[Sin[(1/x)],{x,-1,1}]

If you want to plot more than one function on a graph, you need to put them in a "List".

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

If you want, you can get quite fancy.

Plot[{x,(x^2),(x^3),(x^4)},{x,0,1},PlotStyle->{{RGBColor[1,0,0]},{RGBColor[0,0,0],Dashing[{0.05`,0.05`}]},{RGBColor[0,0,1]},{RGBColor[0,1,1]}},FrameLabel->{"Sound of One Hand Clapping","Color of Thunder"},ImageSize->300,PlotLabel->"Ice Cream Koan",RotateLabel->True,Frame->True]

Simple 3D plots

Plotting in 3D isn't much harder. You just have to specify  a range of x and y values.  Below are a few cute examples.

Plot3D[(x^3)+(y^3),{x,-2,2},{y,-2,2}]

Plot3D[Sin[x Sin[x y]],{x,0,4},{y,0,3},PlotPoints->30]

Plot3D[-((x^2)+(y^2))+40 Cos[(Sqrt[(x^2)+(y^2)])],{x,-15,15},{y,-15,15},PlotPoints->30]

Of  course, if you want to get really fancy.... Try the next plot.  Be sure to evaluate the first line - it loads the software you need  to plot the next graph.  It may take a minute or two to run, though.

Needs["Graphics`ParametricPlot3D`"]
SphericalPlot3D[2+Sin[3 t] Sin[3 p],{t,0,π},{p,0,2 π},PlotPoints->{30,45}]

Recall our function from above:

    f[x]=(x^4)+(x^3)-4 (x^2)-x+1
    
 Let's tell Mathematica that we're denoting this function by the symbol "fred".

fred=(x^4)+(x^3)-4 (x^2)-x+1

We can plot it:

Plot[fred,{x,-2,2}]

Where are the minima and maxima of fred?   We need to solve for where the derivative is zero.

Solve[D[fred,x]==0,x]

Ugh!  This exact mathematics obscures the answer.  Let's tell it that we are looking for a solution to a numerical equation.

bob=N[fred]
Solve[D[bob,x]==0,x]

Look and see if they line up with the points on the graph.

Mathematica cares about Capitalization

Every function or command in Mathematica begins with a Capital Letter.  Any English word in the command will have an internal Capital Letter.  The function for a contour plot is ContourPlot, not Contourplot.  Mathematica will interpret any incorrect spelling or capitalization as a definition of some new command.  For example, only one of the following is correct.  Which one?

arcsin[1]
Arcsin[1]
ArcSin[1]

Mathematica is a symbolic manipulator.

What is the value of Sin[(π/4)] ?

a=Sin[(π/4)]

Mathematica will keep this symbolic expression, unless you tell it to evaluate it as a number:

N[a]

The two expressions below are not equivalent to Mathematica: one is a collection of symbols, and one is a number.

10/7
10./7

Mathematica never forgets

Mathematica remembers everything you've executed.  Even if you've deleted the cell or forgotten about the command, it will remember it (until you either restart the kernel or tell it explicitly to forget something).   Now, let's say you want to solve the quadratic equation:

Solve[{a (x^2)+b x+c ==0},{x}]

Oops!  We don't get the quadratic formula.  We set a numerical value to "a" in the previous section.  We have to clear the value of "a"

Clear[a];
Solve[{a (x^2)+b x+c ==0},{x}]

Assignment versus equals

Look at the two expressions below.  The first assigns the value "0" to the variable "zero".  Whenever you type the variable name, Mathematica just substitutes in the value you assigned.  The second expression is an equation.  It is a logical relation.  It's not a rule for substitution, but a relationship that you can use in solving systems of equation.

zero=0
(y1^2) + 3 == 0

Plotting against you

Mathematica can only plot numbers.  If any part of your expression is not defined, it won't be able to plot it.

y=A Sin[k x-w t];
A=2;
k=π;
w=2;
Plot[y,{x,0,2}]

Set the value of t above and then try it again.

Variable blindness

If you don't leave spaces between your variables, Mathematica just assumes that this is some new variable.  Note that "mg" is not defined, but "m g" is defined.

m=2;
g = 9.8;
mg
m g