Mathematica Primer (the first one.)

This notebook has been written in Mathematica by 

Mark J. McCready
Professor and Chair of Chemical Engineering
University of Notre Dame
Notre Dame IN 46556
USA

mjm@nd.edu
http://www.nd.edu/~mjm/


It is copyrighted to the extent allowed by whatever laws pertain to the World Wide Web and the Internet.

I would hope that as a professional courtesy that this notice remain visible to other users. 
There is no charge for copying and dissemination   

Version:  8/20/00
More recent versions of this notebook should be available at the web site:
http://www.nd.edu/~mjm/Mathematicaprimer.1.nb

Basic Mathematica commands

Arithmetic

A system of such colossal power probably can do simple mathematics

2+2

Note that for multiplication, there is no need for a sign.  Just use a space.

3 4

Don't forget the space.

34

You can use a * if you wish.

3*4

This one is fun

35/17

Mathematica does not switch to floating point, which is inherently less precise, unless you want it to.

We could do N[%], which says make the previous result floating point

N[%]

Or

N[35/17]

It gives

2^10

or

3^19

Vectors and Matrices

A vector is

{a,b,c}

Another is

{d,e,f}

Take the "dot" product of these.

{a,b,c}  .{d,e,f}

This looks good for the scalar multiplication of vectors.  We can also do:

Dot[{a,b,c},{d,e,f}]

One kind of vector product is:

Cross[{a,b,c},{d,e,f}]

The normal cross product can also be obtained from the determinant of a matrix as you will see below.

You can also do

{a,b,c} {d,e,f}

But this multiplication does not arise in any physical problems that we will be considering this semester.  It could be useful for numerical solution techniques.

