Solve an ordinary differential equation - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Home : Support : Online Help : Getting Started : How Do I... : Solve an ordinary differential equation

How Do I

 Solve an Ordinary Differential Equation?

This topic introduces you to the commands and techniques used to solve ordinary differential equations (ODEs) in Maple. Unless noted otherwise, each example goes through the following steps:

 1 Defining your system of ODEs.
 2 Finding solutions.
 3 Assigning the solutions to names (thus making them easier to plot and investigate).
 4 Plotting your solution.

Before you start looking at the examples on this page, you should consider what type of solution you need: symbolic or numeric.

 • Symbolic solutions return equations that contains your independent variables. These solutions are exact and can be easily manipulated to find, for example, a value at a point, a derivative of the solution, or an integral of the solution. Note that not every ODE or system of ODEs has a symbolic solution. However, if you are taking an introductory course in ODEs, it is most likely that you'll be interested in finding symbolic solutions.
 • Numeric solutions return a procedure that integrates your ODEs. Since they are integrated numerically, numeric solutions are approximations. Most ODEs can be solved numerically even if they don't have a symbolic solution. If you need to find derivatives or integrals of your solution, you should include equations for the derivatives and integrals in your system of ODEs.

This topic contains separate sections on how to find symbolic and numeric solutions since the techniques for solving those problems differ slightly. The examples in each section are arranged from simple to more complex.

Finally, if you do not find the information you are looking for in this topic, the Additional Resources section contains links to other pages that go into more detail about solving ODEs.

Solving an ODE Symbolically

The default behavior of dsolve is to find a symbolic solution for your ODE. After you have a symbolic solution, you can assign it to a variable so that you can evaluate and graph it.

 – Basic Example: y' =a y
 – Taking Derivatives and Integrals of Symbolic Solutions
 – Solving a System of ODEs
 – Solving a Linear Two-Point BVP for a Second-order ODE
 – Implicit Solutions with RootOf

Basic Example: y' = a y

The following example shows how to find a symbolic solution to the ODE .

Define a variable called basicODE and set it equal to the ODE.

 > $\mathrm{restart};$
 > $\mathrm{basicODE}≔\mathrm{diff}\left(y\left(x\right),x\right)=a\cdot y\left(x\right);$
 ${\mathrm{basicODE}}{≔}\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){=}{a}{}{y}{}\left({x}\right)$ (1.1.1)

Use dsolve(basicODE) to find the solution. Here, we assign the result to a variable called basicSolution. The result is an equation that satisfies the ODE. Terms of the form _Cn (where n is an integer) are constants of integration.

 > $\mathrm{basicSolution}≔\mathrm{dsolve}\left(\mathrm{basicODE}\right);$
 ${\mathrm{basicSolution}}{≔}{y}{}\left({x}\right){=}{\mathrm{_C1}}{}{{ⅇ}}^{{a}{}{x}}$ (1.1.2)

To find the constant for a particular solution, include an initial value equation with the ODE in a set or list and then pass the set/list to dsolve. The following expression finds a solution that satisfies the condition $y=5$ when $x=0$.

 >
 ${\mathrm{basicODE}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){=}{a}{}{y}{}\left({x}\right){,}{y}{}\left({0}\right){=}{5}\right\}$ (1.1.3)
 > $\mathrm{basicSolution}≔\mathrm{dsolve}\left(\mathrm{basicODE}\right);$
 ${\mathrm{basicSolution}}{≔}{y}{}\left({x}\right){=}{5}{}{{ⅇ}}^{{a}{}{x}}$ (1.1.4)

Test your solution with the odetest function. The odetest function takes the symbolic solution from dsolve and your ODEs as parameters. It returns 0 if the solution satisfies the ODEs.

 >
 $\left\{{0}\right\}$ (1.1.5)

To find the value of $y$ at a particular value of $x$ or to graph the solution, you can use either the rhs function or the eval(expression, equations) command to assign the right-hand side of the solution to a variable. Of the two methods, eval is more flexible (especially when dealing with a set of solutions), and will be used in the rest of the examples in this section.

 >
 ${Y}{≔}{5}{}{{ⅇ}}^{{a}{}{x}}$
 ${Y}{≔}{5}{}{{ⅇ}}^{{a}{}{x}}$ (1.1.6)

After assigning a value to the parameter $a$, use eval on Y to get an exact value for the solution at a point.

 > $a≔-\frac{1}{3};$
 ${a}{≔}{-}\frac{{1}}{{3}}$ (1.1.7)
 >
 ${5}{}{{ⅇ}}^{{-}\frac{{5}}{{3}}}$ (1.1.8)

Use the evalf function to get a floating-point value.

 >
 ${0.9443780140}$ (1.1.9)

To visualize the solution, use the plot function. The following graphs Y over $x=0..5$.

 >

Finally, the function Y can be used in fsolve to find, for example, the value of x for which y(x)=2.

 > $b≔\mathrm{fsolve}\left(Y\left(x\right)=2,x\right);$
 ${b}{≔}{2.748872196}$ (1.1.10)
 > $\mathrm{eval}\left(Y,x=b\right);$
 ${2.000000000}$ (1.1.11)
 > 

Taking Derivatives and Integrals of Symbolic Solutions

Symbolic solutions can be differentiated and integrated using the commands diff and int.

In this example, we have a first-order differential equation in v(t), the velocity of an object. Once we find v(t), we want to take its derivative to find the object's acceleration, and then take the integral of v(t) to find the distance traveled by the object. Finally, we want to plot the velocity, acceleration, and distance on a single plot.

We start by defining equations, a set that contains the ODE and the initial condition (we assume the initial velocity to be 0).

 > $\mathrm{restart};$
 >
 ${\mathrm{equations}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{v}{}\left({t}\right){=}{-}{1}{+}{{v}{}\left({t}\right)}^{{2}}{,}{v}{}\left({0}\right){=}{0}\right\}$ (1.2.1)

Run dsolve(equations) to get a solution for v(t).

 > $\mathrm{solution}≔\mathrm{dsolve}\left(\mathrm{equations}\right);$
 ${\mathrm{solution}}{≔}{v}{}\left({t}\right){=}{-}{\mathrm{tanh}}{}\left({t}\right)$ (1.2.2)
 > $\mathrm{odetest}\left(\mathrm{solution},\mathrm{equations}\right);$
 $\left\{{0}\right\}$ (1.2.3)

Assign the solution for v(t) to a variable, V:

 > $V≔\mathrm{eval}\left(v\left(t\right),\mathrm{solution}\right);$
 ${V}{≔}{-}{\mathrm{tanh}}{}\left({t}\right)$ (1.2.4)

You can use the diff function to take the derivative of V and get the acceleration of the object, A.

 > $A≔\mathrm{diff}\left(V,t\right);$
 ${A}{≔}{-}{1}{+}{{\mathrm{tanh}}{}\left({t}\right)}^{{2}}$ (1.2.5)

Similarly, you can use the int command to integrate V and get S, the distance traveled by the object. Since V is an expression in t, we need to integrate V over t from our initial time, 0, to some final time, T, giving us an expression in T. (Note that we assume the initial position of the object to be 0.)

 > $S≔\mathrm{int}\left(V,t=0..T\right);$
 ${S}{≔}{-}{\mathrm{ln}}{}\left({\mathrm{cosh}}{}\left({T}\right)\right)$ (1.2.6)

If you are only interested in solutions for T > 0, you can specify this by using assuming on the int expression.

 >
 ${S}{≔}{\mathrm{ln}}{}\left({2}\right){-}{\mathrm{ln}}{}\left({{ⅇ}}^{{2}{}{T}}{+}{1}\right){+}{T}$ (1.2.7)

Note that this expression is equal to our initial expression for S, -ln(cosh(T)).

