How do I extract a function calculated and used inside my ode45 function?
3 views (last 30 days)
Show older comments
Hello,
I'm using ode45 to produce solutions to ODE's and It works. However, I have a function inside of the code that I want to do a graph in the main.m. The system is modelled with the following equations:
Well, the graph I want to do is and versus time. Do you have any ideas how to get them?
Thank you very much in advance
5 Comments
Jan
on 20 Feb 2019
Edited: Jan
on 20 Feb 2019
Hello Jan, once again thanks for answering :)
So the first equation the biomass balance and the second one the oxygen generation (I cannot post the whole code, because some parameters "p" are in the main.m)
dy(1) = u_Net*y(1)-Ru*(u/(Ag.^2))*y(1);
dy(2) = k_O.*mu_bar(I,y(1),p)*y(1) - (kL_O2*(y(2) - O2_star) - ...
RO2.*(u/(Ag.^2))*y(1));
The curve of oxygen reaches a maximal value, and I would like to show that at first the generation term (the one of the right in equation 2) is stronger and consumption, and after reaching the maximum, the behaviour is on the other way around; consumption stronger than generation. And indeed when I do the graph dO2/dt versus time, I obtained the expected results, but I wanto show that is due to the reason I explained before.
Do you understand me? Maybe I'm not being clear...
Thanks again!
Jan
on 20 Feb 2019
Edited: Jan
on 20 Feb 2019
I do not understand the equation or the description. But understanding the meaning is not needed to solve the problem. You've asked for getting the values of h and y. I do not see, where the values of thses functions are calculated. Therefore it is still not clear to me, what you are asking for.
As far as I understand, you need something like this:
[t, y] = ode45(@(t, y) fcn(t, y, p), [0, 1], [0, 0]);
function dy = fcn(t, y, p)
a = t * p(1); % or what ever
b = y(1) * t^2;
dy = [a * y(2); b * y(1)];
end
Now you want to obtain the values of a and b for the evalulated time steps. Then:
[t, y] = ode45(@(t, y) fcn(t, y, p), [0, 1], [0, 0]);
[dy, a, b] = fcn(t, y.', p); % <== here a and b are requested
function [dy, a, b] = fcn(t, y, p)
a = t * p(1); % or what ever
b = y(1, :) * t .^ 2;
dy = [a .* y(2, :); b .* y(1, :)];
end
I'm not sure about the transpositions of y. Please debug this. The point is, that the function must accept a vector as input and replies the wanted arrays as 2nd or 3rd output argument, where they are ignore during the integration, but it is easy to calculate them afterwards, when the t and y values are available.
Does this match your question?
Answers (1)
Ignacio López
on 25 Feb 2019
1 Comment
Jan
on 25 Feb 2019
@Ignacio: If you need dy, you can use the 1st output of the function directly. Remember that "while ode45 is running" means even the rejected steps, which are not included in the replied trajectory. This is rarely useful.
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!