Introduction to Mathematica and Differential Equations
Math 260 Lab #1

The purpose of today's lab is to get acquainted with Mathematica by doing some basic calculations and plotting.  You don't have to submit any formal work for today's lab, but in future labs you will need to email me your completed worksheet. You may work in pairs if you wish.

Three rules of thumb to remember about Mathematica:

1) Commands and functions begin with capital letters.  Also, constants like Pi are capitalized.

2) Use square brackets with commands and functions (not parentheses).  For example, Cos[x] or Exp[x^2].

3) Spaces imply multiplication (or use asterisks, except with matrices and vectors).

Work through the examples below to get the hang of Mathematica.

Example 1.  Evaluate the command line below by putting the cursor anywhere on that line and pressing shift-enter.  Note that saying 2 space Pi means 2 times Pi.  This command plots the graph of y=cos(x) for x from 0 to 2π.

Plot[Cos[x],{x,0,2 Pi}]

Example 2.  Put your cursor just below this cell so that a long horizontal line appears, then click.  This  creates a new command line.  Have Mathematica multiply 345 by 29.  Don't use an asterisk--just put a space between the numbers, and hit shift-enter to evaluate.  To multiply negative numbers or do other arithmetical grouping, use parentheses: (-345)(29+12).

Example 3.  Find the square root of 4489 by using the command Sqrt[4489].  Also find the square root of 4500.  Note that Mathematica gives the answer in exact form.  If you want an decimal approximation, use N[number] (N stands for numerical approximation), e.g., N[Sqrt[4500]].

Example 4.  Mathematica can do some algebraic and trigonometric simplification.  For example, see what happens if you ask for the cosine of the arcsine of x:  Cos[ArcSin[x]].

Example 5.  To evaluate ex, write Exp[x]; to find the natural logarithm, use Log[x].  Try finding decimal approximations for e3 and for ln4 (natural log of 4).

Example 6.  For some of the geometrical analysis we will be doing, it will be handy to plot functions in Mathematica.  Imitate the plotting command in Example 1 to plot the following curves:

(a) y=sin(x) for 0≤x≤4π (note the Plot command only takes the function, not the “y=” part, and sin(x) is Sin[x] in Mathematica)

(b) y=ln(4x) for 1≤x≤5 (remember to use Log[...])

(c) y=arccos(x) for -1≤x≤1

(d)  y=ex for -2≤x≤2 (remember to use Exp[...])

Example 7.  To plot multiple graphs on the same set of axes, put the functions inside curly braces, as in the following example:

Plot[{Sin[x],Cos[x],1-x},{x,0,2 Pi}]

Plot the square root of x and x squared on the same plot.

Example 8.  Mathematica can take derivatives; use the command D[f[x],x], for example, D[Sin[x],x].  Try taking the derivatives of Exp[x^2] and of Sin[x]Cos[x].

Example 9.  Mathematica can also solve differential equations.  For simple DEs it can find explicit solutions, but for most it can only find numerical approximations to the solutions.  If you don't give an initial condition, it gives the general solution (with a constant C[1]).  The Clear command clears the values of the variables and can prevent trouble. Hit shift-enter on each of the following commands to see what Mathematica does:

Clear[x,t]

DSolve[D[x[t],t]==3 x[t]+5,x[t],t]

DSolve[{D[x[t],t]==3 x[t]+5,x[0]==1},x[t],t]

sol=NDSolve[{D[x[t],t]==Sin[x[t]],x[0]==1},x[t],{t,0,10}]
Plot[x[t]/.sol,{t,0,10},PlotRange->All]

Example 10. Find and plot a solution to the DE x'(t)=x(3-x) with initial condition x(0)=5.