To get an expression in t, evaluate S at t:

 > $S≔\mathrm{eval}\left(S,T=t\right);$
 ${S}{≔}{\mathrm{ln}}{}\left({2}\right){-}{\mathrm{ln}}{}\left({{ⅇ}}^{{2}{}{t}}{+}{1}\right){+}{t}$ (1.2.8)

The following command plots our solution, V, its derivative, A, and its integral, S.

 >
 > 

Solving a System of ODEs

When solving a system of ODEs, enclose the system in a set or list. The following is an example of a system with two ODEs enclosed in a set.

 > $\mathrm{restart};$
 >
 ${\mathrm{odeSystem}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{x}{}\left({t}\right){=}{-}{x}{}\left({t}\right){-}{y}{}\left({t}\right){,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({t}\right){=}{x}{}\left({t}\right){-}{y}{}\left({t}\right)\right\}$ (1.3.1)

Run dsolve(odeSystem) to get a solution. The result is a set of equations that satisfies the system.

 > $\mathrm{systemSol}≔\mathrm{dsolve}\left(\mathrm{odeSystem}\right);$
 ${\mathrm{systemSol}}{≔}\left\{{x}{}\left({t}\right){=}{{ⅇ}}^{{-}{t}}{}\left({\mathrm{_C2}}{}{\mathrm{cos}}{}\left({t}\right){+}{\mathrm{_C1}}{}{\mathrm{sin}}{}\left({t}\right)\right){,}{y}{}\left({t}\right){=}{-}{{ⅇ}}^{{-}{t}}{}\left({\mathrm{cos}}{}\left({t}\right){}{\mathrm{_C1}}{-}{\mathrm{sin}}{}\left({t}\right){}{\mathrm{_C2}}\right)\right\}$ (1.3.2)
 > $\mathrm{odetest}\left(\mathrm{systemSol},\mathrm{odeSystem}\right);$
 $\left\{{0}\right\}$ (1.3.3)

To access the equations in the solution, use the eval(expression, equations) command. Enter either x(t) or y(t) for expression; enter systemSol for equations.

 >
 ${X}{≔}{{ⅇ}}^{{-}{t}}{}\left({\mathrm{_C2}}{}{\mathrm{cos}}{}\left({t}\right){+}{\mathrm{_C1}}{}{\mathrm{sin}}{}\left({t}\right)\right)$ (1.3.4)
 > $Y≔\mathrm{eval}\left(y\left(t\right),\mathrm{systemSol}\right);$
 ${Y}{≔}{-}{{ⅇ}}^{{-}{t}}{}\left({\mathrm{cos}}{}\left({t}\right){}{\mathrm{_C1}}{-}{\mathrm{sin}}{}\left({t}\right){}{\mathrm{_C2}}\right)$ (1.3.5)

Include the initial conditions in the set of ODEs to find a particular solution for the system. The following specifies that the solution should go through the point (1,2) at t=0.

 >
 ${\mathrm{odeSystem}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{x}{}\left({t}\right){=}{-}{x}{}\left({t}\right){-}{y}{}\left({t}\right){,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({t}\right){=}{x}{}\left({t}\right){-}{y}{}\left({t}\right){,}{x}{}\left({0}\right){=}{1}{,}{y}{}\left({0}\right){=}{2}\right\}$ (1.3.6)
 > $\mathrm{systemSol}≔\mathrm{dsolve}\left(\mathrm{odeSystem}\right);$
 ${\mathrm{systemSol}}{≔}\left\{{x}{}\left({t}\right){=}{{ⅇ}}^{{-}{t}}{}\left({\mathrm{cos}}{}\left({t}\right){-}{2}{}{\mathrm{sin}}{}\left({t}\right)\right){,}{y}{}\left({t}\right){=}{-}{{ⅇ}}^{{-}{t}}{}\left({-}{2}{}{\mathrm{cos}}{}\left({t}\right){-}{\mathrm{sin}}{}\left({t}\right)\right)\right\}$ (1.3.7)
 > $X≔\mathrm{eval}\left(x\left(t\right),\mathrm{systemSol}\right);$
 ${X}{≔}{{ⅇ}}^{{-}{t}}{}\left({\mathrm{cos}}{}\left({t}\right){-}{2}{}{\mathrm{sin}}{}\left({t}\right)\right)$ (1.3.8)
 > $Y≔\mathrm{eval}\left(y\left(t\right),\mathrm{systemSol}\right);$
 ${Y}{≔}{-}{{ⅇ}}^{{-}{t}}{}\left({-}{2}{}{\mathrm{cos}}{}\left({t}\right){-}{\mathrm{sin}}{}\left({t}\right)\right)$ (1.3.9)

Use eval and evalf to analyze your solution.

 >
 ${-}{{ⅇ}}^{{-}{\mathrm{\pi }}}$
 ${-0.04321391825}$ (1.3.10)
 >
 ${-}{2}{}{{ⅇ}}^{{-}{\mathrm{\pi }}}$
 ${-0.08642783650}$ (1.3.11)

The following command finds the first time x(t) = y(t) with t ≥ 1.5.

 > $\mathrm{fsolve}\left(X\left(t\right)=Y\left(t\right),t=1.5\right);$
 ${2.819842099}$ (1.3.12)

The X and Y variables can be used to graph the solutions using the plot function. The following is a graph of x(t) and y(t) versus t.

 >

The following is a parametric plot of the solution over the same time interval as the preceding graph.

 > $\mathrm{plot}\left(\left[X,Y,t=0..5\right],\mathrm{labels}=\left["x","y"\right]\right);$
 > 

Solving a Linear Two-Point BVP for a Second-Order ODE

Finding solutions to a two-point boundary value problem (BVP) is more involved than solving an initial value problem. For example, let's say you have an ODE that can always be solved as an initial value problem. When that same ODE is cast as a two-point BVP, there is no guarantee that a solution can be found. In fact, we will see that to a two-point BVP can result in any of the following:

 • A unique solution
 • No solution
 • An infinite number of solutions

Note: The theory that explains the behavior of the two-point BVP for linear second-order ODEs is attributed to Sturm and Liouville. Such problems are called Sturm-Liouville problems.

Let's start off by finding the solution for an ODE given some initial conditions. After that, we'll look at solving the same ODE but as a two-point BVP.

 > $\mathrm{restart};$
 > $\mathrm{ode}≔\mathrm{diff}\left(y\left(x\right),x,x\right)+y\left(x\right)=0;$
 ${\mathrm{ode}}{≔}\frac{{{ⅆ}}^{{2}}}{{ⅆ}{{x}}^{{2}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){+}{y}{}\left({x}\right){=}{0}$ (1.4.1)
 > $\mathrm{sol}≔\mathrm{dsolve}\left(\mathrm{ode}\right);$
 ${\mathrm{sol}}{≔}{y}{}\left({x}\right){=}{\mathrm{_C1}}{}{\mathrm{sin}}{}\left({x}\right){+}{\mathrm{_C2}}{}{\mathrm{cos}}{}\left({x}\right)$ (1.4.2)

The general solution has two constants, _C1 and _C2. To get a specific solution, include two initial value equations along with ode in dsolve.

 >
 ${\mathrm{iv1}}{≔}{y}{}\left({0}\right){=}{\mathrm{y1}}$
 ${\mathrm{iv2}}{≔}{\mathrm{D}}{}\left({y}\right){}\left({0}\right){=}{\mathrm{y2}}$ (1.4.3)
 > $\mathrm{sol}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{iv1},\mathrm{iv2}\right\}\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{odetest}\left(\mathrm{sol},\left\{\mathrm{ode},\mathrm{iv1},\mathrm{iv2}\right\}\right);$
 ${\mathrm{sol}}{≔}{y}{}\left({x}\right){=}{\mathrm{y2}}{}{\mathrm{sin}}{}\left({x}\right){+}{\mathrm{y1}}{}{\mathrm{cos}}{}\left({x}\right)$
 $\left\{{0}\right\}$ (1.4.4)

This verifies that we can always find a solution for this ODE given the initial values we defined.

Let's now look at the solutions we get from the same ODE as part of a two-point BVP. First, define two generic boundary value conditions at x = 0 and x = π. Our boundary values are linear combinations of y(x) and y'(x) at the boundaries.

 > $\mathrm{bc1}≔a\cdot y\left(0\right)+b\cdot y'\left(0\right)=f;$
 ${\mathrm{bc1}}{≔}{a}{}{y}{}\left({0}\right){+}{b}{}{\mathrm{D}}{}\left({y}\right){}\left({0}\right){=}{f}$ (1.4.5)
 > $\mathrm{bc2}≔c\cdot y\left(\mathrm{Pi}\right)+d\cdot y'\left(\mathrm{Pi}\right)=g;$
 ${\mathrm{bc2}}{≔}{c}{}{y}{}\left({\mathrm{\pi }}\right){+}{d}{}{\mathrm{D}}{}\left({y}\right){}\left({\mathrm{\pi }}\right){=}{g}$ (1.4.6)

When we solve the system consisting of ode, bc1, and bc2, we get the following. (Note that the solution exists and is unique as long as ad - bc ≠ 0.)

 > $\mathrm{solution}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\}\right);$
 ${\mathrm{solution}}{≔}{y}{}\left({x}\right){=}{-}\frac{\left({a}{}{g}{+}{c}{}{f}\right){}{\mathrm{sin}}{}\left({x}\right)}{{a}{}{d}{-}{b}{}{c}}{+}\frac{\left({b}{}{g}{+}{d}{}{f}\right){}{\mathrm{cos}}{}\left({x}\right)}{{a}{}{d}{-}{b}{}{c}}$ (1.4.7)
 > $\mathrm{odetest}\left(\mathrm{solution},\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\}\right);$
 $\left\{{0}\right\}$ (1.4.8)
 > $Y≔\mathrm{eval}\left(y\left(x\right),\mathrm{solution}\right);$
 ${Y}{≔}{-}\frac{\left({a}{}{g}{+}{c}{}{f}\right){}{\mathrm{sin}}{}\left({x}\right)}{{a}{}{d}{-}{b}{}{c}}{+}\frac{\left({b}{}{g}{+}{d}{}{f}\right){}{\mathrm{cos}}{}\left({x}\right)}{{a}{}{d}{-}{b}{}{c}}$ (1.4.9)

