ODE matlab, handling variables

조회 수: 1 (최근 30일)
Juan David Gamba Camacho
Juan David Gamba Camacho 2019년 11월 19일
댓글: Juan David Gamba Camacho 2019년 11월 20일
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
Steven Lord
Steven Lord 2019년 11월 19일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2019년 11월 19일
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
Juan David Gamba Camacho 2019년 11월 20일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by