The Parameter of ODE is not Updated When the EventCallback Function is Called

1 次查看(过去 30 天)
I was simulating ODEs which incorporate additional parameter (ydot = f(t,y,P)).
There is an event occurs in the simulation process and the event is processed with odeEvent function.
The eventCallback function is illustrated as follows:
function [stop,Ye,Pout] = impactEventCallback(te,ye,ie,Pin)
% ... parameter updating calculation
fprintf("parameter befor updating : %.5f After updating : %.5f \n",Pin.v,Pout.v); % output updating result
end
The simulation result is demonstrated as follows:
parameter befor updating : 0.10000 After updating : 0.37232
parameter befor updating : 0.10000 After updating : 0.08382
parameter befor updating : 0.10000 After updating : 0.06401
...
It seems that every time the impactEventCallback function is called, the parameter before updating (Pin) is not changed.
I suspect that the updated parameter Pout is not used by the governing equations ydot = f(t,y,P).
The expected result should be that the next time the value of Pin is the last time value of Pout.
Can anyone give some advices about how to fix this issue ?

回答(1 个)

Chuguang Pan
Chuguang Pan 2024-12-28
移动:Walter Roberson 2024-12-28
I have solved this isssue with the following odeEvent object definition:
E = odeEvent("EventFcn",@impactEvent,"Direction","ascending","CallbackFcn",@impactEventCallback,"Response","callback");
However, the previous definition of odeEvent object is :
E = odeEvent("EventFcn",@(t,y,P) impactEvent(t,y,P),"Direction","ascending","CallbackFcn",@(te,ye,ie,P) impactEventCallback(te,ye,ie,P),"Response","callback");
The difference is that the EventFcn and CallbackFcn are defined utilize function handle syntax explicitly.
I am confused why the implicity syntax of function handle definition is effective yet the explicity syntax is not.
The simulation result after fixing is illustrated as follows:
parameter befor updating : 0.10000 After updating : 0.37243
parameter befor updating : 0.37243 After updating : 0.18519
parameter befor updating : 0.18519 After updating : 0.22448
parameter befor updating : 0.22448 After updating : 0.13929
...

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by