Calculus and Differential Equations

Index

This index gives a list of keywords that are associated with various cells in this Notebook. If you Command-Double-click one of the keywords below,  Mathematica  will find the cell associated with that keyword, select it, and display it in the center of  your window.

D  
DSolve  
Dt  
Evaluate  
Integrate  
Limit  
N  
NDSolve    
NIntegrate`advanced  
NIntegrate`basic  
NProduct  
NSum  
Plot  
Product  
Residue  
Series  
Sum

Derivatives

Syntax  (see pages 87 and 624)

D[f,x ]  gives the partial derivative of f  with respect to x .

D[f ,{x ,n }] gives the n th partial derivative with respect to x .

D[f ,{x ,y ,...}] gives the partial derivative with respect to x , y , ....

Dt[f , x ] gives the total derivative of f  with respect to x .

Dt[f ] gives the total differential of f .

First Derivative

D[ x^n, x ]

Multiple Derivative

This gives the third derivative of x ^n .

D[ x^n, {x, 3}]

Mixed Partial Derivative

This gives the mixed partial with respect to x  and y .

D[x^3 y^4, x, y ]

Multiple Partial Derivative

This gives the second partial with respect to x  and first partial with respect to y .

D[x^3 y^4, {x, 2}, y ]

The Value of a Derivative at a Point

This gives the derivative and then evaluates it at the point x  = 2.

D[x^3, x]

% /. x -> 2

The Derivative as a Function

This gives the definition of a function, its derivative and
the value of the derivative at x  = 2.

f[x_] := x^3

f'[x]

f'[2]

Total Derivative

Note that n  is treated as if it depends on x .

Dt[x^n, x]

Total Differential

Dt[x^2 + y^2]

Integrals

Basic Integrals

Syntax (see pages 89,  106,  630 and 686)

Integrate

Integrate[f,x ]  gives the indefinite integral of f  with respect to x.

Integrate[f, {x ,xmin ,xmax }]   gives the definite integral from xmin  to xmax .

Integrate[f, {x ,xmin ,xmax },{y ,ymin ,ymax }]  gives the multiple integral from xmin  to xmax  and ymin  to ymax . The range of the outermost integration variable appears first.

NIntegrate

NIntegrate[f,{x ,xmin ,xmax }] gives a numerical approximation to the definite integral.

Indefinite Integral

Integrate[x^2 + 5x + 3, x ]

Definite Integral

This gives the definite integral between 0 and 1.

Integrate[x^2 + 5x + 3, {x, 0, 1} ]

Integral with Endpoint Equal to Infinity

Integrate[x^-2, {x, 1, Infinity}]

A Note about Generic Answers for Symbolic Integrals

Mathematica  assumes that any symbolic quantities in your integrand have "generic" values. Thus, for example, Integrate will assume that n is not equal to -1 when integrating x^n . Of course, if you specify a value of -1 for n  before you use Integrate, you will get the correct answer.

Symbolic Integral

Integrate[ x^n, x]

Integrate[x^-1, x]

Integral with No Closed Form Solution

Integrate[ Sin[ Sin[x]], {x,0,1}]

N[%]

A Note on Integrals with No Closed Form Solution

Many integrals do not have a "closed form solution".  If you give Mathematica  such a definite integral it will be returned unevaluated. In this case, you can use N[%] to get a numerical answer as in the example above. You can also use the Mathematica  function NIntegrate directly.  It computes a numerical approximation to the value of the definite integral. Note that it is typically much faster to use NIntegrate directly than to attempt a symbolic integration and then use N. Note also that NIntegrate has many options that allow the user to specify the accuracy goal, the evaluation grid, etc. For more information, see the examples in Numerical Integration below.

Numerical Integral

NIntegrate[ Sin[ Sin[x]], {x, 0, 1}]

A Note about Numerical Integration with Singularities

