Event function and ODE.
41 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Arnab
am 20 Nov. 2024 um 13:17
Kommentiert: Arnab
am 22 Nov. 2024 um 13:34
I was solving an one dimensional ODE using ODE45.
I have two events in event function, where the execution of second event is dependent on the time instant of the first event.
How could I do that? Please help!
Elaborately...
1) Second event will comes into existence only after the execution of first event and is dependent on the time instant of first event.
for example..
Let first event instant be t=te. Then second instant will be at t=te+1.
3 Kommentare
Steven Lord
am 21 Nov. 2024 um 22:54
Can you show the mathematical form of the ODE you're trying to solve, not the code? Show it and describe what it's computing in text, please.
Akzeptierte Antwort
Torsten
am 20 Nov. 2024 um 13:24
Verschoben: Torsten
am 20 Nov. 2024 um 13:29
Don't use both events simultaneously in your event function.
Start the solver with the first event function and integrate until the first event happens.
Return control to the calling program.
Restart the solver with the value of the solution variable obtained so far and the second event function (or up to te+1).
7 Kommentare
Torsten
am 21 Nov. 2024 um 22:20
Bearbeitet: Torsten
am 22 Nov. 2024 um 1:32
I'm not sure if this is what you want.
tfinish = 1.0;
tstart = 0.0;
treached = tstart;
tend = tfinish;
tspan = [tstart, tend];
y0 = 2;
options = odeset('Events',@event);
T = [];
Y = [];
while 1
[t, y] = ode45(@fun, tspan, y0, options); % solving the ode
T = [T;t];
Y = [Y;y];
treached = t(end);
if treached >= tfinish
break
end
tstart = treached;
tend = min(tfinish,treached + 0.01);
tspan = [tstart, tend];
y0 = y(end,1);
[t, y] = ode45(@fun, tspan, y0); % solving the ode
T = [T;t];
Y = [Y;y];
treached = t(end);
if treached >= tfinish
break
end
tstart = treached;
tend = tfinish;
tspan = [tstart,tend];
y0 = y0-0.5*y(end,1);
end
plot(T,Y)
function [value,isterminal,direction] = event(t,y) % when to stop the integration
% y is symbolic variable
% t is a vector
M=2;
ep=0.1;
T=1;
V=abs(y(1));
u=V/(1+V); %black curve
B=-log(M*M/ep)/T;
value =u-M*exp(B*t); %blue curve
isterminal = 1; %flag to stop the integration
direction = 0; %dummy variable
end
function dy = fun(~,y) % intermediate dunction to solve the ode
dy=((y(1))^1.5)*sign(y(1)); % given ode
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!