How to fix: Index exceeds array bounds?
Show older comments
Hey guys, I have a problem with my code. I programmed an Event-ODE-Function which works fine. Check my previous question fore more background about this here. I post my full code after the jump. Now I want to add another ODE into my existing code which should integrate only once but should change based on the old events and their triggers.
My previous code works alright and I'll mark the new parts with this: %new%. Now I get the error message Index exceeds array bounds. Can you help me fixing it?
function mfc
clc
%%parameters
m1=10; m2=10; m3=500; k3=300; k2=(k3*(m1+m2))/m3; my=m1/(m1+m2); FW=10; s=0.4; r=0.4; om=5.73; dv0s=2; dv0=dv0s-my*r*om;
%%starting parameters
tspan = [0 100];
tstart = tspan(1);
tend = tspan(end);
y0 = [0 dv0 0 0];
% initiate starting function
t = tstart;
y = y0;
fcn = @eqmi;
Ifcn = @impulsi; %new%
opt = odeset('Events', @ZustandI);
ateout = [];
ayeout = [];
aieout = [];
%%main loop
while t(end) < tend
% Integriere bis zum Stopp durch Event-Funktion
[at,ay,ate,aye,aie] = ode45(fcn, [t(end) tend], y(end,:), opt);
[at,I,ate,aIe,aie] = ode45(Ifcn, [t(end) tend], 0, opt); %new%
% new starting parameters
t = cat(1, t, at(2:end));
y = cat(1, y, ay(2:end,:));
ateout = [ateout; ate];
ayeout = [ayeout; aye];
aieout = [aieout; aie];
% new functions and events
if (-s<y(end,1)-y(end,3) && y(end,1)-y(end,3)<s)
fcn = @eqmi;
Ifcn = @impulsi; %new%
opt = odeset('Events', @ZustandI);
elseif y(end,1)-y(end,3)<=-s
fcn = @eqmii;
Ifcn = @impulsii; %new%
opt = odeset('Events', @ZustandII);
elseif y(end,1)-y(end,3)>=s
fcn = @eqmiii;
Ifcn = @impulsiii; %new%
opt = odeset('Events', @ZustandIII);
end
end
%%functions
function dy=eqmi(t,y)
dy=zeros(4,1);
dy(1)=y(2);
dy(2)=my*r*om^2*sin(om*t);
dy(3)=y(4);
dy(4)=(FW*sin(w3*t)-k3*y(3))/m3;
end
function dy=eqmii(t,y)
dy=zeros(4,1);
dy(1)=y(2);
dy(2)=my*r*om^2*sin(om*t)-(k2*(y(1)-y(3)-s))/(m1+m2);
dy(3)=y(4);
dy(4)=(FW*sin(w3*t)-k3*y(3)+k2*(y(1)-y(3)-s))/m3;
end
function dy=eqmiii(t,y)
dy=zeros(4,1);
dy(1)=y(2);
dy(2)=my*r*om^2*sin(om*t)-(k2*(y(1)-y(3)+s))/(m1+m2);
dy(3)=y(4);
dy(4)=(FW*sin(w3*t)-k3*y(3)+k2*(y(1)-y(3)+s))/m3;
end
function dI=impulsii (t,I) %new%
dI=k2*(y(1)-y(3)-s);
end
function dI=impulsiii (t,I) %new%
dI=k2*(y(1)-y(3)+s);
end
function dI=impulsi (t,I) %new%
dI=0; % I and dI should be zero at event ZustandI
end
%%Events
function [value,isterminal,direction] = ZustandI(t,y)
value = [double(y(1)-y(3)>-s),double(y(1)-y(3)<s)];
isterminal = [1,1];
direction = [0,0];
end
function [value,isterminal,direction] = ZustandII(t,y)
value = double(y(1)-y(3)<=-s);
isterminal = 1;
direction = 0;
end
function [value,isterminal,direction] = ZustandIII(t,y)
value = double(y(1)-y(3)>=s);
isterminal = 1;
direction = 0;
end
end
I'll leave the plots out for now. Thanks in advance guys.
2 Comments
"Now I get the error message Index exceeds array bounds."
Sure. But you post one hundred lines of code and nothing telling us where the error actually occurs. If you really want to get help, then post the complete error message. This means all of the red text.
madhan ravi
on 4 Oct 2018
W3 undefined
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!