Event function Indexing Error

26 Ansichten (letzte 30 Tage)
Madeleine
Madeleine am 4 Dez. 2024 um 9:19
Beantwortet: Walter Roberson am 4 Dez. 2024 um 18:08
I wrote an event function to stop iteration when the x value of an equation reaches a specific point. It works for the first six initial conditions and then gives me this error:
Index exceeds the number of array elements. Index must not exceed 1.
Error in odezero (line 142)
if any(isterminal(indzc))
The Event function is:
function [value, isterminal, direction] = Xmoon(t, x, mu)
value = x - 1;
isterminal = 1;
direction = 0;
end
and the way i use it in my code is in a for loop that updates my initial conditions twenty times. It lloks like this:
IC = Xu_n;
opts = odeset('RelTol',1e-12,'AbsTol',1e-12,'Events',@Xmoon); % accuracy tolerances
[tspan,xyz_out] = ode45(@ZVC,tspan,IC,opts); % Propogate
x_5 = xyz_out(:,1);
y_5 = xyz_out(:,2);
x_5(end+1:1e4) = nan;
y_5(end+1:1e4) = nan;
x5(:,i) = x_5;
y5(:,i) = y_5;
What is going wrong and how do I fix it?
  2 Kommentare
Torsten
Torsten am 4 Dez. 2024 um 10:54
We cannot tell the reason without the complete code you are using.
But don't you have to set
opts = odeset('RelTol',1e-12,'AbsTol',1e-12,'Events',@(t,x)Xmoon(t,x,mu)); % accuracy tolerances
instead of
opts = odeset('RelTol',1e-12,'AbsTol',1e-12,'Events',@Xmoon); % accuracy tolerances
?
Walter Roberson
Walter Roberson am 4 Dez. 2024 um 18:05
Because the trailing parameter mu is not actually used in the event function, it is not necessary to pass it.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 4 Dez. 2024 um 18:08
The isterminal vector returned by the event function must be the same length as the value return.
Your IC are probably at least two values, so the x passed to Xmoon will be the same number of values; you then set value = x - 1; which will be the same number of values as IC. But you set isterminal to the scalar 1

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by