2

I have a function with the handle @func with initial condition y0 that I want to test over t = [0, tspan]. What, exactly, do I need to do to increase the number of time steps ode45 uses, without changing tspan?

I saw the MATLAB documentation of ode45 and saw that I need to somehow change the options input to ode45. However, I do not really understand how to do this because no example code was provided.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
farid99
  • 712
  • 2
  • 7
  • 25

1 Answers1

4

You need to look at odeset which creates an options structure to be used in ODE solvers, like ode45. You're looking at changing the MaxStep parameter.

The documentation for this (MATLAB R2015a) says:

MaxStep - Upper bound on step size [ positive scalar ]

MaxStep defaults to one-tenth of the tspan interval in all solvers.

Therefore, you can make it something smaller than 0.1*tspan... something like 1e-6 or something like that. This depends on what tspan is, so you'll need to make it smaller than 0.1*tspan if you want to increase the total number of time steps / output points.

As such, create an odeset structure and override the MaxStep parameter. The rest of the values will remain as default:

options = odeset('MaxStep', 1e-6);

Now that you're done, call ode45 on your problem:

[tout,yout] = ode45(@func, tspan, y0, options);

Simply play around with the MaxStep until you get the desired granularity.

Minor Note

Though a bit buried, the documentation does tell you on how to change the options. This is the section that talks about how to call ode45 with options. Please take note of what is highlighted in bold. This is the documentation for MATLAB R2015a:

[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0,OPTIONS) solves as above with default integration properties replaced by values in OPTIONS, an argument created with the ODESET function. See ODESET for details. Commonly used options are scalar relative error tolerance 'RelTol' (1e-3 by default) and vector of absolute error tolerances 'AbsTol' (all components 1e-6 by default). If certain components of the solution must be non-negative, use ODESET to set the 'NonNegative' property to the indices of these components.

rayryeng
  • 102,964
  • 22
  • 184
  • 193