ODE matlab, handling variables

5 Ansichten (letzte 30 Tage)
Juan David Gamba Camacho
Juan David Gamba Camacho am 19 Nov. 2019
Hello,
I'm trying to change a variable once every integration step using Matlab's ODE solver (ode45).
[yd,k] = my_func(t,y,k)
a = func_a(k,y);
b = func_b(k,y);
k = b*a;
end
where k is initialized to be zero.
k=0;
[T,Z] = ode45( @my_func, [0 0.3], INITIAL_COND, options,k)
But, in this way the value of k doesn't change out of the function "py_func" (k is always read as zero), using Persistent and Global options will not solve the problem either because the integrator may evaluate the function many times before taking a certain step. Also persistent and global functionalities seem to slow-down the integration.
Does anyone know any other way for doing this implementation?
Thanks
  1 Kommentar
Steven Lord
Steven Lord am 19 Nov. 2019
Can you show us the mathematical form of the ODEs you're trying to solve? This approach of changing k sounds like it would be problematic when the ODE solver needs to reject a step and take a smaller step from the previous location. I want to understand the underlying problem, as that may help us offer a more robust solution.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

James Tursa
James Tursa am 19 Nov. 2019
You can't do this if the derivative yd depends on k. The ode solvers need to have consistency when calling the derivative function. E.g. the solvers might call your derivative function for a previous time when changing a step size internally, or they might even call your derivative function at the same point more than once. You have no control over this as it all happens in the background while the solvers try to keep the estimated errors within tolerances. If you suddenly change the parameters of the derivative function you effectively pull the rug out from under the ode solver and will confuse it greatly. You will either send it into a condition where it simply quits on you, or worse will simply give you a garbage answer. Changing the parameters of the derivative function during integration sounds like you really don't have an ODE to solve. Can you explain in more detail what you are trying to do and why?
  1 Kommentar
Juan David Gamba Camacho
Juan David Gamba Camacho am 20 Nov. 2019
Thanks for your response, I have managed to obtain 'k' from the current state 'y' and it's the code is working properly.
On the other hand, is it possible to implement something similar to the 'Hit Crossing' block of Simulink in Matlab? In this sense, it will be possible to help the solver to locate certain discontinuity without resorting to excessively small time steps (https://www.mathworks.com/help/simulink/ug/zero-crossing-detection.html).
I have implemented the ODE event location (https://www.mathworks.com/help/matlab/math/ode-event-location.html) but my simulation stills taking a lot of steps to finish the integration process, it seems that these events do not help the solver to find the presented discontinuities.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by