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

44 views (last 30 days)
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 ?

Answers (1)

Chuguang Pan
Chuguang Pan on 28 Dec 2024 at 2:00
Moved: Walter Roberson on 28 Dec 2024 at 3:24
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
...

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!