Question

I am using the following commands to solve the ODE function "DynamicFunc":

x0_sim = zeros(12,1);
time_sim = [0 100];
options=odeset('AbsTol',1e-9,'RelTol',1e-6);
[tp,xp]=ode45(@DynamicFunc,time_sim,x0_sim,options);

The DynamicFunc can be any function and the code works very well. the time of simulation is 0 - 100 seconds. the finals xp will have a size of 3017x12 (i.e. there are 12 states and 3017 trial points). If I want to run this code inside a FOR loop with duration of 1 second, something like this:

x0_sim = zeros(12,1);
time_sim = [0 1];
for tt = 1:100
    options=odeset('AbsTol',1e-9,'RelTol',1e-6);
    [tp,xp]=ode45(@DynamicFunc,time_sim,x0_sim,options);
    x0_sim = xp(:,end);
end

The number of final iterations would be so different. How can I make them the same size (for both methods to have the final output with the same vector size)? I guess the problem can be related to setting an specific option, but I couldn't find any.

Was it helpful?

Solution

Your problems are due to the step-size control in ODE45 that dynamically increases and decreases the size of the time steps. In your second setup, you enforce the time grid points at every second while in the first setup the algorithm choses its own grid points.

To make both match, you should consider the option of providing a properly chosen timegrid vector tspan to get the function values at a predefined grid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top