How to autosave matlab ode sover results if power goes

1 view (last 30 days)
My simulation is taking so much time. If the power goes off during my simulation, I have to start the simulation again.
Do we have any alternative for autosave results at specific intervals of time during my running simulation.
Thanks and regards

Answers (1)

Star Strider
Star Strider on 12 Mar 2020
Edited: Star Strider on 12 Mar 2020
I would create a ‘tspan’ vector using linspace, then in a loop, integrate over a portion of the vector, save the results to a .mat file, re-start the integration over the next ‘tspan’ segment, using the last values of the integrated variables as the new initial conditions, and continue until the entire ‘tspan’ interval has been integrated. See: How to save data from Genetic Algorithm in case MATLAB crashes? for this solution applied to genetic algorithm intermediate results. For the ODE solvers, you would save all the interim results for each ‘tspan’ segment (both the independent and integrated dependent variables), rather than the fittest individual in each generation.
EDIT — (12 Mar 2020 at 21:52)
Example —
t = linspace(0, 25, 200); % Full Time Vector
odefcn = @(t,y) [y(2); y(1).*sin(y(2)).*exp(-0.01*t)]; % Differential Equations
y0 = [0 1]; % Initial Conditions
T = [];
Y = [];
save('ODE_Results.mat', 'T','Y'); % Initialise .mat File
for k = 1:10
tspan = t((1:20)+(k-1)*19); % Incremental Time Vector
[Tk,Yk] = ode45(odefcn, tspan, y0); % Integrate
y0 = Yk(end,:); % New ‘y0’
previous = load('ODE_Results.mat'); % Load Previous Variables
T = [previous.T; Tk]; % Concatenate
Y = [previous.Y; Yk]; % Concatenate
save('ODE_Results.mat', 'T','Y'); % Save Concatenated Variables
end
Results = load('ODE_Results.mat'); % Retrieve Results
Tv = Results.T;
Ym = Results.Y;
figure
plot(Tv, Ym) % Plot Results
grid
xlabel('T');
ylabel('Y')
legend('Y_1', 'Y_2')

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!