To analyze the solution, assign values to the parameters a, b, c, d, f, and g (ensuring, of course, that ad - bc ≠ 0).

 >
 ${a}{≔}{1}$
 ${b}{≔}{2}$
 ${c}{≔}{3}$
 ${d}{≔}{4}$
 ${f}{≔}\frac{{3}}{{4}}$
 ${g}{≔}{-2}$
 ${y}{}\left({0}\right){+}{2}{}{\mathrm{D}}{}\left({y}\right){}\left({0}\right){=}\frac{{3}}{{4}}$
 ${3}{}{y}{}\left({\mathrm{\pi }}\right){+}{4}{}{\mathrm{D}}{}\left({y}\right){}\left({\mathrm{\pi }}\right){=}{-2}$ (1.4.10)
 > $Y;$
 $\frac{{\mathrm{sin}}{}\left({x}\right)}{{8}}{+}\frac{{\mathrm{cos}}{}\left({x}\right)}{{2}}$ (1.4.11)
 > $\mathrm{plot}\left(Y,x=0..\mathrm{Pi}\right)$

We have found the case where we get a unique solution to the system. To see the other two possibilities (that is, no solution and an infinite number of solutions), let's examine what happens when we set a, b, c, and d such that ad - bc = 0. We do this by setting the boundary values to y(0) = 1 and y(π) = 0.

 >
 ${a}{≔}{1}$
 ${b}{≔}{0}$
 ${c}{≔}{1}$
 ${d}{≔}{0}$
 ${f}{≔}{1}$
 ${g}{≔}{0}$
 ${y}{}\left({0}\right){=}{1}$
 ${y}{}\left({\mathrm{\pi }}\right){=}{0}$ (1.4.12)
 > $\mathrm{sol}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\}\right);$
 ${\mathrm{sol}}{≔}\left(\right)$ (1.4.13)

We get no solution from dsolve. To understand why, recall that the general solution to our ODE is:

y(x) = _C1⋅sin(x) + _C2⋅cos(x)

When we apply our boundary values to the general solution, we get the following equations for the constants _C1 and _C2.

_C1⋅sin(0) + _C2⋅cos(0) = 1 ⇒ _C2 = 1
_C1⋅sin(π) + _C2⋅cos(π) = 1 ⇒ _C2 = 0

Our boundary values, therefore, do not allow for a solution to our system.

Finally, let's see what happens when we set the boundary values to y(0) = 1 and y(π) = -1.

 >
 ${g}{≔}{-1}$
 ${y}{}\left({0}\right){=}{1}$
 ${y}{}\left({\mathrm{\pi }}\right){=}{-1}$ (1.4.14)
 >
 ${\mathrm{sol}}{≔}{y}{}\left({x}\right){=}{\mathrm{_C1}}{}{\mathrm{sin}}{}\left({x}\right){+}{\mathrm{cos}}{}\left({x}\right)$ (1.4.15)
 > $\mathrm{odetest}\left(\mathrm{sol},\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\}\right);$
 $\left\{{0}\right\}$ (1.4.16)

We get a solution from dsolve, but it does not look complete. One of the constants in the general solution was found, but the other, _C1, remains in the solution. We therefore have infinitely many solutions to this BVP since any multiple of sin(x) can be added to cos(x). To understand why this happens, apply the boundary values to the general solution to get the following equations.

_C1⋅sin(0) + _C2⋅cos(0) = 1 ⇒ _C2 = 1
_C1⋅sin(π) + _C2⋅cos(π) = -1 ⇒ _C2 = 1

We can see that our boundary values enable us to solve for _C2, but give no criteria at all for _C1. This means that _C1 can take on any value, which is consistent with the solution found by dsolve.

 > 

Implicit Solutions with RootOf

The dsolve function always tries to return an explicit solution. If the solution cannot be expressed explicitly, dsolve returns a solution that contains a RootOf expression.

Consider the following ODE and initial condition.

 > $\mathrm{restart};$
 >

 ${\mathrm{odesys}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){=}\frac{{1}}{{\mathrm{cos}}{}\left({y}{}\left({x}\right)\right){+}{1.01}}{,}{y}{}\left({0}\right){=}{1}\right\}$ (1.5.1)
 > $\mathrm{sol}≔\mathrm{dsolve}\left(\mathrm{odesys}\right);$
 ${\mathrm{sol}}{≔}{y}{}\left({x}\right){=}{\mathrm{RootOf}}{}\left({101}{}{\mathrm{_Z}}{-}{100}{}{x}{+}{100}{}{\mathrm{sin}}{}\left({\mathrm{_Z}}\right){-}{101}{-}{100}{}{\mathrm{sin}}{}\left({1}\right)\right)$ (1.5.2)

The RootOf expression in the solution for this ODE tells us that an explicit solution for the ODE could not be found. (Note that odetest verifies that the solution does satisfy the ODE.)

 >
 $\left\{{0}\right\}$ (1.5.3)

There are two ways to solve this. The first is to call dsolve with the implicit option to force dsolve to return an implicit equation for y(x).

 > $\mathrm{sol1}≔\mathrm{dsolve}\left(\mathrm{odesys},\mathrm{implicit}\right);$
 ${\mathrm{sol1}}{≔}{x}{-}\frac{{101}{}{y}{}\left({x}\right)}{{100}}{-}{\mathrm{sin}}{}\left({y}{}\left({x}\right)\right){+}\frac{{101}}{{100}}{+}{\mathrm{sin}}{}\left({1}\right){=}{0}$ (1.5.4)

