ODE solvers (ode45) in MATLAB - Time & Space Complexity
When solving ordinary differential equations (ODEs) with ode45, it is important to understand how the time to solve grows as the problem size or time span increases.
We want to know how the number of calculations changes when we ask ode45 to solve over longer times or with more complex equations.
Analyze the time complexity of the following ode45 usage.
function dydt = simpleODE(t, y)
dydt = -2 * y + sin(t);
end
tspan = [0 10];
y0 = 1;
[t, y] = ode45(@simpleODE, tspan, y0);
This code solves a simple ODE from time 0 to 10 starting at y=1 using ode45.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: ode45 repeatedly evaluates the ODE function at many time points.
- How many times: The number of function evaluations depends on the solver's step size and error control, which adapts to the problem.
As the time span or complexity of the ODE increases, ode45 takes more steps, so the number of operations grows roughly in proportion to the length of the time interval.
| Input Size (time span length) | Approx. Operations (function calls) |
|---|---|
| 10 | ~100 to 200 |
| 100 | ~1000 to 2000 |
| 1000 | ~10,000 to 20,000 |
Pattern observation: Doubling the time span roughly doubles the number of steps and function evaluations.
Time Complexity: O(n)
This means the time to solve grows roughly linearly with the length of the time interval or the number of steps needed.
[X] Wrong: "ode45 always takes the same number of steps regardless of the time span or equation complexity."
[OK] Correct: ode45 adapts its step size based on the problem and interval length, so more time or harder equations mean more steps and more work.
Understanding how numerical solvers like ode45 scale helps you explain performance in simulations and scientific computing tasks, showing you grasp practical algorithm behavior.
"What if we changed the solver from ode45 to ode15s for stiff problems? How would the time complexity change?"
Practice
ode45 primarily do?Solution
Step 1: Understand the purpose of ode45
Theode45function in MATLAB is designed to solve ordinary differential equations (ODEs) using a numerical method called Runge-Kutta.Step 2: Compare options with ode45's function
Options A, C, and D describe plotting, symbolic differentiation, and analytical integration, which are not whatode45does.Final Answer:
It solves ordinary differential equations numerically. -> Option AQuick Check:
ode45 solves ODEs numerically = B [OK]
- Confusing ode45 with plotting functions
- Thinking ode45 does symbolic math
- Assuming ode45 integrates analytically
ode45 for solving dy/dt = -2y with initial value y(0)=1 over time interval [0 5]?Solution
Step 1: Check function handle and argument order
The function handle for ode45 must be @(t,y) for time t and variable y. [t,y] = ode45(@(t,y) -2*y, [0 5], 1); uses @(t,y) correctly with the expression -2*y.Step 2: Verify time span and initial value format
Time span must be a vector [0 5], and initial value is a scalar 1. [t,y] = ode45(@(t,y) -2*y, [0 5], 1); matches this format exactly.Final Answer:
[t,y] = ode45(@(t,y) -2*y, [0 5], 1); -> Option BQuick Check:
Correct syntax: @(t,y), time vector, initial value = A [OK]
- Swapping t and y in function handle
- Using colon operator instead of vector for time span
- Passing time points as separate arguments
f = @(t,y) t - y; [t,y] = ode45(f, [0 2], 1); disp(y(end));
What is the output approximately?
Solution
Step 1: Understand the ODE and initial condition
The ODE is dy/dt = t - y with y(0) = 1, solved over [0 2]. This is a linear ODE where y tends to follow t but is pulled down by -y.Step 2: Estimate solution behavior at t=2
Numerical solution with ode45 typically gives y around 1.3 at t=2 for this ODE and initial value.Final Answer:
1.3 -> Option AQuick Check:
ode45 output y(end) ≈ 1.3 for dy/dt = t - y [OK]
- Assuming output equals initial value
- Confusing function input order
- Ignoring numerical approximation
f = @(y,t) y^2 - t; [t,y] = ode45(f, [0 1], 0); plot(t,y);
Solution
Step 1: Check function handle argument order
ODE functions for ode45 must have arguments in order (t,y), but here it is @(y,t), which is incorrect.Step 2: Verify other parts of the code
Initial value zero is allowed, time span as [0 1] is valid, and plot labels are optional, so these are not errors.Final Answer:
Function handle arguments are in wrong order. -> Option CQuick Check:
Function handle must be @(t,y), not @(y,t) [OK]
- Swapping t and y in function handle
- Thinking initial value can't be zero
- Believing time span must be column vector
dx/dt = y
dy/dt = -x
with initial conditions
x(0)=0, y(0)=1 over [0 10]. Which MATLAB code correctly uses ode45 to solve this system?Solution
Step 1: Define function handle for system of ODEs
The system has two variables x and y, combined as vector u where u(1)=x and u(2)=y. The function must be @(t,u) returning [y; -x], which is [u(2); -u(1)].Step 2: Check initial conditions and call to ode45
Initial conditions are [0 1] for x=0 and y=1. Time span is [0 10]. f = @(t,u) [u(2); -u(1)]; [t,u] = ode45(f, [0 10], [0 1]); matches this exactly.Final Answer:
f = @(t,u) [u(2); -u(1)]; [t,u] = ode45(f, [0 10], [0 1]); -> Option DQuick Check:
System ODEs use @(t,u) with vector u = D [OK]
- Using separate arguments for variables in function handle
- Swapping order of arguments in function handle
- Returning wrong derivative vector
Ready to prove it?
Pass the test and this pattern moves unknown