You have to be careful when numerically evaluating an integral that has a singularity, i.e., a point where the function is infinite or not defined. For example, if  Integrate[x^(-2), {x,-1,1}] is given as input, Mathematica  will compute the indefinite integral of x^(-2), evaluate the result at 1 and -1, and then subtract these results. This will give a result of -2 instead of the correct answer of infinity. For more information on how to deal with this situation see the examples in Advanced Integrals below.

Multiple Integral

The inner limits of integration are 0 to x  and are with respect to y . The outer limits are 0 to 1 and are with respect to x . The outer limits must be given first.

Integrate[ x^2 + y^2, {x,0,1}, {y,0,x}]

Numerical Integration

Syntax (see page 686)

Numerical Evaluation after Using Integrate

N[Integrate[ expr ,{x ,xmin ,xmax }]] tries to perform an integral exactly, then finds a numerical approximation to the parts that remain.

NIntegrate

NIntegrate[f ,{x ,xmin ,xmax }] gives a numerical approximation to the definite integral.

NIntegrate[f ,{x ,xmin ,xmax },{y ,ymin ,ymax }]  gives a numerical approximation to a multidimensional definite integral. The range of the outermost integration variable appears first.

NIntegrate[f ,{x ,xmin ,x1 ,x2 , ..., xmax }] does a numerical integral along a line, statrting at xmin , going through the points xi, and ending at xmax .

Options for NIntegrate (for additional options, see pages 688 and 830)

WorkingPrecision -> n  specifies that n  digits be used in internal computations.

MinRecursion -> n  specifies the minimum number of recursive subdivisions of the integration region.

MaxRecursion -> n  specifies the maximum number of recursive subdivisions of the integration region.

A Numerical Integral

NIntegrate[ Sin[ Sin[x]], {x, 0, 1}]

An Integral with Infinite Upper Limit

NIntegrate[Exp[-x^3], {x, 0, Infinity}]

A Multidimensional Integral

NIntegrate[ x^2 + y^2, {x, -1, 1}, {y, -1, 1}]

An Integral with Singularities

NIntegrate[ 1/ Sqrt[Abs[x]], {x, -1, 0, 2}]

A Note on Integrals with Singularities

Mathematica  automatically looks for singularities at the
endpoints of your integration region. When other singularities are present, NIntegrate may not give you the right answer for the integral. If you know that your integrand has singularities at particular points, you can explicitly tell NIntegrate to deal with them as in the previous example.

Specifying an Integration Contour

NIntegrate[ 1/ z, {z, -1, -I, 1, I, -1}]

Chop[%]

Specifying the Working Precision

NIntegrate[ 1/x, {x, 1, 2},
                       WorkingPrecision -> 40]

Specifying the Recursive Subdivisions

With the default settings, NIntegrate misses the peak in the function at x = 0 and gives the wrong answer. With the option MinRecursion -> 3,  NIntegrate samples enough points to notice the peak. The option MaxRecursion -> 10 allows enough points to be sampled to obtain the correct answer.

NIntegrate[Exp[-x^2], {x, -1000, 1000}]

NIntegrate[Exp[-x^2], {x, -1000, 1000},
                       MinRecursion -> 3]

NIntegrate[Exp[-x^2], {x, -1000, 1000},
   MinRecursion -> 3, MaxRecursion -> 10]

Limits

Syntax (see pages 102 and  646)

Limit[expr , x -> x0  ] gives the limiting value of expr  as x  approaches x0.

The Limit at a Finite Point

Limit[ Sin[x]/x, x -> 0]

The Limit at Infinity

Limit[ ArcTan[x], x -> Infinity]

One-Sided Limit

The option Direction -> 1 gives the limit from below. To compute the limit from above use Direction -> -1.

Limit[ 1/x, x -> 0, Direction -> 1]

Numerical Series and Products

Syntax (see pages 106 and 689)

Sum

Sum[f ,{i ,imax }] evaluates the sum of terms of the form f  from i  = 0 to i = imax .

Sum[f ,{i ,imin ,imax }] starts with i = imin .

Sum[f ,{i ,imin,imax , di}]  uses step size di .

