261 Mathematica Session 2

As in the last session, this notebook has a collection of cells which you should read and evaluate.
There are also problems to solve, for which you will create and evaluate your own cells.

Be sure to switch control of the keyboard and mouse regularly, so that all group members can be equally frustrated.

Clear symbols

In order to avoid interference from symbols defined in other notebooks, we first Clear all symbols.  We assume that the relevant symbols are in the Global` context.

Clear["Global`*"]

We put this here so you can re-evaluate this cell when things are going all wrong.

Review

Here are some things to recall from last time.
	  Do you remember the significance and use of the various parentheses: [square] (round) {squiggly}?
	  What is the difference between these:  x = 2  and x == 2?
	  How about making a Plot of a function?  Let's take Tanh[x] to be specific.
	  And finally, try using Solve to find an angle such that Cos[θ] = Tan[θ]

Math worksheet stuff

The math worksheet portion of the homework asks you to sketch a few functions and take some derivatives.  Of course the goal is for you to be able to do it "by hand", but you are always free to ask Mathematica for the answer and then figure out why you always knew that.
As a warmup, here is some random function:

f[x_] = (x^2 -1)/(x-3)

First ask yourself: where are the zeroes and singularities? How does the function behave for large |x|?

Then here is how to ask Mathematica for that information:

Solve[f[x]==0,x]

Solve[1/f[x]==0,x]

Limit[f[x]/x,x->Infinity]

With that information, try making a sketch by hand, then check using Plot[].
Likewise with any of the homework you care to take on here and now.

Substitution Rules

Here is a new Mathematica-ism for the day:  the slashdot.  Often we have an expression involving a variable or two and we'd like
to know the value of the expression when we plug in specific values.  Here is how to do that quickly and without lasting side-effects:

q^2 /. q-> 3

Or maybe there are several parameters we want to substitute:

(m1 + m2) g /. {m1->m, m2->2m, g->9.8}

Simultaneous Equations

As we attack more complicated physics problems we typically end up with a whole set of unknowns (e.g. accelerations, tensions, ...)
and a whole set of relationships (F=ma, constraints, ...) which we need to solve simultaneously.  Mathematica's Solve[] can handle it.
Here is a set of simultaneous linear equations.  Note that the single = sign
is used to assign a name to the equation, but the equation itself has the double == sign.

eqn1 = 4 x + 3 y - z == 3

eqn2 = 2 x - y + z == 3

eqn3 = x - y -z == 0

solutionrule = Solve[{eqn1,eqn2,eqn3},{x,y,z}]

Note the syntax: we give a list of equations to be satisfied, and then a list of the variables
we are solving for.
Now check the solution by plugging back in:

4 x + 3 y  - z /. solutionrule

Or check all three at once this way:

{eqn1,eqn2,eqn3} /. solutionrule

Now you try.  Solve  2x + 2y + z = -1;  x + 2y -z = 2 ;  y+z =0.

Differential Equations

Mathematica can solve not only ordinary algebraic equations, but also differential equations.
What is a differential equation?  It is an equation which gives a relationship involving the
derivatives of a function.  The solution to a differential equation is not just a number, it is a
whole function.  For example, what function y[x] is it which satisfies:  dy/dx = x^2?
Must be y[x] = (1/3) x^3 plus a constant, right?
Here is the incantation to get Mathematica to give the answer:

DSolve[y'[x]== x^2,y[x],x]

Again note the syntax: first we give the equation, where the prime on y'[x] indicates a deriviative,
then we give the thing we are looking for (y[x]), and then we specify the independent variable (x).
In giving the answer Mathematica has created a new constant for us (C[1]).  In a given problem,
the value of this constant will be determined from other information.  E.g. if we are told that the
particular y[x] we seek satisfies y[1] == 1, then what must the constant be?
Here is how we can get Mathematica to find the particular solution:

DSolve[{y'[x]==x^2, y[1]==1},y[x],x]

Another Differential Equation

Some differential equations give a relationship between the derivatives of a function and the
function itself.  For example, what function y[x] is its own derivative, i.e. solves y'[x] == y[x].
To get a specific solution lets also demand that y[1] = 1.

DSolve[{y'[x]==y[x],y[1]==1},y[x],x]

Ballistic Motion

Suppose that at time t=0 a ball is thrown straight up in the air.  Its initial height is z[0] == z0,
and its initial velocity is z'[0] == v0.  Its acceleration is of course z''[t] = -g.
Have Mathematica give you the full form of z[t].

Parametric Plots

The function Plot[] gives us a sketch of y=f(x).  Sometimes we want to trace out the trajectory as a vector {x(t),y(t)} varies with time.
For this the recipe is ParametricPlot. For example, here is a projectile:

x = 5 t
y = 4 + 3 t - 2 t^2
ParametricPlot[{x,y},{t,0,3}]