2D plots of ODE solutions

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


plot::Ode2d(f, [t0, t1,...], Y0, <[G1, <Style = style1>, <Color = c1>], [G2, <Style = style2>, <Color = c2>], …>, <method>, <RelativeError = rtol>, <AbsoluteError = atol>, <Stepsize = h>, <a = amin .. amax>, options)
plot::Ode2d(f, [Automatic, tstart, tend, tstep], Y0, <[G1, <Style = style1>, <Color = c1>], [G2, <Style = style2>, <Color = c2>],...>, <method>, <RelativeError = rtol>, <AbsoluteError = atol>, <Stepsize = h>, <a = amin .. amax>, options)
plot::Ode2d([t0, t1, …], f, Y0, <[G1, <Style = style1>, <Color = c1>], [G2, <Style = style2>, <Color = c2>],...>, <method>, <RelativeError = rtol>, <AbsoluteError = atol>, <Stepsize = h>, <a = amin .. amax>, options)
plot::Ode2d([Automatic, tstart, tend, tstep], f, Y0, <[G1, <Style = style1>, <Color = c1>], [G2, <Style = style2>, <Color = c2>],...>, <method>, <RelativeError = rtol>, <AbsoluteError = atol>, <Stepsize = h>, <a = amin .. amax>, options)


plot::Ode2d(f, [t0, t1,...], Y0) renders two-dimensional projections of the solutions of the initial value problem given by f, t0 and Y0.

plot::Ode2d(f, [t0, t1,...], Y0, [G]) computes a mesh of numerical sample points Y(t0), Y(t1), … representing the solution Y(t) of the first order differential equation (dynamical system)


The procedure

maps these solution points (ti, Y(ti)) in ×n to a mesh of 2D plot points [xi, yi]. These points can be connected by straight lines or interpolating splines.

Internally, a sequence of numerical sample points

Y_1 := numeric::odesolve(f, t_0..t_1, Y_0, Options),

Y_2 := numeric::odesolve(f, t_1..t_2, Y_1, Options), and so on

is computed, where Options is some combination of method, RelativeError = rtol, AbsoluteError = atol, and Stepsize = h. See numeric::odesolve for details on the vector field procedure f, the initial condition Y0, and the options.

The utility function numeric::ode2vectorfield can be used to produce the input parameters f, t0, Y0 from a set of differential expressions representing the ODE. See Example 1.

Each of the “generators of plot data” G1, G2 etc. creates a graphical solution curve from the numerical sample points Y0, Y1, and so on. Each generator G is internally called in the form G(t0, Y0), G(t1, Y1),... to produce a sequence of plot points in 2D.

The solver numeric::odesolve returns the solution points Y0, Y1, and so on, as lists or one-dimensional arrays (the actual type is determined by the initial value Y0). Consequently, each generator G must accept two arguments (t, Y): t is a real parameter, Y is a “vector” (either a list or a 1-dimensional array).

Each generator must return a list with 2 elements representing the (x, y) coordinates of the graphical point associated with a solution point (t, Y) of the ODE. All generators must produce graphical data of the same dimension, that is, 2D data as lists with 2 elements for plot::Ode2d. Here are some examples:

  • G := (t, Y) -> [t, Y_1] creates a 2D plot of the first component of the solution vector along the y-axis, plotted against the time variable t along the x-axis

  • G := (t, Y) -> [Y_1, Y_2] creates a 2D phase plot, plotting the first component of the solution along the x-axis and the second component along the y-axis. The result is a solution curve in phase space (parametrized by the time t).

If no generators are given, plot::Ode2d by default plots all components of the solution as functions of time, using [Splines, Points] as the style.

Note that arbitrary values associated with the solution curve may be displayed graphically by an appropriate generator G. See Example 2.

Several generators G1, G2, and so on can be specified to generate several curves associated with the same numerical mesh Y0, Y1, …. See Example 1, Example 2, and Example 3.

The graphical data produced by each of the generators G1, G2,... consists of a sequence of mesh points in 2D.

  • With Style = Points, the graphical data are displayed as a discrete set of points.

  • With Style = Lines, the graphical data points are displayed as a curve consisting of straight line segments between the sample points. The points themselves are not displayed.

  • With Style = Splines, the graphical data points are displayed as a smooth spline curve connecting the sample points. The points themselves are not displayed.

  • With Style = [Splines, Points] and Style = [Lines, Points], the effects of the styles used are combined, that is, both the evaluation points and the straight lines or splines, respectively, are displayed.

The plot attributes accepted by plot::Ode2d include Submesh = n, where n is some positive integer. This attribute only has an effect on the curves which are returned for the graphical generators with Style = Splines and Style = [Splines, Points], respectively. It serves for smoothening the graphical spline curve using a sufficiently high number of plot points.