Sum[f ,{i ,imin ,imax },{j ,jmin ,jmax }, ...] evaluates a multiple sum.

NSum

NSum[f ,{i ,imin ,imax }] gives a numerical approximation to the sum.

NSum[f ,{i ,imin ,imax , di }] uses step di  in the sum.

Product

Product[f , {i , imax }] evaluates the product of terms of the form f  from i  = 0 to i  = imax .

NProduct

NProduct[f , {i , imax }]  gives a numerical approximation to the product.

Finite Sum

This gives the sum of the first 20 terms starting with i  = 1.

Sum[1/ i^3, {i,20}]

Multiple Sum

This is a double sum with the range of both i  and j  from 1 to 10.

Sum[ 1/ (i +j), {i, 1, 10}, {j, 1, 10}]

Numerical Approximation of Infinite Sum

This gives a numerical approximation to the sum as i  goes from 1 to infinity. Note that NSum is used.

NSum[1 / i^(3.2), {i,1, Infinity}]

Numerical Approximation of Infinite Sum Using N

If you give Mathematica  a sum it doesn't know, it will be returned unevaluated. You can still get a numerical approximation using N. Note that this is typically much slower than using NSum directly.

Sum[1 / i^(3.2), {i,1, Infinity}]

N[%]

A Sum with Specified Step Size

Sum[ 0.1 Sin[i], {i, 0, Pi, 0.1}]

Finite  Product

Product[ 1 + I/n, {n, 1, 20}]

Infinite Product

NProduct[ 1 - 1/(n^2), {n, 2, Infinity}]

Power Series

Syntax (see pages 100 and 639)

Series[f ,{x ,x0 ,n }] generates a power series expansion for f  about the point x  = x0  up to order (x -x0 )^n .

Series[f ,{x ,x0 ,n },{y ,y0 ,n }] successively finds series expansions with respect to y , then x .

A Power Series

This gives the power series of Exp[x ] expanded about 0 up to terms of order x ^5.

Series[ Exp[x], {x, 0, 5}]

A Power Series Expanded about Infinity

Series[ Exp[1/x], {x, Infinity, 3}]

Operations on Power Series

Note that Mathematica  generates the appropriate power series for Cos[x ]  and keeps track of the order of the computed series.

Series[ Sin[x], {x,0,4}]

%^2 + Cos[x]

D[%, x]

Inverting a Power Series

This computes a power series for Sin[t ] expanded about t = 0 and then inverts the series. This gives the series for ArcSin[x ] expanded about           x = Sin[0 ] = 1.

Series[ Sin[t], {t,0,5}]

InverseSeries[%, x]

Symbolic Power Series

Series[ g[x], {x,1,2}]

Conversion of Power Series to Ordinary Expressions

This converts the power series for Sin[x ] into an ordinary expression. Note that this does more than simply delete the O[x ]^5.. It changes the way the expression is represented internally in Mathematica .

Series[ Sin[x], {x,0,5}]

Normal[%]

Solving Equations Involving Power Series

In this example a power series named y with unspecified coefficients is generated. The next input gives a differential equation involving the series. LogicalExpand generates a sequence of equations for each power of x. This last step solves the equations for the coefficients a[i].

y = 1 + Sum[a[i] x^i, {i,3}] + O[x]^4

D[y,x]^2 - y == x

LogicalExpand[%]

Solve[%]

Power Series in Two Variables

This  computes the power series of Sin[x y] with respect to y and then x. Both expansions are done to order 4 about 0.

Series[ Sin[x y], {x, 0, 4}, {y, 0, 4}]

Residues

Syntax (see page 648)

Residue[f ,{x ,x0 }]  gives the residue of f  at x  = x0 . The residue at x0  is defined as the coefficient of (x  - x0 )^-1 in the Laurent expansion.

Residue

Residue[ 1/x, {x, 0}]

Ordinary Differential Equations

Basic Differential Equations

Syntax (see pages 99, 109, 637 and 696)

DSolve

DSolve[eqn , y [x ],x ] solves a differential equation for y [x ],
taking x  as the independent variable.

