Event function Indexing Error

조회 수: 2 (최근 30일)
Madeleine
Madeleine 2024년 12월 4일
답변: Walter Roberson 2024년 12월 4일
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일
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일
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일
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 CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by