Prepared by John Davis, Dan Quinn and Howard Stone MAE305MAT301 Fall 2013: Introduction to Mathematica
First of all, if you have not done so already, download Mathematica from the Princeton University website by following the instructions at https://www. princeton.edu/software/licenses/software/mathematica/. Mathematica can be used for many diﬀerent mathematical topics, from cal culus to typesetting to producing graphics. However, in this introduction we will focus on the mathematical functions of the program, speciﬁcally those that are relevant to diﬀerential equations. If you are interested in the other func tions of Mathematica, you can go to their extensive Documentation Center at http://reference.wolfram.com/mathematica/guide/Mathematica.html, or open the Doctumentation Center within Mathematica by clicking on Help → Documentation Center. These pages have introductions to the various functions in Mathematica as well as helpful tutorials. The few pages we have prepared here are simply meant to get you started. Perhaps by the end of the course you will ﬁnd that this kind of software can be helpful in your course work and future studies.
1.1
Cells
At the most basic level, Mathematica functions like a calculator. Open Math ematica, and a blank “notebook” appears. If you hover the cursor in the note book, you will see that the text cursor will be horizontal. This indicates that entering text in this area will create a new cell. Type in 2 + 2. To evaluate a cell, type together Shift + Enter (if you have a numeric keypad, this is equivalent to just hitting the enter button on the keypad). Doing this will produce an output cell with the result 4. If you want to do calculations on the output, use the % sign to symbolize the previous output. For example, next type %ˆ2 to square the output. However, one of the most useful functions of Mathematica is executing code, which you can do that by entering multiple lines of commands into a cell, and evaluating the cell to run all the commands. Use the semicolon at the end of lines whose output you want to suppress. For example, type
(∗ 
This 
is 
a 
comment 
∗) 
appl e s 
= 140; 

peopl e 
= 
3 8; 

s e rving spp 
= 
appl e s / peopl e 

N[ s e rving spp ] 
Note how to add comments to code using (* and *). This code outputs the exact (simpliﬁed) number of servings per person ( ^{7}^{0} ), but the N function also gives the numerical value (3.68421) in the last line of the code.
19
1
1.2 Formatting
1.3 Functions
A function in Mathematica is almost always the English word of the function with the ﬁrst letter in each word capitalized, followed by square brackets [ ] ^{1} enclosing the input of the function. For example,
ContourPlot [ xˆ2
+
y ˆ2 ,
{x ,
−2,
2} ,
{y ,
−2,
2 } ]
displays a contour plot of the function x ^{2} +y ^{2} from −2 < x < 2 and −2 < y < 2. If you are not certain what function you need, you can ﬁnd it in Help → Function Navigator. In this case, we could also type “Contour”, then hit ctrl+k to get Mathematica to give a list of functions that start with that string of letters, or complete the function automatically if it is the only available one. Once you have typed the name of your function, you can hit ctrl+shift+k to show the template of the function. Do this with ContourPlot. It often gets confusing remembering the syntax of a function you need, and using the easily available “Help” resources saves much time and frustration. A list of functions you might ﬁnd useful for this course is included at the end of this introduction.
1.4 Deﬁning constants, variables, functions, and equations
To deﬁne a constant, just type something like mu = 3.38∗ˆ−7 (scientiﬁc nota tion in Mathematica) to assign the value 3.38×10 ^{−}^{7} to µ. You can now operate
with this constant by typing “mu”. To deﬁne a function, type a command similar to f [x ] := xˆ2 + Sin[x]. Make sure to type the underscore and to use := for the equals sign. Then you can type f[Pi/2], which should output 1 + π ^{2} /2. You can also operate on the function, e.g. Plot[Sqrt[f [x ]], {x, 0, Pi}] to plot the square root of our function from 0 to π.
Equations are deﬁned by using a variable and two
equals
signs,
as
in
xˆ2 == x + 4. Equations can be operated on by certain functions, e.g.
Solve [ xˆ2 
== 
x 
+ 
4 ] 

which solves the quadratic equation for x and outputs the two roots 