The second is to use the DEtools:-remove_RootOf command.

 > $\mathrm{with}\left(\mathrm{DEtools}\right):$
 > $\mathrm{sol2}≔\mathrm{remove_RootOf}\left(\mathrm{sol}\right);$
 ${\mathrm{sol2}}{≔}{101}{}{y}{}\left({x}\right){-}{100}{}{x}{+}{100}{}{\mathrm{sin}}{}\left({y}{}\left({x}\right)\right){-}{101}{-}{100}{}{\mathrm{sin}}{}\left({1}\right){=}{0}$ (1.5.5)

To graph the solution, use the plots:-implicitplot command.

 > $\mathrm{with}\left(\mathrm{plots}\right):$
 >

For this example we can use eval  and fsolve to find the value for x given a value of y(x).

 >
 ${\mathrm{eq}}{≔}{707}{-}{100}{}{x}{+}{100}{}{\mathrm{sin}}{}\left({8}\right){-}{100}{}{\mathrm{sin}}{}\left({1}\right){=}{0}$ (1.5.6)
 > $\mathrm{fsolve}\left(\mathrm{eq},x\right);$
 ${7.217887262}$ (1.5.7)
 > 

Solving an ODE Numerically

Use the numeric parameter with dsolve to get a numerical solution for your ODE or ODE system. The numerical solution is a procedure that you execute to analyze.

 – Basic Example: y' = -y
 – Basic Example with Parameters: y' = a y
 – Solving a System of ODEs Numerically
 – Taking Derivatives and Integrals of Numeric Solutions
 – Techniques for Solving a Nonlinear Two-Point BVP
 – Solving Piecewise ODEs and an Introduction to Event Handling
 – Finding Roots Using Events and the Halt Action
 – An Advanced Example with Piecewise ODEs and Event Detection

Basic Example: y' = -y

This example shows how to use dsolve get a numerical solution to .

Define a variable called basicOde, a set that contains the ODE and the initial condition, y(0)=5.

 > $\mathrm{restart};$
 >
 ${\mathrm{basicOde}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){=}{-}{y}{}\left({x}\right){,}{y}{}\left({0}\right){=}{5}\right\}$ (2.1.1)

To get a numerical solution, use dsolve with the numeric parameter.

 > $\mathrm{sol}≔\mathrm{dsolve}\left(\mathrm{basicOde},\mathrm{numeric}\right);$
 ${\mathrm{sol}}{≔}{\mathbf{proc}}\left({\mathrm{x_rkf45}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.1.2)

The output is a procedure that you run to analyze the solution to your ODE. In the output expression, the x_rkf45 term tells you the numerical method used in the solution (in this case rkf45, a Fehlberg fourth-fifth order Runge-Kutta).

To get a value for your solution at a point, run the procedure with the point as the parameter. The following command finds the value for y when x=5.0. The output is a list of equations. The first element in the list echoes your input. The second element gives you the value of y(x).

 > $\mathrm{sol}\left(5.0\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}$
 $\left[{x}{=}{5.0}{,}{y}{}\left({x}\right){=}{0.0336896836290933}\right]$ (2.1.3)
 > $\mathrm{sol}\left(0.916\right);$
 $\left[{x}{=}{0.916}{,}{y}{}\left({x}\right){=}{2.00058112757547}\right]$ (2.1.4)

Use the plots:-odeplot command to graph your solution. The odeplot command accepts the solution from dsolve/numeric as its first parameter. The following commands plot sol over x=0..5.

 > $\mathrm{with}\left(\mathrm{plots}\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol},x=0..5\right);$

Note that this solution is very specific in terms of the initial conditions and the terms in the ODE. The next section shows how you can generalize the solution by using the parameters option with dsolve/numeric.

Assigning the Solution to a Variable

The procedure that dsolve returns is useful for finding the value of the solution at a point and plotting the solution with odeplot, but it cannot be used in fsolve. For example, let's say you want to find the value of x for which  y(x)=2. Using sol directly in fsolve returns the same expression unevaluated.

 > $\mathrm{fsolve}\left(\mathrm{sol}\left(x\right)=2,x\right);$
 ${\mathrm{fsolve}}{}\left({\mathrm{sol}}{}\left({x}\right){=}{2}{,}{x}\right)$ (2.1.5)

To solve this, use the output=listprocedure option with dsolve, and then assign the solution to a variable with the eval(expression,equations) command.

 > $\mathrm{sol}≔\mathrm{dsolve}\left(\mathrm{basicOde},\mathrm{numeric},\mathrm{output}=\mathrm{listprocedure}\right);$
 ${\mathrm{sol}}{≔}\left[{x}{=}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{y}{}\left({x}\right){=}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (2.1.6)
 > $Y≔\mathrm{eval}\left(y\left(x\right),\mathrm{sol}\right);$
 ${Y}{≔}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.1.7)
 > $a≔\mathrm{fsolve}\left(Y\left(x\right)=2,x\right);$
 ${a}{≔}{0.9162905219}$ (2.1.8)

The following commands verify that the output from sol and Y are identical (and that the result from fsolve is correct).

 > $\mathrm{sol}\left(a\right);$
 $\left[{x}{}\left({0.9162905219}\right){=}{0.9162905219}{,}{y}{}\left({x}\right){}\left({0.9162905219}\right){=}{1.99999999996477}\right]$ (2.1.9)
 > $Y\left(a\right);$
 ${1.99999999996477}$ (2.1.10)

With the solution assigned to a variable, you can use the standard plot command to graph your solution instead of plots:-odeplot.

 > $\mathrm{plot}\left(Y,0..5\right);$
 > 

Basic Example with Parameters: y' = a y

This example shows how to use dsolve get a numerical solution to  with the initial condition y(x0) = y0. The terms a, x0, and y0 are parameters that you will be able to set after the solution has been generated.

Define a variable called basicOde2, a set that contains the ODE and the initial condition, y(x0) = y0.

 > $\mathrm{restart};$
 >
 ${\mathrm{basicOde2}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){=}{a}{}{y}{}\left({x}\right){,}{y}{}\left({\mathrm{x0}}\right){=}{\mathrm{y0}}\right\}$ (2.2.1)

Use dsolve to get a numerical solution. Also, because basicOde2 contains the parameters a, x0, and y0,use the parameters option.

 >
 ${\mathrm{sol2}}{≔}{\mathbf{proc}}\left({\mathrm{x_rkf45}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.2.2)

Before you can work with the solution, you must initialize the parameters a, x0, and y0. To initialize the parameters, run the output procedure with values inserted in the parameters list. The following sets a to -1, x0 to 0, and y0 to 5.

 > $\mathrm{sol2}\left(\mathrm{parameters}=\left[-1,0,5\right]\right);$
 $\left[{a}{=}{-1.}{,}{\mathrm{x0}}{=}{0.}{,}{\mathrm{y0}}{=}{5.}\right]$ (2.2.3)

To get a value for your solution at a point, run the procedure with the point given as the argument. The following command finds the value for y when x=5.0. The output is a list of equations. The first element in the list echoes your input. The second element gives you the value of y(x).

 > $\mathrm{sol2}\left(5.0\right);$
 $\left[{x}{=}{5.0}{,}{y}{}\left({x}\right){=}{0.0336896836290933}\right]$ (2.2.4)

The following graphs sol2 over x=0..5.

 > $\mathrm{with}\left(\mathrm{plots}\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol2},x=0..5\right);$

At any time you can change the values for the parameters to get a different solution for your system.

 > $\mathrm{sol2}\left(\mathrm{parameters}=\left[-\frac{1}{3},0,5\right]\right);$
 $\left[{a}{=}{-0.333333333333333}{,}{\mathrm{x0}}{=}{0.}{,}{\mathrm{y0}}{=}{5.}\right]$ (2.2.5)
 > $\mathrm{odeplot}\left(\mathrm{sol2},x=0..5\right);$

