MATLAB: Get variable out of ode 45

function variable from ode45MATLABode45outputfcn

Hi,
I have a function that goes into ode45. In this function multiple variables are calculated to finaly calculate the variable x which is the needed one. This works oke, but to review I would also like to see the other variables calculated, like q12, q23 and q34. I would not only like to see this variables. I also like to make some plots of those values against time, so disp(…) is not a good option. Please if you know how to extract those variables from the function. Let me know.
The function is used to compute water content in different soil layers. This is based on the flow q. Therefore q needs to be calculated, but I would like to know q. Here is the function given where the ode45 is used:
% Calculate content per layer
fh = @(time,x)fflow(time, x, tu, p, Ks, theta_r, theta_s, lambda, n, alpha); % Create function handle to put into ODE-fucntion
[time, xt] = ode45(fh, time, x0); % Compute water content 'x' per layer at time t
The different q values are computed in fflow, part of the code is given for the computation of q12 but for other q values it is the same:
if (TF(1) == 1) && (TF(2) == 0) || ((TF(1) == 1) && (TF(2) == 1)) % First layer saturated, second layer not:
q12 = maxFlowq(Ks(1),lambda(1), n(1), 30); % Maximum flow rate for soil type
elseif ((TF(1) == 0) && (TF(2) == 1)) % First layer not saturated, second layer saturated
q12 = 0;
else % First and second layer not saturated
q12 = -K(1)*((-abs(diff(1))/p(1))+1);
end
% Compute change in water content theta(x)
dx1dt = (qin-q12)/p(1); % Water content difference layer 1
dx2dt = (q12/p(1)-q23/p(2)); % Water content difference layer 2
dx3dt = (q23/p(2)-q34/p(3)); % Water content difference layer 3
dxdt = [dx1dt; dx2dt; dx3dt];
So if possible I would like to have the values for q for every t from this function.I hope you can help me:) Kind regards, Jits

Best Answer

One way is to use OutputFcn. The following entry could be of use.
For details on this option, please see "OutputFcn" section of odeset.