{{x −> 
1/2 
(1 
− 
Sqrt [ 1 7 ]) } , 
{x 
−> 
1/2 
(1 
+ 
Sqrt [ 1 7 ]) } } 
which tells us the two solutions, x = ^{(}^{1}^{±} ^{√} ^{1}^{7}^{)}
2
^{1} Notation: Brackets [] are used for arguments of functions, parentheses () are used for mathematical expressions, and curly brackets {} are used to form lists.
2
2.1 Functions from calculus
First of all, you will need to be able to use a variety of common functions, some of which are shown in a table below.
Table 1: Mathematical Functions from Calculus
Mathematica Name 
Function 
Log[x] 
Natural logarithm, ln x 
Log[10,x] 
Common logarithm, log x 
Exp[x] Power[x, y] or xˆy Sqrt[x] 
Exponential function, e ^{x} Power function, x ^{y} Square root function, ^{√} x 
Sin[x], Cos[x], Tan[x] ArcSin[x], ArcCos[x], ArcTan[x] 
sin x, cos x, tan x arcsin x, arccos x, arctan x 
You can try typing Exp[x] and evaluating the cell. Mathematica recognizes the function and outputs e ^{x} .
2.2 Derivatives
Derivatives are evaluated by using the command D[ f , x ]
to take the derivative a function f with respect to the variable x. Higherorder derivatives may be evaluated by using
D[ f , { x , n } ]
to take the nth derivative of the function f with respect to the variable x. For an example, type
D[ ArcTan [ x ] , { x , 4 } ] to take the fourth derivative of the arctan function. This operation outputs
−
48x ^{3}
24x
(1 + x ^{2} ) ^{4} ^{+} (1 + x ^{2} ) ^{3}
A very helpful command for simplifying results and algebra is the Simplify command. For example, performing the previous example with Simplify we would type
Simplif y [ 
D[ ArcTan [ x ] , { x 
, 4 } ] 
] 

and the output is found to be 

− 
24x ^{} −1 + x ^{2} ^{} 

(1 + x ^{2} ) ^{4} 

3 
2.3 Integrals
The indeﬁnite integral ^{} f dx is carried out by the command
I n t e g r a t e [ f , x ]
Type Integrate[xˆ2,x], which will output ^{x} ^{3} . Note that Mathematica does not
3
include the constant of integration so you still have to exercise your knowledge of calculus!
For the deﬁnite integral ^{}
x
x max
min
f dx, use
I n t e g r a t e [ f , { x , x
min
, x
max
} ]
1
For an interesting example, let’s calculate ^{}
0
I n t e g r a t e
[ Log [( 1
+
Sqrt [ 1
+
4
x ]) / 2 ] / x ,
{x ,
0 ,
)dx by typing 1 } ]
2
This example takes a few seconds to evaluate, but outputs ^{π} _{1}_{5} ! As you can see, Mathematica will be able to give a symbolic result even in very complicated cases.
In Mathematica, both analytical and numerical solutions to diﬀerential equa tions can be found. For most purposes in this class, you will want analytical solutions, so that you will have the solution in terms of known functions. Some times, however, a diﬀerential equation cannot be solved analytically, and in these cases, you will need to solve them numerically to see the behavior of their solutions.
3.1 Analytical solutions using DSolve
To ﬁnd symbolic solutions to diﬀerential equations, use the DSolve command using the syntax
DSolve [ { eqn } , y [ x ] , x ] For a system of equations we would type
DSolve [ { eqn1 , eqn2 ,
. . .
} , { y1 [ x ] , y2 [ x ] ,
. . .
}
, x ]
Initial conditions are placed in the curly brackets following the equation, and are separated by commas. For example, to solve y ^{}^{} (x) + y(x) = sin(x) with initial conditions y(0) = 1 and y ^{} (1) = 4 we type
s o l
=
DSolve [ { y ’ ’ [ x ]
+
y [ x]==Sin [ x ] ,
y[0]==1 ,
y ’[1]==4 } ,
y [ x ] ,
x ]
This outputs a long solution since Mathematica is following an algorithm. How ever, if we also use the Simplify command the solution is much cleaner. Try it!
4
Important: When you deﬁne the diﬀerential equation you need to use two equal signs so that Mathematica recognizes it as an equation. If you were to try to operate on the variable sol above, maybe to plot it, Mathematica would give you an error since the solution for DSolve is in the wrong format. You need to use the /. operator, which applies a substitution rule to the preceding function. In this case the substitution rule you want is the output of sol, so you would type
Plot [ y [ x ]
/ .
s ol ,
{x ,
0 ,
10 }]
Again, try these steps. The plot should look like this:
Figure 1: Plotting an analytical solution of a diﬀerential equation.
3.2 Numerical solutions using NDSolve
We will not have time to do much with numerical solutions in this course, but we will try to provide a short introduction where it makes sense in the lectures and examples. To use Mathematica tools to ﬁnd numerical solutions to diﬀerential equations, use the NDSolve command with the syntax
NDSolve [ eqns , u , {t , tmin , tmax } ]
This command uses standard algorithms to generate a numerical solution to the ordinary diﬀerential equations indicated as eqns for the function u with the independent variable t in the range t _{m}_{i}_{n} to t _{m}_{a}_{x} . For an example, consider the nonlinear ﬁrstorder ODE y ^{} = y[cos(x + y)] with initial condition y(0) = 1. Type in
s o l 
= 
NDSolve [ { y ’ [ x 
] 
== y [ x ] 
Cos [ x 
+ y [ x ] ] 
, 
y [ 0 ] 
== 
1 } , 
y , 
{x , 
0 , 
3 0 } ] ; 

