For ode45
:
The general syntax is ode45('func', [tmin,tmax], [x0, y0])
,func.m
, tmin
and tmax
are the minimum and maximum values of time you want, and [x0, y0]
are the initial conditions at tmin
. Don't forget the quotes around func
. It's also possible to do this for systems of dimensions higher and 2.
While the command above will give you a plot, you can extract the numerical data via [t,vars] = ode45('func', [tmin,tmax], [x0, y0])
.[tn, [xn, yn]]
. Then you can do plot(t,x(:,1))
to just plot the x's versus time, for example.
The script func.m
should define a function that takes t
and vars
as input, where vars
is a list containing the current values of your variables, and returns another list containing the current values of the derivatives of your variables.
The example I did in class is available here. Try downloading this and running ode45('predatorprey', [0, 60], [250, 30])
.