# how to extract variables from ode45

4 views (last 30 days)
chirag patel on 5 Feb 2020
Answered: Bjorn Gustavsson on 5 Feb 2020
how to extract data from ode45 which is not ouput of ode45?
usingoutputfn i have extracted but it is extracted for 11 points instead of 41?
same way how to extract data from fsolve which is not output of fsolve?

Bjorn Gustavsson on 5 Feb 2020
My pedestrian-layman approach to this type of problem is to integrate the ODE, typically with t = t0:dt:tend, then when I have y(t) I call the ODE again (in a loop or with the entire t and y(t)) and extract the extra outputs. Typically somthing like this:
y0 = [0 1 2 3];
t_span = (0:0.1:123)';
[t,y] = ode45(@(t,y) ode_def(t,y,[1],0),t_span,y0)
for i_t = numel(t):-1:1
[~,Pars_of_interest(i1)] = ode_def(t(i_t),y(i_t,:),[1],1);
end
Where the ode_def could be written to return the extra output when the input argument extraoutputs is 1
function varargout = ode_def(t,y,pars,extraoutputs)
dydt = [y(2);-abs(y(1))^3*sign(y(1));-tan(y(4));y(1)+y(3)-y(2)^2];
varargout{1} = dydt;
if extraoutputs == 1
Q = pars(1)+sum(y);
varargout{2} = Q;
end
end
QD-solution perhaps, but a solution that is robust to misinterpretations and lack of understanding of how to use the outputfunction cleanly.
HTH