You are on page 1of 8

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

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. princeton.edu/software/licenses/software/mathematica/. Mathematica can be used for many dierent mathematical topics, from calculus to typesetting to producing graphics. However, in this introduction we will focus on the mathematical functions of the program, specically those that are relevant to dierential equations. If you are interested in the other functions 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 Mathematica, and a blank notebook appears. If you hover the cursor in the notebook, 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 i s a comment ) apples = 140; people = 38; servingspp = apples / people N[ s e r v i n g s p p ] Note how to add comments to code using (* and *). This code outputs the exact (simplied) number of servings per person ( 70 19 ), but the N function also gives the numerical value (3.68421) in the last line of the code. 1

1.2 1.3

Formatting 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 x2 + 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

Dening constants, variables, functions, and equations

To dene a constant, just type something like mu = 3.387 (scientic notation in Mathematica) to assign the value 3.38107 to . You can now operate with this constant by typing mu. To dene a function, type a command similar to f [x ] := x2 + 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 dened by using a variable and two equals signs, as in x2 == x + 4. Equations can be operated on by certain functions, e.g. S o l v e [ x 2 == x + 4 ] which solves the quadratic equation for x and outputs the two roots {{ x > 1/2 ( 1 S q r t [ 1 7 ] ) } , { x > 1/2 ( 1 + S q r t [ 1 7 ] ) } } which tells us the two solutions, x =
(1 17) 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

Operations From Calculus


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] Exponential function, ex Power[x, y] or xy Power function, xy Sqrt[x] Square root function, x Sin[x], Cos[x], Tan[x] sin x, cos x, tan x ArcSin[x], ArcCos[x], ArcTan[x] arcsin x, arccos x, arctan x You can try typing Exp[x] and evaluating the cell. Mathematica recognizes the function and outputs ex .

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 48x3 (1 +
4 x2 )

24x (1 + x2 )
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 S i m p l i f y [ D[ ArcTan [ x ] , { x , 4 } ] ] and the output is found to be 24x 1 + x2 (1 + x2 ) 3
4

2.3

Integrals
f dx is carried out by the command

The indenite integral Integrate [ f , x ]

Type Integrate [x2,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! xmax f dx, use For the denite integral xmin I n t e g r a t e [ f , { x , x min , x max } ] For an interesting example, lets calculate
1 1 0 x

ln (1 +

1+4x )dx 2

by typing

I n t e g r a t e [ Log [ ( 1 + S q r t [ 1 + 4 x ] ) / 2 ] / x , { x , 0 , 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.
2

Solving Dierential Equations

In Mathematica, both analytical and numerical solutions to dierential equations 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. Sometimes, however, a dierential 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 dierential 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]==S i n [ x ] , y [0]==1 , y [ 1 ] = = 4 } , y [ x ] , x ] This outputs a long solution since Mathematica is following an algorithm. However, if we also use the Simplify command the solution is much cleaner. Try it! 4

Important: When you dene the dierential 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 P l o t [ y [ x ] / . s o l , {x , 0 , 1 0 } ] Again, try these steps. The plot should look like this:

Figure 1: Plotting an analytical solution of a dierential 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 dierential 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 dierential equations indicated as eqns for the function u with the independent variable t in the range tmin to tmax . For an example, consider the nonlinear rst-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 } ] ; P l o t [ E v a l u a t e [ y [ x ] / . s o l ] , { x , 0 , 3 0 } , PlotRange > A l l ] 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 plots range to include all values of the function. The plot should look like this:

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

4
4.1

Useful Mathematica Commands


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

Series[f , {x, x0, n}]: For expanding a function f [x] to the nth term at the point x0. Solve[expr,vars ]: For solving an expression for a certain variable. DSolve[eqn,y,x]: For solving a dierential equation with solution y (x) NDSolve[eqn, y ,{x, xmin , xmax }]: For numerically solving a dierential equation from xmin to xmax . D[f,x]: For taking the partial derivative of f with respect to x. Integrate [ f ,x]: For the indenite integral of f with variable x. Integrate[f,{x,xmin,max}]: For the denite integral of f (x) integrating from xmin to xmax . Plot[f,{x,xmin,xmax}]: For plotting a function f (x) from xmin to xmax . ParametricPlot[{fx, fy}, {u, umin, umax}]: For plotting a parametric curve with coordinates fx and fy as a function of u. This command is useful for phase plane plots where fx = y (x) and fy = y (x).

Optional - Partial Dierential Equations

In the second half of the course, we will look at partial dierential 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 dierence between an ordinary dierential equation (ODE) and a partial dierential equation (PDE) is that the unknown function in a PDE is a multivariable equation, and the PDE contains its partial derivatives. An example is the Laplace equation, which you may have seen before (and you will see it many more times so get used to it) 2u 2u + 2 = 0, x2 y which is equivalent in shorthand as uxx + uyy = 0. To create PDEs in Mathematica, you must use the D function, which takes derivatives. Typing D[x3, x] outputs 3x2 . For example, if we wanted to plot y + y 2 x 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 ] == S i n [ 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 dene our domain for each variable. Plot3D [ y [ x , t ] / . s o l , { x , 10, 1 0 } , { t , 5, 5 } , PlotRange > A l l ] This plot should look like:

Figure 3: Plotting the solution to a partial dierential equation. Now that you know how to dene a PDE, you can also solve it using NDSolve the same way you would for an ODE.

You might also like