Main Content

selectSolver

Change ODE solver

Since R2024b

Description

s = selectSolver(F,Name=Value) selects a solver for the ode object F based on the parameters specified using one or more name-value arguments and returns the solver name. Change the ode object solver by assigning the output of the selectSolver function to the Solver property, for example, F.Solver = selectSolver(F,DetectStiffness="on").

example

Examples

collapse all

Create an ode object for the van der Pol equations with mu = 1000 using the built-in function file vdp1000.m. Specify the initial values of y' and y'' as 2 and 0, respectively.

F = ode(ODEFcn=@vdp1000,InitialValue=[2;0])
F = 
  ode with properties:

   Problem definition
               ODEFcn: @vdp1000
          InitialTime: 0
         InitialValue: [2x1 double]
         EquationType: standard

   Solver properties
    AbsoluteTolerance: 1.0000e-06
    RelativeTolerance: 1.0000e-03
               Solver: auto
       SelectedSolver: ode45

  Show all properties


The automatically selected solver is ode45. Solve the ODE system over the time interval [0 3000]. Measure the time the ode45 solver takes by using tic and toc.

tic
sol45 = solve(F,0,3000);
toc
Elapsed time is 11.110268 seconds.

Call the selectSolver function with the DetectStiffness name-value argument to choose a solver using a stiffness detection heuristic. Also specify the IntervalLength name-value argument as 3000.

F.Solver = selectSolver(F,DetectStiffness="on",IntervalLength=3000)
F = 
  ode with properties:

   Problem definition
               ODEFcn: @vdp1000
          InitialTime: 0
         InitialValue: [2x1 double]
             Jacobian: []
         EquationType: standard

   Solver properties
    AbsoluteTolerance: 1.0000e-06
    RelativeTolerance: 1.0000e-03
               Solver: ode15s

  Show all properties


The selected solver is now ode15s. Solving the ODE system over the time interval [0 3000] using the ode15s solver is faster and more efficient than using the ode45 solver.

tic
sol15s = solve(F,0,3000);
toc
Elapsed time is 0.243462 seconds.

Input Arguments

collapse all

ODE problem to solve, specified as an ode object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: s = selectSolver(F,DetectStiffness="on")

Stiffness detection heuristic, specified as "off" or "on". Specify DetectStiffness as "on" to incorporate a stiffness detection heuristic when selecting a solver.

If you do not know if the problem is stiff but want to account for potential stiffness, specify the DetectStiffness name-value argument as "on". Otherwise, set the Solver property of the ode object to:

  • "auto" if you do not need to account for stiffness or only need to perform basic checks. For example, if you specify a Jacobian, then "auto" will always return a stiff solver.

  • "stiff" if you know the problem is stiff.

  • "nonstiff" if you know the problem is not stiff.

Data Types: string | char

Solver time interval, specified as a positive integer. The interval length is |tfinaltinitial|. You can use IntervalLength to filter out irrelevant timescales when selecting a solver.

The function ignores this argument when DetectStiffness is "off".

Version History

Introduced in R2024b

See Also

|