You are on page 1of 8

Prepared by John Davis, Dan Quinn and Howard Stone MAE305-MAT301 Fall 2013: Introduction to Mathematica

  • 1 The Basics

First of all, if you have not done so already, download Mathematica from the Princeton University website by following the instructions at https://www. Mathematica can be used for many different mathematical topics, from cal- culus to typesetting to producing graphics. However, in this introduction we will focus on the mathematical functions of the program, specifically those that are relevant to differential equations. If you are interested in the other func- tions of Mathematica, you can go to their extensive Documentation Center at, 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 find that this kind of software can be helpful in your course work and future studies.



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







appl e s




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 (simplified) number of servings per person ( 70 ), but the N function also gives the numerical value (3.68421) in the last line of the code.



  • 1.2 Formatting

  • 1.3 Functions

A function in Mathematica is almost always the English word of the function with the first 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} ,

{y ,


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 find 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 find useful for this course is included at the end of this introduction.

  • 1.4 Defining constants, variables, functions, and equations

To define a constant, just type something like mu = 3.38ˆ7 (scientific nota- tion in Mathematica) to assign the value 3.38×10 7 to µ. You can now operate

with this constant by typing “mu”. To define 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 defined by using a variable and two





xˆ2 == x + 4. Equations can be operated on by certain functions, e.g.

Solve [ xˆ2




4 ]

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

{{x >



Sqrt [ 1 7 ]) } ,






Sqrt [ 1 7 ]) } }

1.2 Formatting 1.3 Functions A function in Mathematica is almost always the English word of the

which tells us the two solutions, x = (1± 17)


1 Notation: Brackets [] are used for arguments of functions, parentheses () are used for mathematical expressions, and curly brackets {} are used to form lists.


  • 2 Operations From Calculus

    • 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



Natural logarithm, ln 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. Higher-order 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


(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



1 + x 2


(1 + x 2 ) 4



  • 2.3 Integrals

The indefinite 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


include the constant of integration so you still have to exercise your knowledge of calculus!

For the definite integral


  • x max


f dx, use

  • I n t e g r a t e [ f , { x , x


, x


} ]


For an interesting example, let’s calculate


1 x ln (1 + √ 1+4x 2
x ln (1 + √ 1+4x

I n t e g r a t e

[ Log [( 1


Sqrt [ 1



x ]) / 2 ] / x ,

{x ,

0 ,

)dx by typing 1 } ]


This example takes a few seconds to evaluate, but outputs π 15 ! As you can see, Mathematica will be able to give a symbolic result even in very complicated cases.

  • 3 Solving Differential Equations

In Mathematica, both analytical and numerical solutions to differential 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 differential 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 find symbolic solutions to differential 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!


Important: When you define the differential 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:

Important : When you define the differential equation you need to use two equal signs so

Figure 1: Plotting an analytical solution of a differential 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 find numerical solutions to differential 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 differential equations indicated as eqns for the function u with the independent variable t in the range t min to t max . For an example, consider the nonlinear first-order 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 ,



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


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:

results rather than a known symbolic solution. The PlotRange command simply automatically adjusts the plot’s range

Figure 2: Plotting a numerical solution of a differential equation.

  • 4 Useful Mathematica Commands



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

Infinity 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][any letter][esc]: α, β, χ, etc.





Simplify[expr]: For simplifying an expression.


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 differential equation with solution y(x)

NDSolve[eqn, y,{x, x min , x max }]: For numerically solving a differential equa- tion from x min to x max .

D[f,x]: For taking the partial derivative of f with respect to x.

Integrate[ f ,x]: For the indefinite integral of f with variable x.

Integrate[f,{x,xmin,max}]: For the definite integral of f(x) integrating from

x min to x max .

Plot[f,{x,xmin,xmax}]: For plotting a function f(x) from x min to x max .

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).

  • 5 Optional - Partial Differential Equations

In the second half of the course, we will look at partial differential 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 find it! The difference between an ordinary differential equation (ODE) and a par- tial differential 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 xx + u yy = 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



∂x + ∂y


= sin(x) with initial condition y(0, t) = cos(t) we would type:


= D[ y [ x ,




t ]



D[ y [ x ,

t ]


x ]


Sin [ x ] ;

s o l


DSolve [ {

pde ,

y [ 0 ,

t ]

== Cos


t ] }


y [ x ,

t ]


{x ,

t } ]


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 define our domain for each variable.

Plot3D [ y [ x , t ] / . s ol , {x , −10,
Plot3D [ y [ x ,
t ]
/ .
s ol ,
{x ,
10} ,
{t ,
5} ,
This plot should look like:

PlotRange > All ]

Figure 3: Plotting the solution to a partial differential equation.

Now that you know how to define a PDE, you can also solve it using NDSolve the same way you would for an ODE.