Finally, if you want to analyze the solution using fsolve, you must generate a solution using the output=listprocedure option, and then assign the solution to a variable.

 >
 ${\mathrm{sol2}}{≔}\left[{x}{=}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{y}{}\left({x}\right){=}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (2.2.6)
 > $Y≔\mathrm{eval}\left(y\left(x\right),\mathrm{sol2}\right);$
 ${Y}{≔}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.2.7)

Before you can use the function, you must set values for the parameters a, x0, and y0.

 > $Y\left(\mathrm{parameters}=\left[-1,0,5\right]\right);$
 $\left[{a}{=}{-1.}{,}{\mathrm{x0}}{=}{0.}{,}{\mathrm{y0}}{=}{5.}\right]$ (2.2.8)

You can now use fsolve on the solution. The following finds the value of x for which the solution equals 2.

 > $b≔\mathrm{fsolve}\left(Y\left(x\right)=2,x\right);$
 ${b}{≔}{0.9162905219}$ (2.2.9)
 > $Y\left(b\right);$
 ${1.99999999996477}$ (2.2.10)
 > 

Solving a System of ODEs Numerically

Define the system of ODEs along with their initial conditions. The system of ODEs in this example is the Lotka-Volterra predator-prey system, where x(t) represents the population of the prey and y(t) the population of the predator.

 > $\mathrm{restart};$
 >
 ${\mathrm{des}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{x}{}\left({t}\right){=}{a}{}{x}{}\left({t}\right){-}{b}{}{x}{}\left({t}\right){}{y}{}\left({t}\right){,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({t}\right){=}{-}{c}{}{y}{}\left({t}\right){+}{d}{}{x}{}\left({t}\right){}{y}{}\left({t}\right){,}{x}{}\left({\mathrm{t0}}\right){=}{\mathrm{x0}}{,}{y}{}\left({\mathrm{t0}}\right){=}{\mathrm{y0}}\right\}$ (2.3.1)

Generate the solution with a list of parameters.

 > $\mathrm{desSol}≔\mathrm{dsolve}\left(\mathrm{des},\mathrm{numeric},\mathrm{parameters}=\left[a,b,c,d,\mathrm{t0},\mathrm{x0},\mathrm{y0}\right]\right);$
 ${\mathrm{desSol}}{≔}{\mathbf{proc}}\left({\mathrm{x_rkf45}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.3.2)

Before working with the solution, set values for the parameters.

 > $\mathrm{desSol}\left(\mathrm{parameters}=\left[0.1,0.01,0.025,0.001,0,50,15\right]\right);$
 $\left[{a}{=}{0.1}{,}{b}{=}{0.01}{,}{c}{=}{0.025}{,}{d}{=}{0.001}{,}{\mathrm{t0}}{=}{0.}{,}{\mathrm{x0}}{=}{50.}{,}{\mathrm{y0}}{=}{15.}\right]$ (2.3.3)

To find the populations at a particular time, run the solution with the time given as the argument. The result is a list of equations for the independent variable (t), the prey population (x(t)), and the predator population (y(t)).

 > $\mathrm{desSol}\left(90\right);$
 $\left[{t}{=}{90.}{,}{x}{}\left({t}\right){=}{20.0599816840952}{,}{y}{}\left({t}\right){=}{5.29175138992018}\right]$ (2.3.4)

To graph multiple curves with odeplot, use a nested list that contains a list for each pair of coordinates you would like to graph. For example, for the following odeplot command graph, both the prey and predator populations are displayed on the same graph.

 > $\mathrm{with}\left(\mathrm{plots}\right):$
 >

If three terms are given for the variables list, a 3-D graph is produced.

 > $\mathrm{odeplot}\left(\mathrm{desSol},\left[t,x\left(t\right),y\left(t\right)\right],0..300\right);$

Finally, a parametric graph of the solution, with the prey population on the horizontal axis and the predator population on the vertical axis.

 >

Assigning Solutions to Variables

If you want to analyze your solution using fsolve or other commands, use the output=listprocedure option with dsolve and then assign the solutions to variables.

 > $\mathrm{desSol}≔\mathrm{dsolve}\left(\mathrm{des},\mathrm{numeric},\mathrm{parameters}=\left[a,b,c,d,\mathrm{t0},\mathrm{x0},\mathrm{y0}\right],\mathrm{output}=\mathrm{listprocedure}\right);$
 ${\mathrm{desSol}}{≔}\left[{t}{=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{x}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{y}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (2.3.5)
 > $X≔\mathrm{eval}\left(x\left(t\right),\mathrm{desSol}\right);$
 ${X}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.3.6)
 > $Y≔\mathrm{eval}\left(y\left(t\right),\mathrm{desSol}\right);$
 ${Y}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.3.7)

Assigning parameter values for either X or Y automatically assigns the parameter values for the other variable.

 > $X\left(\mathrm{parameters}=\left[0.1,0.01,0.025,0.001,0,50,15\right]\right);$
 $\left[{a}{=}{0.1}{,}{b}{=}{0.01}{,}{c}{=}{0.025}{,}{d}{=}{0.001}{,}{\mathrm{t0}}{=}{0.}{,}{\mathrm{x0}}{=}{50.}{,}{\mathrm{y0}}{=}{15.}\right]$ (2.3.8)
 > $Y\left(\mathrm{parameters}\right);$
 $\left[{a}{=}{0.1}{,}{b}{=}{0.01}{,}{c}{=}{0.025}{,}{d}{=}{0.001}{,}{\mathrm{t0}}{=}{0.}{,}{\mathrm{x0}}{=}{50.}{,}{\mathrm{y0}}{=}{15.}\right]$ (2.3.9)
 > $\mathrm{plot}\left(\left[X\left(t\right),Y\left(t\right)\right],t=0..150,\mathrm{labels}=\left["t","Population"\right],\mathrm{legend}=\left["x\left(t\right) - Prey","y\left(t\right) - Predator"\right]\right);$

The following command uses fsolve to find the first time the populations x(t) and y(t) are equal.

 > $a≔\mathrm{fsolve}\left(X\left(t\right)=Y\left(t\right),t\right);$
 ${a}{≔}{16.82819661}$ (2.3.10)
 > $X\left(a\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}Y\left(a\right);$
 ${16.5912730797044}$
 ${16.5912730836271}$ (2.3.11)
 > 

Taking Derivatives and Integrals of Numeric Solutions

To find the derivative or integral of a numeric solution, you should include ODEs for the derivative or integral in your system of equations.

For this example, we have a first-order differential equation in v(t), the velocity of an object, along with an initial condition.

 > $\mathrm{restart};$
 > $\mathrm{velocityEqs}≔\mathrm{diff}\left(v\left(t\right),t\right)=-1+v{\left(t\right)}^{2},v\left(0\right)=\mathrm{v0};$
 ${\mathrm{velocityEqs}}{≔}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{v}{}\left({t}\right){=}{-}{1}{+}{{v}{}\left({t}\right)}^{{2}}{,}{v}{}\left({0}\right){=}{\mathrm{v0}}$ (2.4.1)

Let's suppose that in addition to the velocity we want to find the object's acceleration, a(t), and position, s(t). We could use the preceding equations to find v(t), and then take the derivative and integral of v(t) to find the acceleration and position. However, taking the derivative or integral of a numeric solution is not recommended (see Why You Should Not Use Int or Diff on a Numeric Solution).

Instead, you should define an augmented system of ODEs that includes the ODEs and initial conditions for the object's velocity, acceleration, and position. In the following command, we define equations, an augmented system that contains the equations for the acceleration and position.

 >
 ${\mathrm{equations}}{≔}\left\{{a}{}\left({t}\right){=}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{v}{}\left({t}\right){,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{s}{}\left({t}\right){=}{v}{}\left({t}\right){,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{v}{}\left({t}\right){=}{-}{1}{+}{{v}{}\left({t}\right)}^{{2}}{,}{s}{}\left({0}\right){=}{\mathrm{s0}}{,}{v}{}\left({0}\right){=}{\mathrm{v0}}\right\}$ (2.4.2)

Generate the solution with parameters for v0 and s0.

 > $\mathrm{solution}≔\mathrm{dsolve}\left(\mathrm{equations},\mathrm{numeric},\mathrm{parameters}=\left[\mathrm{v0},\mathrm{s0}\right],\mathrm{output}=\mathrm{listprocedure}\right);$
 ${\mathrm{solution}}{≔}\left[{t}{=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{a}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{s}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{v}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (2.4.3)

Set the initial velocity and initial position both to zero.

 > $\mathrm{solution}\left(\mathrm{parameters}=\left[0,0\right]\right);$
 $\left[{t}{}\left({\mathrm{parameters}}{=}\left[{0}{,}{0}\right]\right){=}\left[{\mathrm{v0}}{=}{0.}{,}{\mathrm{s0}}{=}{0.}\right]{,}{a}{}\left({t}\right){}\left({\mathrm{parameters}}{=}\left[{0}{,}{0}\right]\right){=}\left[{\mathrm{v0}}{=}{0.}{,}{\mathrm{s0}}{=}{0.}\right]{,}{s}{}\left({t}\right){}\left({\mathrm{parameters}}{=}\left[{0}{,}{0}\right]\right){=}\left[{\mathrm{v0}}{=}{0.}{,}{\mathrm{s0}}{=}{0.}\right]{,}{v}{}\left({t}\right){}\left({\mathrm{parameters}}{=}\left[{0}{,}{0}\right]\right){=}\left[{\mathrm{v0}}{=}{0.}{,}{\mathrm{s0}}{=}{0.}\right]\right]$ (2.4.4)

Assign the velocity, acceleration, and position to the variables V, A, and S, respectively.

 > $V≔\mathrm{eval}\left(v\left(t\right),\mathrm{solution}\right);$
 ${V}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.4.5)
 > $A≔\mathrm{eval}\left(a\left(t\right),\mathrm{solution}\right);$
 ${A}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.4.6)
 > $S≔\mathrm{eval}\left(s\left(t\right),\mathrm{solution}\right);$
 ${S}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.4.7)

Finally, use plot to plot the velocity, acceleration, and position.

 > $\mathrm{plot}\left(\left[V\left(t\right),A\left(t\right),S\left(t\right)\right],t=0..3,\mathrm{legend}=\left["V\left(t\right) - velocity","A\left(t\right) - acceleration","S\left(t\right) - position"\right]\right);$

Why You Should Not Use Int or Diff on a Numeric Solution

Although it is possible to use the Int or Diff commands to numerically integrate or differentiate the procedure from a numeric solution, doing so will lead to results that will either take much longer to calculate, be inaccurate, or both. The following example illustrates this.

First, we'll define a second set of equations with only the velocity, and then we'll assign the solution for the velocity to V2.

 >
 ${\mathrm{equations2}}{≔}\left\{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{v}{}\left({t}\right){=}{-}{1}{+}{{v}{}\left({t}\right)}^{{2}}{,}{v}{}\left({0}\right){=}{0}\right\}$ (2.4.1.1)
 > $\mathrm{sol2}≔\mathrm{dsolve}\left(\mathrm{equations2},\mathrm{numeric},\mathrm{output}=\mathrm{listprocedure}\right);$
 ${\mathrm{sol2}}{≔}\left[{t}{=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{v}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (2.4.1.2)
 > $\mathrm{V2}≔\mathrm{eval}\left(v\left(t\right),\mathrm{sol2}\right);$
 ${\mathrm{V2}}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.4.1.3)

Next, we'll define SInt by integrating our numerical solution, V2.

 > $\mathrm{SInt}≔T\to \mathrm{evalf}\left(\mathrm{Int}\left(\mathrm{V2}\left(t\right),t=0..T\right)\right);$
 ${\mathrm{SInt}}{≔}{T}{↦}{\mathrm{evalf}}{}\left({{\int }}_{{0}}^{{T}}{\mathrm{V2}}{}\left({t}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{t}\right)$ (2.4.1.4)

The following commands compare the results from SInt with S and the exact solution, -ln(cosh(T)) (which was found in the example on taking integrals and derivatives of symbolic solutions).

 > $\mathrm{CodeTools}:-\mathrm{Usage}\left(\mathrm{SInt}\left(6\right)\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{CodeTools}:-\mathrm{Usage}\left(S\left(6\right)\right);\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{evalf}\left(-\mathrm{ln}\left(\mathrm{cosh}\left(6\right)\right)\right);$

 memory used=18.88MiB, alloc change=36.00MiB, cpu time=296.00ms, real time=728.00ms, gc time=31.20ms
 ${-5.30685989107477}$
 memory used=8.88KiB, alloc change=0 bytes, cpu time=0ns, real time=2.00ms, gc time=0ns
 ${-5.30685896501487}$
 ${-5.306858964}$ (2.4.1.5)

Notice that SInt is takes longer to calculate the integral and is also less accurate when compared to the exact solution.

We will now use the Diff function to find ADiff, the derivative of V2, and compare that with A and the exact solution, tanh(t)2−1.

 > $\mathrm{ADiff}≔T\to \mathrm{eval}\left(\mathrm{Diff}\left(\mathrm{V2}\left(t\right),t\right),t=T\right);$
 ${\mathrm{ADiff}}{≔}{T}{↦}\genfrac{}{}{0}{}{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{\mathrm{V2}}{}\left({t}\right)}{\phantom{{t}{=}{T}}}{|}\genfrac{}{}{0}{}{\phantom{\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{\mathrm{V2}}{}\left({t}\right)}}{{t}{=}{T}}$ (2.4.1.6)

The following commands compare ADiff with A and the exact solution, tanh(t)2−1.

 >
 memory used=110.88KiB, alloc change=0 bytes, cpu time=0ns, real time=27.00ms, gc time=0ns
 ${-0.00002442061000}$
 memory used=7.10KiB, alloc change=0 bytes, cpu time=0ns, real time=0ns, gc time=0ns
 ${-0.0000245764945147024}$
 ${-0.0000245764}$ (2.4.1.7)

The result from ADiff is less accurate than the result from A.

This should convince you that it is always better to augment your system of ODEs with equations for the integral or derivative of your solution.

 > 

Techniques for Solving a Nonlinear Two-Point BVP

This example looks at a nonlinear two-point boundary value problem (BVP). The system consists of the following nonlinear ODE and two nonlinear boundary conditions (bc1 and bc2).

 > $\mathrm{restart};$
 >
 ${\mathrm{ode}}{≔}\frac{{{ⅆ}}^{{2}}}{{ⅆ}{{x}}^{{2}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right){+}{{y}{}\left({x}\right)}^{{2}}{}\left(\frac{{ⅆ}}{{ⅆ}{x}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{y}{}\left({x}\right)\right){+}{\mathrm{cos}}{}\left({y}{}\left({x}\right)\right){=}{x}$ (2.5.1)
 > $\mathrm{bc1}≔\left(1+y\left(0\right)\right)\cdot y'\left(0\right)-3\cdot y\left(0\right)=0;$
 ${\mathrm{bc1}}{≔}\left({1}{+}{y}{}\left({0}\right)\right){}{\mathrm{D}}{}\left({y}\right){}\left({0}\right){-}{3}{}{y}{}\left({0}\right){=}{0}$ (2.5.2)
 >
 ${\mathrm{bc2}}{≔}\left({1}{+}{y}{}\left({1}\right)\right){}{\mathrm{D}}{}\left({y}\right){}\left({1}\right){-}{3}{}{y}{}\left({1}\right){-}{{y}{}\left({1}\right)}^{{2}}{+}{0.5}{=}{0}$ (2.5.3)

We want to find all of the solutions to this system in the interval $0\le x\le 1$.

A first attempt at using dsolve/numeric fails.

 > $\mathrm{solution}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\},\mathrm{numeric}\right);$

Despite this, we will show that this system can be solved and we will give two techniques for finding the solutions.

The first technique finds a solution by giving dsolve an approximate solution (that is, by using the approxsoln option).

The second technique finds all of the solutions for the nonlinear BVP by turning the BVP into an initial value problem.

Getting One Solution Using approxsoln

For some nonlinear boundary conditions it is necessary to give dsolve an initial approximation to the solution. To provide the initial approximation, use the approxsoln option. This forces dsolve to try finding a solution that is close to the initial approximation.

For our system, let's try using $y\left(x\right)=x$ as our initial approximation.

 >
 ${\mathrm{solution}}{≔}{\mathbf{proc}}\left({\mathrm{x_bvp}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.5.4)
 > $\mathrm{plots}:-\mathrm{odeplot}\left(\mathrm{solution}\right);$

We succeeded in finding a solution, but we do not know if this is the only solution or if there are more solutions to our system. The next section demonstrates how to find all of the solutions that exist for the system.

Finding all possible solutions

One option to find other solutions for our nonlinear two-point BVP is to try different initial approximations. This, however, would be tedious and would not guarantee that every solution is found.

A better approach is to look at the boundary conditions we have and use them to turn our nonlinear two-point BVP into an initial value problem and from there try finding all of the solutions to the ODE. That is, we will implement the shooting method.

Our boundary conditions consist of two equations with four unknowns (y(0), y'(0), y(1), and y'(1)):

$\left(1+y\left(0\right)\right)\cdot y'\left(0\right)-3\cdot y\left(0\right)=0$

$\left(1+y\left(1\right)\right)\cdot y'\left(1\right)-3\cdot y\left(1\right)-{\left(y\left(1\right)\right)}^{2}+0.5=0$

We want to rewrite these equations in terms of two unknowns, y(0) and y'(0), so that we can solve for these unknowns and then use them as the initial values for our ODE.

Start by making the substitutions y(0) = a and y'(0) = b in the first equation:

$\left(1+a\right)\cdot b-3\cdot a=0$

$\left(1+y\left(1\right)\right)\cdot y'\left(1\right)-3\cdot y\left(1\right)-{\left(y\left(1\right)\right)}^{2}+0.5=0$

Next, define a procedure, h( a, b), that, given the initial values a and b, numerically integrates our ODE and returns the endpoint values, y(1) and y'(1), in a list.

 >

We can now substitute h(a, b)[1] for y(1) and h(a, b)[2] for y'(1) in our second equation. This gives us two equations with two unknowns.

$\left(1+a\right)\cdot b-3\cdot a=0$

$\left(1+\left(h\left(a,b\right)\left[1\right]\right)\right)\cdot \left(h\left(a,b\right)\left[2\right]\right)-3\cdot \left(h\left(a,b\right)\left[1\right]\right)-{\left(h\left(a,b\right)\left[1\right]\right)}^{2}+0.5=0$

Before we can solve for the (a, b) pairs, we need to redefine our boundary conditions as procedures. This makes it easier to analyze our boundary conditions with the implicitplot and fsolve commands.

 >
 ${\mathrm{BC1}}{≔}{\mathbf{proc}}\left({a}{,}{b}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left({1}{+}{a}\right){*}{b}{-}{3}{*}{a}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.5.5)
 >
 ${\mathrm{BC2}}{≔}{\mathbf{proc}}\left({a}{,}{b}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left({1}{+}{h}{}\left({a}{,}{b}\right){[}{1}{]}\right){*}{h}{}\left({a}{,}{b}\right){[}{2}{]}{-}{3}{*}{h}{}\left({a}{,}{b}\right){[}{1}{]}{-}{h}{}\left({a}{,}{b}\right){[}{1}{]}{^}{2}{+}{0.5}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.5.6)

In the following implicit graph of BC1(a, b) = 0 and BC2(a, b) = 0, the points where the curves intersect are the (a, b) pairs that solve our BVP. There are four solutions to our BVP. Note that the solution we found using approxsoln should correspond to the point of intersection in the first quadrant (since that is the only solution with a > 0).

 > $\mathrm{with}\left(\mathrm{plots}\right):$
 >

To find the values for (a, b), use the fsolve command with an interval around each of the points of intersection.

 >
 ${\mathrm{S1}}{≔}\left[{-3.5359390185871604}{,}{4.1829937463052169}\right]$
 ${\mathrm{S2}}{≔}\left[{-2.0022359204985014}{,}{5.9933072030663520}\right]$
 ${\mathrm{S3}}{≔}\left[{-0.77342410180917261}{,}{-10.240596303289645}\right]$
 ${\mathrm{S4}}{≔}\left[{0.11555841921815774}{,}{0.31076387545660007}\right]$ (2.5.7)

We can now find all of the solutions for the BVP by using the (a, b) pairs as initial values. As expected, S4 (the (a, b) pair in the first quadrant) corresponds to the solution we found using the approxsoln option.

 > $\mathrm{sol1}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},y\left(0\right)=\mathrm{S1}\left[1\right],y'\left(0\right)=\mathrm{S1}\left[2\right]\right\},\mathrm{numeric}\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol1},x=0..1\right);$
 > 
 > $\mathrm{sol2}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},y\left(0\right)=\mathrm{S2}\left[1\right],y'\left(0\right)=\mathrm{S2}\left[2\right]\right\},\mathrm{numeric}\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol2},x=0..1\right);$
 > 
 > $\mathrm{sol3}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},y\left(0\right)=\mathrm{S3}\left[1\right],y'\left(0\right)=\mathrm{S3}\left[2\right]\right\},\mathrm{numeric}\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol3},x=0..1\right);$
 > 
 > $\mathrm{sol4}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},y\left(0\right)=\mathrm{S4}\left[1\right],y'\left(0\right)=\mathrm{S4}\left[2\right]\right\},\mathrm{numeric}\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol4},x=0..1\right);$
 > 

We also could have arrived at the solutions if we had used appropriate initial solutions with approxsoln. The following is a list of initial approximations, along with the solutions derived from them.

 >
 ${L}{≔}\left[{x}{-}{4}{,}{3}{}{x}{-}{2}{,}{-}{10}{}{x}{,}{x}{}\left({1}{-}{x}\right)\right]$ (2.5.8)

 > $\mathrm{sol1}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\},y\left(x\right),\mathrm{numeric},\mathrm{approxsoln}=\left[y\left(x\right)=L\left[1\right]\right]\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol1},x=0..1\right);$
 > 
 > $\mathrm{sol2}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\},y\left(x\right),\mathrm{numeric},\mathrm{approxsoln}=\left[y\left(x\right)=L\left[2\right]\right]\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol2},x=0..1\right);$
 > 
 > $\mathrm{sol3}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\},y\left(x\right),\mathrm{numeric},\mathrm{approxsoln}=\left[y\left(x\right)=L\left[3\right]\right]\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol3},x=0..1\right);$
 > 
 > $\mathrm{sol4}≔\mathrm{dsolve}\left(\left\{\mathrm{ode},\mathrm{bc1},\mathrm{bc2}\right\},y\left(x\right),\mathrm{numeric},\mathrm{approxsoln}=\left[y\left(x\right)=L\left[4\right]\right]\right):$
 > $\mathrm{odeplot}\left(\mathrm{sol4},x=0..1\right);$
 > 

 > 