n is the number of plot points between two consecutive numerical points corresponding to the time mesh. The default value is n = 4, that is, the splines are plotted as five straight line segments connecting the numerical sample points.


AttributePurposeDefault Value
AbsoluteErrormaximal absolute discretization error 
AffectViewingBoxinfluence of objects on the ViewingBox of a sceneTRUE
AntiAliasedantialiased lines and points?TRUE
Colorslist of colors to use[RGB::Blue, RGB::Red, RGB::Green, RGB::MuPADGold, RGB::Orange, RGB::Cyan, RGB::Magenta, RGB::LimeGreen, RGB::CadmiumYellowLight, RGB::AlizarinCrimson, RGB::Aqua, RGB::Lavender, RGB::SeaGreen, RGB::AureolineYellow, RGB::Banana, RGB::Beige, RGB::YellowGreen, RGB::Wheat, RGB::IndianRed, RGB::Black]
Framesthe number of frames in an animation50
Functionfunction expression or procedure 
InitialConditionsinitial conditions of the ODE 
Legendmakes a legend entry 
LegendTextshort explanatory text for legend 
LegendEntryadd this object to the legend?FALSE
LineWidthwidth of lines0.35
LineStylesolid, dashed or dotted lines?Solid
LinesVisiblevisibility of linesTRUE
Namethe name of a plot object (for browser and legend) 
ODEMethodthe numerical scheme used for solving the ODEDOPRI78
ParameterEndend value of the animation parameter 
ParameterNamename of the animation parameter 
ParameterBegininitial value of the animation parameter 
ParameterRangerange of the animation parameter 
PointSizethe size of points1.5
PointStylethe presentation style of pointsFilledCircles
PointsVisiblevisibility of mesh pointsTRUE
Projectorsproject an ODE solution to graphical points 
RelativeErrormaximal relative discretization error 
Stepsizeset a constant step size 
Submeshdensity of submesh (additional sample points)4
TimeEndend time of the animation10.0
TimeMeshthe numerical time mesh 
TimeBeginstart time of the animation0.0
TimeRangethe real time span of an animation0.0 .. 10.0
Titleobject title 
TitleFontfont of object titles[" sans-serif ", 11]
TitlePositionposition of object titles 
TitleAlignmenthorizontal alignment of titles w.r.t. their coordinatesCenter
TitlePositionXposition of object titles, x component 
TitlePositionYposition of object titles, y component 
USubmeshdensity of additional sample points for parameter “u”4
VisibleAfterobject visible after this time value 
VisibleBeforeobject visible until this time value 
VisibleFromToobject visible during this time range 
VisibleAfterEndobject visible after its animation time ended?TRUE
VisibleBeforeBeginobject visible before its animation time starts?TRUE


Example 1

The following procedure f together with the initial value Y0 represent the initial value problem , Y(0) = 2. In MuPAD®, the one-dimensional vector Y is represented by a list with one element. The body of the function f below addresses the first (and only) entry of this list as Y1 and returns the one-dimensional vector tY - Y2 as a list with one element. Also the initial condition Y0 is a one-dimensional vector represented by a list. For details on the format of f, please see numeric::odesolve:

f := (t, Y) -> [t*Y[1] - Y[1]^2]: 
Y0 := [2]:

Alternatively, the utility function numeric::ode2vectorfield can be used to generate the input parameters in a more intuitive way:

