MATLAB Answers


ODE matlab, handling variables

Juan David Gamba Camacho さんによって質問されました 2019 年 11 月 19 日 13:48
最新アクティビティ Juan David Gamba Camacho さんによって コメントされました 2019 年 11 月 20 日 15:23
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;
where k is initialized to be zero.
[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?

  1 件のコメント

Steven Lord
2019 年 11 月 19 日 14:34
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.

サインイン to comment.

1 件の回答

James Tursa
回答者: James Tursa
2019 年 11 月 19 日 18:05

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 件のコメント

Juan David Gamba Camacho 2019 年 11 月 20 日 15:23
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 (
I have implemented the ODE event location ( 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.

サインイン to comment.

Translated by