Solving Piecewise ODEs and an Introduction to Event Handling

The dsolve/numeric command handles ODEs that are defined using the piecewise command.

The following example describes the motion of an object that is undergoing a constant downward acceleration due to gravity and a drag force that is proportional to the square of its velocity. The direction of the drag force is opposite to the direction of the object's velocity.

 > $\mathrm{restart};$
 >
 ${\mathrm{acceleration}}{≔}\frac{{{ⅆ}}^{{2}}}{{ⅆ}{{t}}^{{2}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right){=}\left\{\begin{array}{cc}{-}{9.81}{+}{6}{}{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right)\right)}^{{2}}& \frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right){\le }{0}\\ {-}{9.81}{-}{6}{}{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right)\right)}^{{2}}& {\mathrm{otherwise}}\end{array}\right\$ (2.6.1)

The following assignments set the initial conditions. At t = 0 the object is at the origin (h = 0) and has an initial velocity of 20.

Note: For the initial velocity, we use the differential operator (D) to represent the derivative of h evaluated at a point.

 > $\mathrm{initialPosition}≔h\left(0\right)=0;\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathrm{initialVelocity}≔\mathrm{D}\left(h\right)\left(0\right)=20;$
 ${\mathrm{initialPosition}}{≔}{h}{}\left({0}\right){=}{0}$
 ${\mathrm{initialVelocity}}{≔}{\mathrm{D}}{}\left({h}\right){}\left({0}\right){=}{20}$ (2.6.2)

