query regarding the output function in ode 45 ?

1 Ansicht (letzte 30 Tage)
SOUVIK DARIPA
SOUVIK DARIPA am 30 Aug. 2024
Bearbeitet: SOUVIK DARIPA am 16 Sep. 2024
I want my output function to save results every fifth time the solver is called. I've set tspan as [0, tt/99, tt/98, tt/97, tt/96, ..., tt], where tt is 50 seconds. The output I'm getting when saving to a binary file is either 10 or 6 because the output function only saves data every fifth solver call, which is fine with me. I've specified this tspan to avoid storing y (output) at all the intermediate points, thereby preventing memory issues (such as swap) during execution. Is there a way to instruct the solver not to save any output within y?
code
time_span_interval=linspace(0, 1e8,2e6);
dt=time_span_interval(2)-time_span_interval(1);
tspan=linspace(0,dt-1,200);
% Define the output function with Nx as an additional argument
% myOutput function is a sub routoine that is called at each step of the
% solver and saves output conditionally
outputFcn = @(t, y, flag)mylastout5(t, y, flag, Nx);
options = odeset('Refine',1,'RelTol', 1e-3, 'AbsTol', 1e-6,'MaxStep',1e6,'OutputFcn',outputFcn);
for i =1:length(time_span_interval)-1
tt=(time_span_interval(i+1)-time_span_interval(i));
time=[tspan tt];
[t,y] = ode45(@(t,y)patch_weakening_4(t,y,Nx,par,law),...
time,initial_conditions',options);
initial_conditions=y(end,:);
disp(' vmax and t output taken')
format shortg
c = clock
end
OUTPUT FUNCTION
function status = mylastout5(t, y, flag, Nx)
persistent stepCounter
if isempty(stepCounter)
stepCounter = 0; % Initialize the step counter
end
if isempty(flag)
% Intermediate call: increment the step counter
stepCounter = stepCounter + 1;
% Save solution after every 2 steps
if mod(stepCounter,10) == 0
% Save time to a binary file
fid = fopen('time_ode_AGE_231_3.bin', 'ab'); % Open file for writing in binary mode
fwrite(fid, t, 'double'); % Write time as double precision
fclose(fid); % Close the file
% Save solution to a binary file
fid = fopen('ode_AGE_231_3.bin', 'ab'); % Open file for writing in binary mode
fwrite(fid, y(1:3*Nx,:), 'double'); % Write solution vector as double precision
fclose(fid); % Close the file
end
elseif strcmp(flag, 'done')
% Final call: no action needed for final call
end
status = 0; % Continue the solver
end

Antworten (1)

Shishir Reddy
Shishir Reddy am 3 Sep. 2024
Hi Souvik
As per my understanding you would like to prevent the solver from computing and returning the full solution in ‘y’. To prevent the solver from computing and returning the solution in ‘y’, the approach can be modified to handle the solution incrementally within the output function itself. Which means, the output of the ode45 solver should not be stored in the variable ‘y’.
The result of ode45 is assigned to ‘[t, y]’ as shown in the following line of code.
[t, y] = ode45(@(t, y)harmonicOscillator(t, y, par, law), tspan, initial_conditions, options);
However, if the result of ode45 solver is not assigned to ‘[t, y]’, the solver will still compute the solution but will not store it in the workspace under a variable name ‘y’, which can be done as follows.
ode45(@(t, y)harmonicOscillator(t, y, par, law), tspan, initial_conditions, options);
For more information regarding the ode45 solver, kindly refer to the following documentation. https://www.mathworks.com/help/matlab/ref/ode45.html
I hope this helps.
  1 Kommentar
SOUVIK DARIPA
SOUVIK DARIPA am 16 Sep. 2024
Bearbeitet: SOUVIK DARIPA am 16 Sep. 2024
yes but i also need the last output y as the new initial condition ... i was expecting some in place storing of the variable y which will save memory .

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by