0
0
MATLABdata~10 mins

ODE solvers (ode45) in MATLAB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ODE solvers (ode45)
Define ODE function
Set time span and initial value
Call ode45 solver
Solver runs steps internally
Return time and solution arrays
Plot or analyze results
The flow shows how you define the ODE, set initial conditions, call ode45, which solves step-by-step, then returns results.
Execution Sample
MATLAB
f = @(t,y) -2*y;
tspan = [0 5];
y0 = 1;
[t,y] = ode45(f,tspan,y0);
plot(t,y)
This code solves dy/dt = -2y from t=0 to 5 with y(0)=1 and plots the solution.
Execution Table
Stept (time)y (solution)ActionNotes
101Start at initial conditionInitial time and value
20.10.8187Compute next y using ode45 stepApproximate y at t=0.1
30.20.6703Compute next yApproximate y at t=0.2
40.30.5488Compute next yApproximate y at t=0.3
50.40.4493Compute next yApproximate y at t=0.4
60.50.3679Compute next yApproximate y at t=0.5
.........Continue stepsSolver adapts step size internally
N54.54e-05Final stepReached end of time span
💡 Reached t=5, end of time span for solution
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5...Final
t00.10.20.30.40.5...5
y10.81870.67030.54880.44930.3679...4.54e-05
Key Moments - 3 Insights
Why does ode45 return arrays of t and y instead of just one value?
Because ode45 solves the ODE step-by-step over the time span, it returns arrays showing the solution at multiple time points (see execution_table rows 2 to N).
How does ode45 decide the size of each time step?
ode45 adapts the step size internally to keep the solution accurate, so time steps are not always equal (not shown explicitly but mentioned in execution_table notes).
What does the function handle @(t,y) -2*y represent?
It defines the ODE dy/dt = -2y, which ode45 uses to compute slopes at each step (see execution_sample code line 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the approximate value of y at step 3?
A0.5488
B0.8187
C0.6703
D0.4493
💡 Hint
Check the 'y (solution)' column at Step 3 in the execution_table.
At which step does the solver reach the end of the time span?
AStep N
BStep 5
CStep 6
DStep 1
💡 Hint
Look at the exit_note and the last row in execution_table.
If the initial value y0 was changed to 2, how would the variable_tracker change after Step 1?
Ay would be about 1.0
By would be about 1.6374 times 2
Cy would be about 1.6374
Dy would be about 1.6374 times 0.5
💡 Hint
Since y0 doubles, the values after Step 1 roughly double too (see variable_tracker y values).
Concept Snapshot
ode45 solves ODEs numerically in MATLAB.
Define the ODE as a function @(t,y).
Set time span and initial value.
Call [t,y] = ode45(odefun,tspan,y0).
 ode45 returns arrays of time and solution.
Use plot(t,y) to visualize results.
Full Transcript
This visual execution shows how MATLAB's ode45 solver works step-by-step. First, you define the ODE as a function of time and solution. Then you set the time range and initial value. Calling ode45 runs the solver, which calculates the solution at many time points by adapting step size for accuracy. The solver returns arrays of time and solution values. The example solves dy/dt = -2y from 0 to 5 starting at y=1. The execution table traces the solution values at each step, showing how y decreases exponentially. Key moments clarify why ode45 returns arrays, how step size adapts, and what the function handle means. The quiz tests understanding of solution values and solver behavior. The snapshot summarizes the main steps to use ode45 in MATLAB.