Now we'll find the solution and plot it.

 > $\mathrm{solution}≔\mathrm{dsolve}\left(\left\{\mathrm{acceleration},\mathrm{initialPosition},\mathrm{initialVelocity}\right\},\mathrm{numeric}\right);$
 ${\mathrm{solution}}{≔}{\mathbf{proc}}\left({\mathrm{x_rkf45}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.6.3)
 > $\mathrm{with}\left(\mathrm{plots}\right):$
 > $\mathrm{plots}:-\mathrm{odeplot}\left(\mathrm{solution},0..1\right);$

The first thing we notice is that the solution handled the piecewise ODE properly. However, it would be nice if we could have the object's velocity reverse when it hits the ground to simulate a bouncing object.

To do this, we'll use the events=[[trigger, action]] option. The events option makes the numerical solver look for the trigger condition between integration steps. When the trigger is found, the solver performs the associated action.

In the following command, the trigger is set to h(t)=0: this fires an event whenever the solution detects the object hitting the ground. The action term is set to diff(h(t),t)=-diff(h(t),t): this reverses the direction of the velocity so that the object bounces up. (For more information on how to set triggers and actions, see dsolve,numeric,Events.)

 >
 ${\mathrm{solution}}{≔}{\mathbf{proc}}\left({\mathrm{x_rkf45}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.6.4)
 > $\mathrm{odeplot}\left(\mathrm{solution},0..1\right);$

 > 

Finding Roots Using Events and the Halt Action

In most cases fsolve is sufficient to find all of the roots of your solution. However, there are cases where fsolve will either fail to find a root or you will need to specify a range in order for fsolve to find a root. In such cases, you can use the halt action to stop the solution every time a root is found.

We start by defining an ODE and then plotting the solution to see where the roots for our solution are.

 > $\mathrm{restart};$
 > $\mathrm{odes}≔\left\{\mathrm{diff}\left(h\left(t\right),t,t\right)=-9.81-6\cdot \mathrm{diff}{\left(h\left(t\right),t\right)}^{2},h\left(0\right)=\mathrm{h0},\mathrm{D}\left(h\right)\left(0\right)=\mathrm{v0}\right\};$
 ${\mathrm{odes}}{≔}\left\{\frac{{{ⅆ}}^{{2}}}{{ⅆ}{{t}}^{{2}}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right){=}{-}{9.81}{-}{6}{}{\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right)\right)}^{{2}}{,}{h}{}\left({0}\right){=}{\mathrm{h0}}{,}{\mathrm{D}}{}\left({h}\right){}\left({0}\right){=}{\mathrm{v0}}\right\}$ (2.7.1)
 > $\mathrm{sol}≔\mathrm{dsolve}\left(\mathrm{odes},\mathrm{numeric},\mathrm{parameters}=\left[\mathrm{h0},\mathrm{v0}\right],\mathrm{output}=\mathrm{listprocedure}\right);$
 ${\mathrm{sol}}{≔}\left[{t}{=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}{h}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}{,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right){=}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}\right]$ (2.7.2)

