Event function Indexing Error

26 ビュー (過去 30 日間)
Madeleine
Madeleine 2024 年 12 月 4 日 9:19
回答済み: Walter Roberson 2024 年 12 月 4 日 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 件のコメント
Torsten
Torsten 2024 年 12 月 4 日 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 2024 年 12 月 4 日 18:05
Because the trailing parameter mu is not actually used in the event function, it is not necessary to pass it.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 12 月 4 日 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

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by