DSolve[eqn , y , x ]  gives a solution for y  in pure function form.

DSolve[{eqn1 , eqn2 ,...}, {y1 [x1 ,...], ...}, {x1 , ...}] solves a list of differential equations.

NDSolve

NDSolve[eqns , y , {x ,xmin ,xmax }] finds a numerical solution for y to the differential equations given by eqns with the independent variable x in the range xmin to xmax.

NDSolve[eqns , {y1 , y2 , ...}, {x ,xmin ,xmax }] finds numerical solutions for the functions yi .

Differential Equation in a Single Variable

This computes the solution of the differential equation, y'[x] == 2 y[x]. Note that in the equations the dependence on x must be given explicitly. In the solution, the C[1] is an undetermined coefficient.

DSolve[ y'[x] == 2 y[x], y[x], x]

Differential Equation with Initial Conditions

This  computes the solution of the differential equation,          y'[x] == 2 y[x] with initial conditions y[0] == 1.

DSolve[ {y'[x] == 2 y[x], y[0]==2},
                                  y[x], x]

Differential Equation in Two Variables

DSolve[ {x'[t] == - y[t], y'[t] ==  x[t] },
                             {x[t],y[t]}, t]

Second Order Differential Equation

DSolve[y''[x] - k y[x] == 1, y[x], x]

A Note on Pure Function Solutions

When you ask DSolve to get a solution for y[x], the rules it returns specify how to replace y[x] in any expression. If you want to manipulate solutions that you get from DSolve, you will often find it better to ask for solutions for y, rather than y[x]. In this case, the solution is returned as a "pure function". This is illustrated in the next example. For more information on how the Function object returned by DSolve works, see page 207.

Pure Function Solution to a Differential Equation

This computes the solution of the differential equation,            y'[x] == 2 y[x] and returns the solution as a pure function. In the next line, the solution is evaluated at x = 2.8. The last input  substitutes the solution into y'[x] - 2 y[x]. The result of 0 confirms that we have found the solution.

soln = DSolve[ {y'[x] == 2 y[x], y[0] == 1}, y, x]

y[2.8] /. soln

y'[x] - 2 y[x] /. soln

A Note on  Numerical Solutions to Differential Equations

Most differential equations do not have a "closed form" solution. In these cases you can use NDSolve to generate a numerical approximation of the solution.  NDSolve represents solutions as InterpolatingFunction objects. The InterpolatingFunction objects provide approximations to the solutions over the range of the independent variable. For more information on InterpolatingFunction objects, see page 676.

Numerical Solution of a Differential Equation

This  finds a numerical solution to the differential equation                   y''[x] + Sin[x]^2 y'[x] + y[x] == Cos[x]^2 with initial conditions y[0] == 1 and y'[0] == 0 and x in the range of 0 to 10.

NDSolve[
   {y''[x] + Sin[x]^2 y'[x] + y[x] ==Cos[x]^2,
         y[0] == 1, y'[0] == 0},y, {x,0,10}]

Plotting the Solution

NDSolve[
   {y''[x] + Sin[x]^2 y'[x] + y[x] ==Cos[x]^2,
         y[0] == 1, y'[0] == 0},y, {x,0,10}]

Plot[ Evaluate[ y[x] /. %], {x, 0, 10}];

Power Series Solution of a Differential Equation

In this example a power series named y with unspecified coefficients is generated. The next input gives a differential equation involving the series. LogicalExpand generates a sequence of equations for each power of x. This last step solves the equations for the coefficients a[i].

y = 1 + Sum[a[i] x^i, {i,3}] + O[x]^4

D[y,x]^2 - y == x

LogicalExpand[%]

Solve[%]

Numerical Solutions of Differential Equations

Syntax ( see page 108 and 696)

NDSolve

NDSolve[eqns, y, {x, xmin, xmax }]  finds a numerical solution for y  to the differential equations given by eqns  with the independent variable x  in the range xmin  to xmax .

NDSolve[eqns, {y1, y2, ...},{x , xmin , xmax }]  finds numerical solutions for the functions yi .

Getting a Value

y [x ]/. solution   uses the list of rules for the function y  to get values for y [x ].

Plotting a Solution

Plot[Evaluate[ y [x  ]/.solution ]], {x, xmin, xmax }] plots the solution to a differential equation.

Options for NDSolve

AccuracyGoal -> n  specifies that n  digits of accuracy are sought.

PrecisionGoal -> n  specifies that n  digits of precision are sought.

WorkingPrecision -> n  specifies that n  digits are used in internal computations.

A Differential Equation in One Variable

Note that in the equations, you must give initial conditions and include the dependence of y  on x .

NDSolve[
    {y'[x] == y[x], y[0] == 1}, y, {x, 0, 2}]

A Note on Numerical Solutions to Differential Equations

Most differential equations do not have a "closed form" solution. In these cases you can use NDSolve to generate a numerical approximation of the solution.  NDSolve represents solutions as InterpolatingFunction objects. The InterpolatingFunction objects provide approximations to the solutions over the range of the independent variable. For more information on InterpolatingFunction objects, see page 676.

A Second Order Equation in One Variable

NDSolve[
   {y''[x] + Sin[x]^2 y'[x] + y[x] ==Cos[x]^2,
         y[0] == 1, y'[0] == 0},y, {x,0,10}]

Plotting the Solution

NDSolve[
   {y''[x] + Sin[x]^2 y'[x] + y[x] ==Cos[x]^2,
         y[0] == 1, y'[0] == 0},y, {x,0,10}]

Plot[ Evaluate[ y[x] /. %], {x, 0, 10}];

A Pair of Coupled Equations

Note that because the initial conditions are both given at t = 0, you do not have to give the starting point for t explicitly.

sol = NDSolve[
      {x'[t] == -y[t] - x[t]^2, 
       y'[t] == 2 x[t] - y[t], x[0] ==y[0] == 1},
       {x, y}, {t, 10}]

Plotting the Solutions of Two Coupled Equations

Note that sol was defined in the previous example.

ParametricPlot[Evaluate[{x[t], y[t]} /. sol],
                  {t, 0, 10}, PlotRange -> All];

A Table of Equations

Clear[y]

NDSolve[{y'[x]==y[x],y[0]==1},y,{x,10}]

eqns = Join[
   Table[ y[i]'[x] == y[i-1][x] - y[i][x],
                            {i, 2, 4}], 
 {y[1]'[x] == -y[1][x],
          y[5]'[x] == -y[4]'[x], y[1][0] == 1},
     Table[y[i][0] == 0, {i, 2, 5}]
             ]

NDSolve[eqns, Table[y[i], {i, 5}], {x, 10}]

Plot[Evaluate[Table[y[i][x], {i, 5}] /. %],
                                  {x, 0, 10}]

Generating a High-Precision Solution

NDSolve[{y'[x] == I/4 y[x], y[0] == 1}, y, {x, 1},
    AccuracyGoal -> 20, PrecisionGoal -> 20,
                      WorkingPrecision -> 25]

y[1] /. %

An Equation in Three Dimensions

Note that if the solutions have a complicated structure, it may be necessary to have a large setting for MaxSteps to reproduce the full structure of the solutions.

lorenz = NDSolve[
 {x'[t] == -3 (x[t] - y[t]),
    y'[t] == -x[t] z[t] + 26.5 x[t] - y[t],
       z'[t] == x[t] y[t] - z[t],
          x[0] == z[0] == 0, y[0] == 1},
    {x, y, z}, {t, 0, 20}, MaxSteps -> 3000]

Plotting a Three-Dimensional Solution

PlotPoints is an option for ParametricPlot3D that specifies the number of sample points to be used. Increasing the number of plot points ensures a smooth plot of the trajectories.

ParametricPlot3D[
     Evaluate[{x[t], y[t], z[t]} /. lorenz],
                {t, 0, 20}, PlotPoints -> 1000];