Section 1.14 MATLAB's IVP solvers
The basic syntax for Octave/MATLAB's own solvers is identical to SciPy's one. Once the right hand side of the ODE has been defined as a function, namedode
in the code below, the solver can be called, for instance, as ode45(@ode,[t0 tf],x0)
. The main difference with respect to SciPy's version is that here each method has its own function name, so that the method is not decided passing a string to a general solver function but rather using the name of the solver. The list of all solvers can be found at the matworks page Choose an ODE Solver, their internals is discussed in the MATLAB ODE suite. Here we just mention ode45
, and ode89
for nonstiff problems and ode15s
for still ones. The numbers refer to the orders of the methods. Below we use ode45
(which uses RK4 and RK5) to solve our usual IVP with MATLAB: One could be much more minimalistic:
Remark. In Octave, it is needed to move the function
declaration to the top of the program or the code will not run. Moreover, it is advisable to lower the default values of the absolute and relative tolerance or the quality of the numerical solution will be low.
Now we solve our 2-dimensional physical pendulum problem
\begin{equation*}
\begin{cases}
\dot x(t) = y(t),\cr
\dot y(t) = -\sin(x(t))\cr
\end{cases}
\end{equation*}
with initial conditions \(x(0)=1, y(0)=0\) and \(t_f=16.5\text{.}\) Here is the MATLAB code:
And here is the Octave one: