Basic
[tSol, ySol] = ode45(@odefun, tRange, y0)
- @odefun: A function handle to the derivative function, need to specify
- tRange: the time range to calculate the differential function
- y0: the initial value
- tSol: indepent variable, two element vector containing the initial and final values
- ySol: dependent variable, the initial value of the dependent variable
ODE general solution structure:
- Call ode45 with a single output variable:
sol = ode45(@odefun,tRange,y0)
- use the deval function to evaluate the solution at any point in the interval tRange by passing the solution structure and the point to deval:
y2 = deval(sol,t)
t is the time point, can be a single time or an array of time - use
fzero
to find when sol evaluates to a value(e.g. 336):tTermVel = fzero(@(t) deval(sol,t) - 336, 300)
1 | tRange = [0 120]; |
Solve system of ODE
Writing Vector-Valued ODE functions
Creating a vector-valued ODE function can be broken down into three parts:
- extracting the individual elements from the vector of dependent variables
- implementing the derivative functions for the dependent variables
- returning the output variable as a column vector of the derivatives
Initial condition is a column vector