Saving multiple outputs from ODE45 with different dimensions?

7 Ansichten (letzte 30 Tage)
Adham Alkhaja
Adham Alkhaja am 21 Jun. 2020
Beantwortet: J. Alex Lee am 21 Jun. 2020
I have written an ODE45 function inside a for loop with an event where it stops at a zero-crossing. I have creating a variable that is three-dimensional and saves the output each iteration in the for-loop. The issue is that each iteration, the dimension of the output is different as each flags the stop condition at a different number of steps.
How can I save a changing variable size in one variable?
The code is,
counter_minus = 1;
for n=2:k
options=odeset('RelTol',1e-13,'AbsTol',1e-22,'Events',@(t,y) Xcrossing(t,y));
[t_out,xU_out]=ode113(@(t,y) DifferentialEquation(t,y),tspan,y0,options);
XU_OUTPUT(:,:,counter_minus) = xU_out;
counter_minus=counter_minus+1;
end

Antworten (1)

J. Alex Lee
J. Alex Lee am 21 Jun. 2020
I would use cells...also what's with the dual indexing? options can be defined outside since nothing is changing from iteration to iteration
% define outside the loop since nothing is changing
% AbsTol = 1e-22 < eps, is this realistic?
options=odeset('RelTol',1e-13,'AbsTol',1e-22,'Events',@(t,y) Xcrossing(t,y));
% pre-allocate
XU_OUTPUT = cell(k-1,1);
for counter = 1:(k-1)
% presumably something is changing with iterations...otherwise it's the same thing over and over again?
[t_out,xU_out]=ode113(@(t,y) DifferentialEquation(t,y),tspan,y0,options);
XU_OUTPUT{counter} = xU_out;
end

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by