Event function Indexing Error
26 ビュー (過去 30 日間)
古いコメントを表示
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
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
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
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!