Lab 1  Introduction to Mathematica

Name: 
Student number:

REMEMBER TO SAVE YOUR WORK FREQUENTLY!

Basic Math

In the input box below type
	2 + 2
To evaluate the statement press shift-enter

Evaluate the following arithmetic sequence
	(1/3 - 1/5)*2

Now enter the same sequence using two dimensional format
	((1/3)-(1/5))*2
To do this, either use an input palette or type 1 CTRL-/ 3 CTRL-space to enter the (1/3), similarly for (1/5).

Evaluate (2^3) (use the palette or simply 2 CTRL-^ 3 CONTRL-space)

Evaluate (Sqrt[2]) to 20 figures using N[(Sqrt[2]),20].  A shortcut for the square root is CTRL-SHIFT-2.

Some common mathematical constants are Pi (ESC p ESC), E (ESC e e ESC) and I (ESC i i ESC).  Evaluate e abd π to 6 figures (that's the default number of figures for the N function).

Evaluate the following exactly
	Cos[π]
	Exp[i π]
	Log[(E^3)]

REMEMBER TO SAVE YOUR WORK FREQUENTLY!

Algebra

Enter the following polynomial in x and see how Mathematica evaluates it
	(x^3)+3 (x^2)-2x+1

Nothing much should happen.  Mathematica simply reorders the terms according to their powers.

Try the following
Expand[(((x+3))^2)+x+3]
Factor[(((x+3))^2)+x+3]

You can evaluate expressions for a specific value of the variables by using Mathematica's replacement rules
(((x^2)-12)/(x+15))/.x->4
You can get the arrow by typing - > (minus greater-than)
Replacement rules are very impportant in Mathematica.  We will see them a lot.

To find the intersection between a line
y=x +1 and parabola
y=(((x-3))^2)-2
we use the Solve function
	Solve[x+1==(((x-3))^2)-2,x]

Let's look up the help section on Solve to see that we can also enter the set of equations in the following way
	?Solve
	Solve[{y==x+1,y==(((x-3))^2)-2},{x,y}]
	and also by storing the result 
	sol=Solve[{y==x+1,y==(((x-3))^2)-2},{x,y}]

The Solve function returns the answer in a set of replacement rules, which we have stored in the variable sol.  If we want to store the roots themselves in variables, we have to take a couple of extra steps making use of the replacement rules.  Type
	sol[[1]]
	sol[[2]]
	root1 = x /. sol[[1]]
	root2 = x /. sol[[2]]

The use of double brackets is very important.  They are used to get parts of an expression, variable, array, list, etc.

REMEMBER TO SAVE YOUR WORK FREQUENTLY!

Calculus

Usually when starting a new problem it is worth wiping the slate clean.  Type
	Remove["Global`*"]

Check to see that this has worked by typing
	root1

Mathematica should simply return the name of the variable, and not the value assigned to it in the previous sections.  (Let the lab assistant or instructor know if you do not obtain this result.)

Let us now define a function, recalling how to do so form class
	x[t_]:=(t/(1+(t^4)))

Plot the function by entering
	Plot[x[t], {t, 0, 3}]

Now let's take the first and second derivatives of this function three different ways.  Starting with the "prime" notation that is applicable in the case that our function is a function of a single variable.
	x'[t]
	x''[t]

Let's plot these derivatives (first derivative in blue, second derivative in red).
	Plot[{x'[t], x''[t]}, {t, 0, 3}, PlotStyle -> {Blue, Red}, PlotRange -> {All, All}]

Another way to find these derivatives, defining functions to store them along the way, is the following
	d1[t_] := Derivative[1][x][t]
	d2[t_] := Derivative[2][x][t]

The second line, for example, reads "Take the derivative twice of the function x with respect to its argument and then evaluate it at t".

Let's plot the derivatives
	Plot[{d1[t], d2[t]}, {t, 0, 3}, PlotStyle -> {Blue, Red}, PlotRange -> {All, All}]

Finally, we can use the D function, or equivalnetly (∂_x) from the palette (or ESC pd ESC, then to get a subscript type CTRL-_).  Enter
	?D
	(∂_t)x[t]
	(∂_(t,t))x[t]
	D[x[t],t]
	D[x[t],{t,2}]

As a note of caution, let’s try to define a function as the derivative of x[t] through
	dd1[t_]:=D[x[t],t]

Let's see what happens when we try to plot the derivatives this time
	Plot[dd1[t], {t, 0, 3}, PlotStyle -> Blue, PlotRange -> {All, All}]

This causes an error because the variable t within the plot command has a numeric value.  If you go back and look at the definition of, say, dd1[t], this translates to the equivalent of (let's say t=0.2345)
	D[x[0.2345], {0.2345, 1}]
which doesn't make sense; we're trying to differentiate with respect to 0.2345.  The upshot is that we must be careful of how we define functions, and in general be mindful of what exactly we're asking the computer to do, being mindful of the difference between = and :=

If you wanted to use D instead of Derivative, how might you change the syntax of the function definition?

dd1[t_] INSERT_APPROPRIATE_OPERATOR  D[x[t],t]

Now try to plot dd1[t].

REMEMBER TO SAVE YOUR WORK FREQUENTLY!

Now let's integrate our function, first in general.  From the palette select
	∫■d□
and use it to find the antiderivative to our function x[t].

Notice that their is no constant of integration.  Done correctly, the above operation is equivalent to
	Integrate[x[t], t]

To evaluate a definite integral, enter
	Integrate[x[t], {t, 0, 3}]
	or
	(∫_0^3)x[t]dt (from the palette or ESC dintt ESC)

Notice that the answer is returned as an exact number.  To get a decimal approximation enter
	N[Integrate[x[t], {t, 0, 3}]]
	or
	N[(∫_0^3)x[t]dt]

If the function we're trying to integrate doesn't have an antiderivative in terms of elementary functions, Mathematica will simply spit back the input you gave it.  Try
	(∫_0^3)Exp[x[t]]dt

In such cases, the integral must be done numerically using the NIntegrate function
	NIntegrate[Exp[x[t]], {t, 0, 3}]

Note: The following will try to do the integral symbolically, and upon failure, will call NIntegrate
	Integrate[x[t], {t, 0, 3}] // N
	N[Integrate[Exp[x[t]], {t, 0, 3}]]
	N[(∫_0^3)Exp[x[t]]dt]
	(∫_0^3)Exp[x[t]]dt//N

Kinematics problem

Let's put some elements together.

A particle’s velocity is given in m/s by  v(t) = exp(- ((t^2)/8)) sin(t )-0.2 t .
Tasks:
1) Define a Mathematica function that represents v(t).
2) Plot your function for 0 ≤ t ≤ 3 s.  (Include axes labels with units - look up how to include labels.)
3) Find when the particle first comes to a (momentary) stop.  Store this time in a variable called tstop   [Ans 2.202 s].
4) Find the displacement of the particle from t = 0  to   t = tstop  [0.766 m].
5) Find the acceleration of the particle at t=tstop  [ -0.764 m/(s^2)].

Lab finished!  Include your name and student number at the top of the lab.  Save your work and submit via the P2820 DropBox on your desktop.