Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

ode45 in MATLAB: Syntax, Example, Solver Choice

Use ode45 in MATLAB: call syntax, first-order and second-order examples, default tolerances, ode15s for stiff problems, and the SciPy solve_ivp equivalent.

Written and maintained by CASRAI Editorial Board

Last updated

ode45 in MATLAB solves nonstiff ordinary differential equations (ODEs) numerically. You pass it a function handle for the derivatives, a time span and initial conditions, and it returns a column of times t and a matrix of solutions y. MathWorks calls it “a versatile ODE solver and is the first solver you should try for most problems.” This page covers the call syntax, a worked first-order example, second-order and multi-equation systems, how to read the output, and what to do when ode45 is slow, with a mapping to SciPy’s solve_ivp for readers who need a free alternative. Facts below are from the MathWorks documentation pages listed under Sources, read 2026-11-07.

What ode45 is used for in research

ode45 integrates initial value problems of the form y’ = f(t, y). Per the MathWorks documentation it is an explicit Runge-Kutta (4,5) solver based on the Dormand-Prince pair and a single-step solver, intended for nonstiff problems at medium accuracy. Typical research uses are kinetics models, population or epidemic models, mechanical and electrical systems, and any model you can write as one or more first-order equations. Related MATLAB spellings people search for (matlab ode45, ode45 in matlab) all refer to this same function. Readers working with symbolic equations first may also want the CASRAI guide to MATLAB syms.

Install or access (official source only)

ode45 is documented in the core MATLAB reference at mathworks.com/help/matlab/ref/ode45.html, and MathWorks is the only official source for MATLAB itself. Get MATLAB through mathworks.com or through your institution’s MathWorks licence; this page does not state pricing. Once MATLAB starts, type doc ode45 at the command line to open the documentation for your installed release.

Step-by-step: solve an ODE with ode45

The following steps use the first-order example from the MathWorks documentation, y’ = 2t with y(0) = 0.

  1. Write the right-hand side as a function handle. The function takes t and y in that order and returns a column vector of derivatives: odefun = @(t,y) 2*t;. It must accept both inputs even if one is unused.
  2. Set the time span. tspan = [0 5]; gives the start and end time. With a two-element vector the solver chooses its own steps.
  3. Set the initial condition. y0 = 0; must have the same length as the vector your function returns.
  4. Call the solver. [t,y] = ode45(odefun,tspan,y0);
  5. Plot the result. plot(t,y,'-o')

In one block (from the documentation example): tspan = [0 5]; y0 = 0; [t,y] = ode45(@(t,y) 2*t, tspan, y0); plot(t,y,'-o')

Second-order equations and systems of equations

ode45 accepts only first-order equations, so a second-order ODE has to be rewritten as a first-order system. The documentation’s van der Pol example does this: with y1′ = y2 and y2′ = μ(1 − y1²)y2 − y1, the state is the vector [y1; y2], μ = 1, tspan = [0 20] and y0 = [2; 0]. The function returns a column vector with one derivative per state, and the same pattern extends to any number of coupled equations. Each column of the output y is then one state variable. The MathWorks page notes that if such a problem is stiff or needs high accuracy, another solver may suit it better.

Syntax variants

Call What it does (MathWorks ode45 reference)
[t,y] = ode45(odefun,tspan,y0) Integrates over tspan with default settings.
[t,y] = ode45(odefun,tspan,y0,options) Same, with options created by odeset.
[t,y,te,ye,ie] = ode45(odefun,tspan,y0,options) Also returns event times, solutions and indices when an events function is set.
sol = ode45(___) Returns a structure you can evaluate later with deval.

Output and how to read it

t is a column vector of time points. Each row of y is the solution at the corresponding time in t, so y(:,1) is the first state over time. If tspan has more than two elements, the solution is returned at exactly those times instead of at solver-chosen steps. For reproducible research, keep the odefun, tspan, y0 and any odeset options together in one script so the run can be repeated.

Tolerances and options with odeset

Settings are created with odeset and passed as the fourth argument. Documented defaults: RelTol is 1e-3 and AbsTol is 1e-6; MaxStep defaults to 0.1*abs(t0−tf). Example: options = odeset(RelTol=1e-8,AbsTol=1e-10); (name=value syntax; before R2021a use quoted names separated by commas). An events function has the form [value,isterminal,direction] = myEventsFcn(t,y), and OutputFcn accepts a function handle.

Troubleshooting: ode45 vs ode15s and common errors

Symptom What the documentation points to
Dimension or shape errors Check that odefun takes (t,y) in that order, returns a column vector, and that y0 has the same length as that output.
Run is very slow or fails on a problem with widely different time scales The problem may be stiff. MathWorks advises trying ode15s when ode45 fails or is slow and the problem seems stiff.
Result not accurate enough Tighten RelTol and AbsTol with odeset, or consider the higher-accuracy solvers for smooth problems (ode78, ode89).

Which solver instead?

Solver Problem type and accuracy (MathWorks “Choose an ODE Solver”)
ode45 Nonstiff, medium accuracy; first solver to try.
ode23 Nonstiff, low accuracy; crude tolerances or moderate stiffness.
ode113 Nonstiff, low to high accuracy; stringent tolerances or costly function.
ode78, ode89 Nonstiff, high accuracy on smooth problems.
ode15s Stiff, low to medium accuracy; also differential-algebraic equations.
ode23s Stiff with a constant mass matrix.
ode23t, ode23tb Further stiff solvers listed on the same page.
ode15i Fully implicit problems.

The same page explains that stiffness arises from different scales or time scales in a problem, and that supplying a Jacobian or sparsity pattern can help stiff solvers.

Free alternatives: MATLAB ode45 to SciPy solve_ivp

SciPy’s scipy.integrate.solve_ivp is a free Python equivalent whose default method RK45 is also an order 5(4) Runge-Kutta. If you work in Python, see the CASRAI guides to installing Anaconda Python and Python for exploratory data analysis.

Item MATLAB ode45 SciPy solve_ivp
Call ode45(odefun,tspan,y0) solve_ivp(fun,t_span,y0,method='RK45')
Default relative / absolute tolerance 1e-3 / 1e-6 1e-3 / 1e-6 (rtol, atol)
Output orientation t is a column; each row of y is one time t has shape (n_points,); y has shape (n, n_points), so each column is one time
Stiff problems ode15s, ode23s and others Radau, BDF, LSODA

The transposed output is the usual porting mistake: code that indexes y(:,1) in MATLAB needs y[0] in SciPy.

FAQ

What is ode45 in MATLAB?

It is MATLAB’s general-purpose solver for nonstiff ODE initial value problems, using an explicit Runge-Kutta (4,5) Dormand-Prince method, and MathWorks recommends trying it first for most problems.

What is the ode45 MATLAB formula or method?

It is a single-step explicit Runge-Kutta method using the Dormand-Prince pair of orders 4 and 5; you supply y’ = f(t, y) as odefun rather than a closed-form formula.

How do I use ode45 for a second-order equation or a system of equations?

Rewrite the equation as a first-order system, return one derivative per state as a column vector, and give y0 one entry per state, as in the van der Pol example above.

When should I use ode15s instead of ode45?

When ode45 fails or is very slow and the problem appears stiff. MathWorks lists ode15s for stiff problems and differential-algebraic equations.

What does the odefun input need to look like?

A function handle dydt = odefun(t,y) that returns a column vector and accepts both t and y, even if one is not used.

Sources

See also: moving average in MATLAB and normal distribution in MATLAB.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about ode45 in MATLAB: Syntax, Example, Solver Choice

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →