How do I use anonymous functions to pass information between derivate functions for ODE23 ODE45 ODE15s and outputFcn?
1 view (last 30 days)
Show older comments
I've mastered simple anonymous functions, but I think I'm stretching it too far here. I am trying to record intermediate variables into a data structure SD and save them on every successful step of an ODE routine.
I could, and have in the past, used global variables, but this syntax should be more elegant. Should it work?
function testODE
SD=SteppingData;
SD.iy=0;
SD.Flow=0;
SD.FlowArray=zeros(11,1);
options=odeset('OutputFcn', @(t,y,flag) Stepoutput(t,y,flag,SD));
[t,y]=ode23(@(t,y) equns(t,y,SD),[0 10], 10, options);
SD.FlowArray
end
function dydt=equns(t,y,SD)
SD.Flow=y+2;
dydt=-0.01*y; % first order reaction giving exponential decay
end
function status=Stepoutput(t,y,flag,SD)
% record some intermediate results each step
if ~strcmp(flag,'done')
SD.iy=SD.iy+1;
SD.FlowArray(SD.iy)=SD.Flow;
status=0;
end
end
And SteppingData.m is:
classdef SteppingData
properties
% data to record on each step. Normally algebraic variables.
iy; % record number
FlowArray; % record of flow
Flow;
end
end
Answers (0)
See Also
Categories
Find more on Ordinary Differential Equations 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!