[f, t0, Y0] := [numeric::ode2vectorfield(
      {y'(t) = t*y(t) - y(t)^2, y(0) = 2}, [y(t)])]

The numerical solution is to consist of sample points over the time mesh ti = i, i = 0, 1, …, 10. We use the default generator of plot::Ode2d. This generates the sample points together with a smooth spline curve connecting these points:

p := plot::Ode2d(f, [$ 0..10], Y0,
                 PointSize = 2*unit::mm,
                 PointStyle = Stars):

Finally, the ode solution is rendered by a call to plot:

plot(p, TicksDistance = 2.0, GridVisible = TRUE, 
     SubgridVisible = TRUE):

Example 2

Consider the nonlinear oscillator , . As a dynamical system for , solve the following initial value problem , Y(0) = Y0. For details on the format of f, see numeric::odesolve:

f := (t, Y) -> [Y[2], - Y[1]^7]: 
Y0 := [1, 0]:

The following generator produces a plot of the solution Y(t) against the time parameter t:

G1 := (t, Y) -> [t, Y[1]]:

For demonstration purposes, plot the function . The generator G2 produces the values along the solution and plots these values against t:

G2 := (t, Y) -> [t, Y[1]^2/2 + Y[2]^2/2]:

The energy function, or “Hamiltonian”, must be conserved along the solution curve because the total energy of the system is constant. Define a corresponding generator G3 to plot as a function of t:

G3 := (t, Y) -> [t, Y[1]^8/8 + Y[2]^2/2]:

The solution curve is combined with the graph of the function and the Hamiltonian:

p := plot::Ode2d(f, [i/2 $ i = 0..40], Y0,
                 [G1, Style = Lines, Color = RGB::Red],
                 [G1, Style = Points, Color = RGB::Black],
                 [G2, Style = Lines, Color = RGB::Blue],
                 [G2, Style = Points, Color = RGB::Black],
                 [G3, Style = Lines, Color = RGB::Green],
                 [G3, Style = Points, Color = RGB::Black],
                 PointSize = 1.5*unit::mm,
                 LineWidth = 0.2*unit::mm

Note that by using each generator twice, you can set different colors for the lines and points. The renderer is called:


The Hamiltonian G3 is constant which verifies the accuracy of the integrator.

To visualize the dependency of the trajectory on the initial conditions, animate plot::Ode2d over different values of :

plot(plot::Ode2d(f, [i/6 $ i = 0..120], [1, a], a = -1/2..1/2,
                 [G1, Style = Lines, Color = RGB::Red],
                 [G2, Style = Lines, Color = RGB::Blue],
                 [G3, Style = Lines, Color = RGB::Green],
                 LineWidth = 0.2*unit::mm, Frames=25))

Example 3

Consider the initial value problem , y(0) = 0:

f := (t, y) -> t*sin(t + y^2): Y0:= [0]:

Use numeric::ode2vectorfield to generate the following vector field which is tangent to the solution curves:

p1 := plot::VectorField2d([1, f(t, y)], t = 0..4, y = -1.2..1.2,
                          Mesh = [21, 25], Color = RGB::Black):

The following object represents the plot of the solution as a function of t:

p2 := plot::Ode2d(
   (t,Y) -> [f(t, Y[1])], [i/3 $ i=0..12], Y0,
   [(t, Y) -> [t, Y[1]], Style = Points, Color = RGB::Red],
   [(t, Y) -> [t, Y[1]], Style = Splines, Color = RGB::Blue]):

Define the point size explicitly:

p2::PointSize := 2*unit::mm:

Finally, combine the vector field and the ODE plot to a scene and call the renderer:

plot(p1, p2, XTicksDistance = 0.5, YTicksDistance = 0.2,
     Axes = Frame, AxesTitles = ["t", "y"], 
     GridVisible = TRUE):

Example 4

By default, numeric::odesolve (which is used by plot::Ode2d internally) uses adaptive step sizes and a method of order 8. Usually, there is no reason to change these settings, except for demonstrative purposes. In the following animation, use a straightforward explicit Euler method (of first order) and show how decreasing the step size improves the quality of the calculated solution.

The differential equation is , obviously fulfilled by the exponential function:

[f, t0, Y0] := [numeric::ode2vectorfield(
                   {y'(t)=y(t), y(0)=1}, [y(t)])]:

To judge the quality of the numerical solution, plot the symbolic solution alongside the approximation:

plot(plot::Function2d(exp(x), x=0..3,
                      Color = RGB::Black, LineStyle = Dashed),
     plot::Ode2d(f, [Automatic, 0, 3, 1/n], Y0, n = 1..50,
                 EULER1, Stepsize = 1/n,
                 [(t, Y) -> [t, Y[1]], Style=[Lines, Points]]))



The vector field of the ODE: a procedure. See numeric::odesolve for details.

f is equivalent to the attribute Function.

t0, t1, …

The time mesh: real numerical values. If data are displayed with Style = Splines, these values must be in ascending order.

t0, t1, … is equivalent to the attribute TimeMesh.

tstart, tend, tstep

The time mesh: real numerical values. tend must be larger than tstart and tstep must be positive and should be smaller than tend - tstart.

tstart, tend, tstep are equivalent to the attribute TimeMesh.


The initial condition of the ODE: a list or a 1-dimensional array. See numeric::odesolve.

Y0 is equivalent to the attribute InitialConditions.

G1, G2, …

“generators of plot data”: procedures mapping a solution point (t, Y(t)) to a list [x, y] or [x, y, z] representing a plot point in 2D or 3D, respectively.

G1, G2, … is equivalent to the attribute Projectors.


Use a specific numerical scheme (see numeric::odesolve)


Animation parameter, specified as a = amin..amax, where amin is the initial parameter value, and amax is the final parameter value.



Option, specified as Style = style

Sets the style in which the plot data are displayed. The following styles are available: Points, Lines, Splines, [Lines, Points], and [Splines, Points]. The default style is [Splines, Points].


Option, specified as Color = c

Sets the RGB color c in which the plot data are displayed. The default color of the ith generator is the ith entry of the attribute Colors.


Option, specified as RelErr = rtol

Sets a numerical discretization tolerance (see numeric::odesolve)


Option, specified as AbsErr = atol

Sets a numerical discretization tolerance (see numeric::odesolve)


Option, specified as Stepsize = h

Sets a constant stepsize (see numeric::odesolve)