How to | Use Derivatives for Setting Up Differential Equations

Mathematica's functions for solving differential equations can be applied to many different classes of differential equations, including ordinary differential equations (ODEs), partial differential equations (PDEs), differential-algebraic equations (DAEs), and boundary value problems (BVPs). Using derivatives to set up these equations for solving in Mathematica is essential.

For an ODE, you can express the derivative of a function of with respect to in several ways.

The most common way is to use D:

In[8]:=
Click for copyable input
Out[8]=

You can specify the order of differentiation, which in this case is 1:

In[4]:=
Click for copyable input
Out[4]=

Using also gives the derivative :

In[9]:=
Click for copyable input
Out[9]=

Using is shorthand for Derivative:

In[10]:=
Click for copyable input
Out[10]=

FullForm shows that and Derivative are equivalent:

In[2]:=
Click for copyable input
Out[2]//FullForm=
    

Use D to set up the ODE for solving and then store the equation as :

In[6]:=
Click for copyable input
Out[6]=

Use TraditionalForm to see the ODE as it would appear in a mathematics textbook or journal article:

In[7]:=
Click for copyable input
Out[7]//TraditionalForm=

Once the ODE is set up, use DSolve to solve it symbolically:

In[8]:=
Click for copyable input
Out[8]=

The solution is represented as a rule in a nested list. For information on getting this solution out of the list and using it, see How to: Use Rule Solutions.

Most of the time, ODEs are accompanied by boundary and initial conditions. Thus, evaluation of derivatives of functions for specific values of variables needs to be used frequently. This can be done in several ways. Here, is used as an example.

Use (shorthand for ReplaceAll) to substitute for . Mathematica first evaluates D, then performs the replacement:

In[12]:=
Click for copyable input
Out[12]=

Or, just evaluate directly by using :

In[13]:=
Click for copyable input
Out[13]=

Also equivalent is Derivative:

In[14]:=
Click for copyable input
Out[14]=

As another example, set up the ODE , , for solving:

In[1]:=
Click for copyable input
Out[1]=

Use DSolve to obtain the symbolic solution:

In[3]:=
Click for copyable input
Out[3]=

Of course, you can solve directly without setting up:

In[4]:=
Click for copyable input
Out[4]=

Alternatively, you can express the ODE as a PDE and solve it for instead of :

In[17]:=
Click for copyable input
Out[17]=
In[19]:=
Click for copyable input
Out[19]=

Here, the ODE , is set up:

In[4]:=
Click for copyable input
Out[4]=

Use DSolve to obtain the symbolic solution:

In[5]:=
Click for copyable input
Out[5]=

Instead of using DSolve, you can use NDSolve to obtain a numerical solution that corresponds to specific values of that are involved in one of the coefficients of the ODE.

The following example gives the solution when . NDSolve gives results in terms of InterpolatingFunction objects:

In[6]:=
Click for copyable input
Out[6]=

The following function gives numerical solutions for each value of :

In[32]:=
Click for copyable input

Plot the results at and . Evaluate is used so that evaluates properly in Plot:

In[34]:=
Click for copyable input
Out[34]=

For more information, see How to: Plot the Results of NDSolve.

    

Mathematica can also set up and solve PDEs.

The function is used here to show the different ways partial derivatives can be expressed in Mathematica.

The most common way to get a partial derivative is to use D:

In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
Out[7]=
In[8]:=
Click for copyable input
Out[8]=

You can accomplish the same thing by using slightly different syntax for D:

In[11]:=
Click for copyable input
Out[11]=
In[12]:=
Click for copyable input
Out[12]=
In[13]:=
Click for copyable input
Out[13]=

Alternatively, use Derivative:

In[12]:=
Click for copyable input
Out[12]=
In[13]:=
Click for copyable input
Out[13]=
In[14]:=
Click for copyable input
Out[14]=

FullForm shows that these methods are equivalent:

In[15]:=
Click for copyable input
Out[15]//FullForm=

Next, consider several variations of a PDE.

Set up the PDE for solving:

In[17]:=
Click for copyable input
Out[17]=

Use DSolve to find a symbolic solution to the PDE. The result is given in terms of an arbitrary function C[1]:

In[19]:=
Click for copyable input
Out[19]=

The solution can also be obtained in terms of a pure function. Function represents a pure function in Mathematica:

In[18]:=
Click for copyable input
Out[18]=

Use DSolve to obtain a symbolic solution to the PDE , :

In[10]:=
Click for copyable input
Out[10]=

Use NDSolve to obtain a numeric solution to the PDE , , . The solution is stored as for later use:

In[14]:=
Click for copyable input
Out[14]=

Use Plot3D to visualize the result of NDSolve:

In[19]:=
Click for copyable input
Out[19]=
    

In the cases above, the derivatives and partial derivatives are given in terms of variables. Here, they are evaluated numerically.

Both of the following ways can be used to express the values of partial differentials, such as :

In[29]:=
Click for copyable input
Out[29]=
In[28]:=
Click for copyable input
Out[28]=
    

In addition to ODEs and PDEs, Mathematica can also solve DAEs (differential-algebraic equations).

As an example, consider the DAE , satisfying :

In[31]:=
Click for copyable input

Use DSolve to obtain a symbolic result for the DAE:

In[32]:=
Click for copyable input
Out[32]=

To see the solution at and , use to substitute these values into the solution:

In[33]:=
Click for copyable input
Out[33]=

Now plot the solution:

In[34]:=
Click for copyable input
Out[34]=

You can also produce the same plot by first substituting and to , and then using NDSolve:

In[35]:=
Click for copyable input
Out[35]=

As before, visualize the solution with Plot:

In[36]:=
Click for copyable input
Out[36]=
New to Mathematica? Find your learning path »
Have a question? Ask support »