We will set the initial position to a very small negative value and the initial velocity to a positive value so that the solution has to cross zero very close to t=0.

 > $\mathrm{sol}\left(\mathrm{parameters}=\left[-0.001,30\right]\right);$
 $\left[{t}{}\left({\mathrm{parameters}}{=}\left[{-0.001}{,}{30}\right]\right){=}\left[{\mathrm{h0}}{=}{-0.001}{,}{\mathrm{v0}}{=}{30.}\right]{,}{h}{}\left({t}\right){}\left({\mathrm{parameters}}{=}\left[{-0.001}{,}{30}\right]\right){=}\left[{\mathrm{h0}}{=}{-0.001}{,}{\mathrm{v0}}{=}{30.}\right]{,}\left(\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right)\right){}\left({\mathrm{parameters}}{=}\left[{-0.001}{,}{30}\right]\right){=}\left[{\mathrm{h0}}{=}{-0.001}{,}{\mathrm{v0}}{=}{30.}\right]\right]$ (2.7.3)

Plotting the solution shows that we can expect to find two roots between t=0 and t=0.4.

 > $H≔\mathrm{eval}\left(h\left(t\right),\mathrm{sol}\right);$
 ${H}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.7.4)
 > $\mathrm{plot}\left(H,0..0.4\right);$

Now try using the fsolve function to find the roots.

 > $\mathrm{fsolve}\left(H\left(t\right)=0,t\right);$
 ${0.3983488244}$ (2.7.5)

We only found the second of the two roots. To find the first root, we can give fsolve an interval to search in.

 > $\mathrm{fsolve}\left(H\left(t\right)=0,t=0..0.1\right);$
 ${\mathrm{fsolve}}{}\left({H}{}\left({t}\right){=}{0}{,}{t}{,}{0}{..}{0.1}\right)$ (2.7.6)

Again, fsolve failed to find the first root. We will try again, using smaller and smaller intervals.

 > $\mathrm{fsolve}\left(H\left(t\right)=0,t=0..0.05\right);$
 ${\mathrm{fsolve}}{}\left({H}{}\left({t}\right){=}{0}{,}{t}{,}{0}{..}{0.05}\right)$ (2.7.7)
 > $\mathrm{fsolve}\left(H\left(t\right)=0,t=0..0.01\right);$
 ${0.00003343371658}$ (2.7.8)

It took several tries (and some knowledge of what we were looking for) to find both roots. Obviously, this isn't ideal.

Let's see what happens when we use the events option to find the roots. For this, we'll set the trigger to h(t)=0 and the action to halt. The halt action stops the solution every time the event triggers.

 > $\mathrm{sol2}≔\mathrm{dsolve}\left(\mathrm{odes},\mathrm{numeric},\mathrm{parameters}=\left[\mathrm{h0},\mathrm{v0}\right],\mathrm{events}=\left[\left[h\left(t\right)=0,\mathrm{halt}\right]\right]\right);$
 ${\mathrm{sol2}}{≔}{\mathbf{proc}}\left({\mathrm{x_rkf45}}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{...}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (2.7.9)
 > $\mathrm{sol2}\left(\mathrm{parameters}=\left[-0.001,30\right]\right);$
 $\left[{\mathrm{h0}}{=}{-0.001}{,}{\mathrm{v0}}{=}{30.}\right]$ (2.7.10)

To find the roots, we'll execute the solution at a time beyond the zero-crossings.

 > $\mathrm{sol2}\left(0.4\right);$
 $\left[{t}{=}{0.0000334337167628194}{,}{h}{}\left({t}\right){=}{7.11507675693612}{}{{10}}^{{-20}}{,}\frac{{ⅆ}}{{ⅆ}{t}}\phantom{\rule[-0.0ex]{0.4em}{0.0ex}}{h}{}\left({t}\right){=}{29.8202119178629}\right]$ (2.7.11)

The first root was found using the event. Notice that the value of h is not exactly zero. This is because the event is triggered when the solver detects that the event occurred during an integration step, and not necessarily at the exact moment h=0.

Before we can continue to the next root, we have to clear the event using eventclear.

 >