We would like to make a matrix that Mathematica can understand.  Use an opening {{ then close each row with } and then open the next with { .......  after the last row you obviously need a closing }.  The one that is defined here has the name "a"

a={{1,2,3},{7,5,6},{1,6,9}}

a+a

The matrix a is a 3 by 3 square matrix. 

A matrix could correspond to many different physical entities.  For example a 3 by 3 could be the elements in a second order tensor which corresponds to the stresses in a fluid at a point and some time.  Of course, if this were the case, the matrix would be symmetric for all simple fluids. (Yes, this course will be complicated at times!)

How can we get a symmetric matrix from a and what is "symmetric". 

First define transpose, which is a switching of a[i,j] --> a[j,i]

Transpose[a]

You see that we left the diagonal elements unchanged and "reflected" the off diagonal elements across the diagonal.  For example elements a[i,j] , where i is row and j is column were transformed to a[ j,i]

a[[1,2]]

a[[2,1]]

Now we can easily get the symmetric part of a (such that a[i,j]=a[j,i] and (of course) its antisymmetric piece.

asymm=(1/2) (a+Transpose[a])

aanti=(1/2) (a-Transpose[a])

We see that the antisymmetric part does not have any diagonal elements because a[i,i] cannot be equal to -a[i,i].  Thus our definition of an anti symmetric matrix is that a[i,j]=-a[j,i].

Symmetric and antisymmetric matrixes are useful, for example, in defining the basic elements of fluid motion in a study of "kinematics".  It is convenient that

asymm+aanti

thus we have recovered a again.

We need to clear the value of a because we use it again below.

Clear[a]

a

The determinant is a standard operation.  You recall

Det[{{a,b},{c,d}}]

As mentioned above, the cross product can be defined with the determinant

Det[{{ex,ey,ez},{a,b,c},{d,e,f}}]

{Coefficient[%,ex],Coefficient[%,ey],Coefficient[%,ez]}

Where we had to work a little too hard with the {ex,ey,ez} being a unit vector and the Coefficient being used to recover the components of each direction.

Cross[{a,b,c},{d,e,f}]

From these operations we see that addition and subtraction of matrices is done by simple addition and subtraction of corresponding elements.

Making plots, special functions and Series expansion

It knows all of the functions, including yours and my favorite orthogonal polynomials.

Plot[ChebyshevT[3,y],{y,-1,1}];

Plot[ChebyshevT[8,y],{y,-1,1}];

You have already figured out that if the first number in the argument, is odd, the polynomial is an odd function.  If it is even, the polynomial is an even function.

From math you may already recall:

(∫_(-1)^1)ChebyshevT[2,y] ChebyshevT[1,y] (1/(Sqrt[1-(y^2)]))\[DifferentialD]y

2/π Integrate[ChebyshevT[8,y] ChebyshevT[3,y] (1/(Sqrt[1-(y^2)])),{y,-1,1}]

Was this magic?

2/π Integrate[ChebyshevT[4,y] ChebyshevT[7,y] (1/(Sqrt[1-(y^2)])),{y,-1,1}]

This is also magic.

2/π Integrate[ChebyshevT[4,y] ChebyshevT[4,y] (1/(Sqrt[1-(y^2)])),{y,-1,1}]

One of the most fun things to do is expand things.

Series[BesselJ[0,x],{x,0,5}]

A secret command that you always need with "Series" is "Normal".  Normal turns the expression with the error term into just a regular expression.

b5=Normal[Series[BesselJ[0,x],{x,0,5}]]

Try some other lengths

b3=Normal[Series[BesselJ[0,x],{x,0,3}]]

b9=Normal[Series[BesselJ[0,x],{x,0,9}]]

b13=Normal[Series[BesselJ[0,x],{x,0,13}]]

Do you see the pattern?

Plot[{BesselJ[0,x], b3,b5,b9,b13},{x,0,5}];

Which is the real function?s

Plot[{BesselJ[0,x]},{x,0,5},PlotStyle->Dashing[{.1,.05}]];

This is not as convenient as Matlab for sure:

Plot[{BesselJ[0,x], b3,b5,b9,b13},{x,0,5},
PlotStyle->{RGBColor[1,0,0],RGBColor[1,1,0],RGBColor[1,0,1],
RGBColor[1,.5,.5], RGBColor[0,1,0],RGBColor[0,0,1]}];

We can always do white!

Plot[{BesselJ[0,x]},{x,0,5},PlotStyle->RGBColor[1,1,1]];

Algebraic equations

In order to solve some equations, we need to make an equation.  This might seem simple.

x+ y = 2
x-y=4.

However in Mathematica we do not do this with just an equal sign.  An equal sign in Mathematica means the thing on the left is replaced by the thing on the right -- when ever you type the thing on the left.  

For example

b=6

b

b^2

Sqrt[b]

Since we might use the symbol b elsewhere here or in another open Mathematica notebook, I need to unassign the value of 6 to b.

Clear[b]

b

To make equations, use ==.

eq1=x+y ==2

eq2=x-y==4

eq1

eq2

Now how can we solve these?

Solve[{eq1,eq2},{x,y}]

Solve is fairly powerful

Solve[x^3+14 x^2-12x+3 ==0,x]

Or even:

Solve[x^4+6 x^3+14 x^2-12x+3 ==0,x]

Or a little harder problem:

Solve[{x^2-4==0, y^2+2x +5z==0,y^2-2z^2+x y +9==0},{x,y,z}]

There are 6 solutions.

N[%]

Differential Equations

If you would like to solve a differential equation, DSolve is for you.  Check the help browser for syntax.

DSolve[y''[x]- y[x]==0,y[x],x]

A more general equation is

DSolve[y''[x]+a y'[x]+b y[x]==0,y[x],x]

An equation that you will need to solve later in the semester for flow between parallel plates, where one is moving and the other is stationary is

DSolve[{v''[y]==0,v[0]==0, v[b]==U},v[y],y]

The resulting velocity profile is linear.

Homework set #1.

Please solve these problems using Mathematica.  The examples above should be sufficient to solve them, or look at the "Help Browser" or send email to someone who can help.

1.  Take the dot product of two orthogonal (3 element) vectors that you make up.  Make sure you get the right answer.  Now give the answer for the cross product.  (Did you use the right hand or the left hand rule to get this answer?  I'll bet you did not know that your answer is not really a vector, it is a pseudo-vector)
2.  (({{2, 4}, {4, 17}})) + (({{0, 3}, {7, 1}})) = ?
3.  Plot the sine and cosine functions to show their periodicity (ie. change this) and relative phase.
4.  Integrate and then figure out how to take the derivative of both the sine and cosine.  
5.  Are the sine and cosine orthogonal functions?  Show this with Mathematica.
6.  Which differential equation from your past or future will be solved by sine and or cosine functions?