Plot [ Evaluate [ y [ x ] 
/ . 
s o l ] 
, 
{x , 
0 
, 
30} , PlotRange −> All ] 
These steps solve the equation numerically from 0 < x < 30. Notice that you need to include the Evaluate command since you are evaluating the numerical
5
results rather than a known symbolic solution. The PlotRange command simply automatically adjusts the plot’s range to include all values of the function. The plot should look like this:
Figure 2: Plotting a numerical solution of a diﬀerential equation.
4.1 
Constants 
You can introduce constants by their name, or you can also press escape, type a shortcut, then press escape again. 

Pi or [esc]p[esc]: π 

E or [esc]ee[esc]: The exponential constant e 

Inﬁnity or [esc]inf[esc]: ∞ 

I or [esc]ii[esc]: The imaginary number, i 

Using the escape method keeps your Mathematica code clean and intuitive to read. Try experimenting with other shortcuts, for example 

[esc]int[esc]: ^{} 

[esc][any letter][esc]: α, β, χ, etc. 

[esc]pd[esc]: ∂ 

4.2 
Functions 
Simplify[expr]: For simplifying an expression.
6
Series[f, {x, x0, n}]:
point x0.
For expanding a function f[x] to the n’th term at the
Solve[expr,vars]: For solving an expression for a certain variable.
DSolve[eqn,y,x]: For solving a diﬀerential equation with solution y(x)
NDSolve[eqn, y,{x, x _{m}_{i}_{n} , x _{m}_{a}_{x} }]: For numerically solving a diﬀerential equa tion from x _{m}_{i}_{n} to x _{m}_{a}_{x} .
D[f,x]: For taking the partial derivative of f with respect to x.
Integrate[ f ,x]: For the indeﬁnite integral of f with variable x.
Integrate[f,{x,xmin,max}]: For the deﬁnite integral of f(x) integrating from
x min to x max .
Plot[f,{x,xmin,xmax}]: For plotting a function f(x) from x _{m}_{i}_{n} to x _{m}_{a}_{x} .
ParametricPlot[{fx, fy}, {u, umin, umax}]: For plotting a parametric curve with coordinates f _{x} and f _{y} as a function of u. This command is useful for phase plane plots where f _{x} = y(x) and f _{y} = y ^{} (x).
In the second half of the course, we will look at partial diﬀerential equations. You do not need to know this now, but it could be helpful later in the class and if you take MAE 306, so save this tutorial somewhere you can ﬁnd it! The diﬀerence between an ordinary diﬀerential equation (ODE) and a par tial diﬀerential equation (PDE) is that the unknown function in a PDE is a multivariable equation, and the PDE contains its partial derivatives. An exam ple is the Laplace equation, which you may have seen before (and you will see it many more times so get used to it)
∂ ^{2} u ∂x ^{2} ^{+}
∂ ^{2} u
_{∂}_{y} _{2}
= 0,
which is equivalent in shorthand as u _{x}_{x} + u _{y}_{y} = 0.
To create PDEs in Mathematica, you must use the D function, which takes derivatives. Typing D[xˆ3, x] outputs 3x ^{2} . For example, if we wanted to plot
2
∂y
∂x ^{+} ∂y
∂t
= sin(x) with initial condition y(0, t) = cos(t) we would type:
pde
= D[ y [ x ,
t
]
,
t ]
+
2
D[ y [ x ,
t ]
,
x ]
==
Sin [ x ] ;
s o l
=
DSolve [ {
pde ,
y [ 0 ,
t ]
== Cos
[
t ] }
,
y [ x ,
t ]
,
{x ,
t } ]
7
First we assign our PDE to the “pde” variable using the D function, then use DSolve just as we did for ODEs. These steps give an output in terms of the function y(x, t), just the same way it did for ODEs, but with another variable. To plot this function taking into account the extra variable, time, we need to use the Plot3D function and make sure to deﬁne our domain for each variable.
PlotRange −> All ]
Figure 3: Plotting the solution to a partial diﬀerential equation.
Now that you know how to deﬁne a PDE, you can also solve it using NDSolve the same way you would for an ODE.
8
Much more than documents.
Discover everything Scribd has to offer, including books and audiobooks from major publishers.
Cancel anytime.