I dont understand the error
11 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
shir hartman
il 24 Ago 2022
Commentato: shir hartman
il 29 Ago 2022
I would like it to stop calculating the ode when both functions meet the requirement that dx=0 , and I did:
alpha=0.5;
beta=0.5;
r1=2;
r2=3;
s1=1;
s2=1;
t0 = 0;
tfinal = 100;
y0 = [1;1];
AnonFun = @(t,y)diag([2+0.5*y(2)-1*y(1),3+0.5*y(1)-1*y(2)])*y;
if (alpha>0)&&(beta>0)
Opt=odeset('Events',@(t,y)myEvent1(t,y,AnonFun));
Other
Opt=odeset('Events',@(t,y)myEvent2(t,y,AnonFun));
end
[t,y,te,ye,ie] = ode23(AnonFun,[t0 tfinal],y0,Opt);
plot (t,y)
function [value, isterminal, direction] = myEvent1(t,y,AnonFun)
value = AnonFun(t,y)-1.0e-3;
isterminal = 1; % Stop the integration
direction = -1;
end
function [value, isterminal, direction] = myEvent2(t,y,AnonFun)
value=abs(AnonFun(t,y))-0.001;
isterminal = 1; % Stop the integration
direction = -1;
end
But when I change the vector y0 to [1;5] for example, I got this message:
Index exceeds the number of array elements. Index must not exceed 1.
Error in odezero (line 142) if any(isterminal(indzc))
Error in ode23 (line 335) odezero(@ntrp23,eventFcn,eventArgs,valt,t,y,tnew,ynew,t0,h,f,idxNonNegative);
Error in LogisticGrowthForTwoSpecies (line 17)
[t,y,te,ye,ie] = ode23(AnonFun,[t0 tfinal],y0,Opt);
0 Commenti
Risposta accettata
Sam Chak
il 24 Ago 2022
Modificato: Sam Chak
il 27 Ago 2022
Edit: After understanding what you really want in your latest clarification. There is a simpler and intuitive way to code the program such that the simulation only run for approximately 3 or 4 times the Settling Time,
.
This allows
or
of the plot window to show the transient trajectories of the states from the initial values to the steady-state values. This method is only meaningful for systems that have stable equilibrium points.
Analysis shows that your system has a stable equilibrium point at
and
and three other unstable equilibrium points at
,
, and
.
Note: I changed the initial values for
and
because
and
, and to show you the difference between the settling time approach and the event function approach.
alpha = 0.5;
beta = 0.5;
r1 = 2;
r2 = 3;
s1 = 1;
s2 = 1;
t0 = 0;
tfinal = 5; % Adjust this parameter roughly 4 times the Settling Time, Ts
y0 = [6 6];
AnonFun = @(t,y) diag([2 + 0.5*y(2) - 1*y(1), 3 + 0.5*y(1) - 1*y(2)])*y;
% AnonFun = @(t,y) [(2 + 0.5*y(2) - 1*y(1))*y(1);
% (3 + 0.5*y(1) - 1*y(2))*y(2)];
[t, y] = ode23(AnonFun, [t0 tfinal], y0);
plot(t, y), grid on
y1 = y(:,1);
y1e = 14/3;
idx = find(t > 1 & y1/y1e > 0.98 & y1/y1e < 1.02); % applying 2% criterion after 1 sec
Ts = t(idx(1))
14 Commenti
Sam Chak
il 27 Ago 2022
I have updated my Answer to show you an alternative approach. But the following uses the Event function approach to force stop the ode45, so that you can see which one suits your needs.
alpha = 0.5;
beta = 0.5;
r1 = 2;
r2 = 3;
s1 = 1;
s2 = 1;
t0 = 0;
tfinal = 10;
y0 = [6 6];
AnonFun = @(t,y) diag([2 + 0.5*y(2) - 1*y(1), 3 + 0.5*y(1) - 1*y(2)])*y;
% AnonFun = @(t,y) [(2 + 0.5*y(2) - 1*y(1))*y(1);
% (3 + 0.5*y(1) - 1*y(2))*y(2)];
if (alpha > 0) && (beta > 0)
Opt = odeset('Events', @(t, y) myEvent1(t, y, AnonFun));
else
Opt = odeset('Events', @(t, y) myEvent2(t, y, AnonFun));
end
[t, y, te, ye, ie] = ode23(AnonFun, [t0 tfinal], y0, Opt);
plot(t, y), grid on
function [value, isterminal, direction] = myEvent1(t, y, AnonFun)
value = norm(AnonFun(t,y)) - 1e-2;
isterminal = 1; % Stop the integration
direction = -1;
end
function [value, isterminal, direction] = myEvent2(t, y, AnonFun)
value = norm(AnonFun(t,y)) - 1e-2;
isterminal = 1; % Stop the integration
direction = -1;
end
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



