ODE45 randomly returns vector of length 1.

I am solving ODEs with a script I run many times, and often what is happening is that I can run it several times sucessfully while changing parameter values every time, but then all of the sudden I get the error "script returns a vector of length 1,..." and then I can't run the script again because I keep getting this error. This has happened me seemingly randomly multiple times, and I can usually shut my computer down and either the next day or two, the error just stops happening even though I do not make any changes to the code.
Is this something that happens to other people, and am I missing something obvious? I could include my script if that helps, but this error happens to me with almost every ODE script I have to run multiple times while changing parameter values.
Thanks for any help.
EDIT: My files are included in the comments. When I call the ODE file, I get the error that the returned vector is of length of 1, but when I just compute the dxdt explicitly from the ODE file, I obtain a vector of length 3 so I'm not sure where the problem is.

2 Comments

Can you share your code? Maybe is a problem definition.
Here is my code:
clear all
%HLO Mutual Invasion
global b B a d f m h v u g
B=0.0625; %(0.0049,0,09)
a=0.3; %(0.4,2.7)
d=0.08; %(0.004,0.6)
h=0.07; %(0.33,1) maybe equal to g
u=0.5; %(0.07,0.9)
g=0.07; %(0.33,1) maybe equal to h
v=1/3; %v>0 in scenario 1
b=0.3;
f=3.2;
m=1.7;
sres=0.74;
Jump=0.01; %Grid step size
RosMacCond=f-m-b*(f+m); %>0 for stable steady state in xy system, <0 for periodic orbit
Tinit=800;
options = odeset('RelTol',1e-8,'AbsTol',[1e-6 1e-6 1e-6]);
Tmax=500;
PC=0; %Percentage trackers
PCold=0;
[TT,SS]=meshgrid(Jump:Jump:1-Jump,sres*Jump:sres*Jump:sres);
[J,I]=size(TT);
MU=zeros(size(TT)); %Floquet multiplier
OIC=zeros(size(TT));
for i=1:I
for j=1:J
Ts=TT(1,i);
s=SS(j,1);
clear X T
x0=0.5;
y0=0;
z0=0.5;
[T,X]=ode45(@HLOode,[0 Tmax],[x0,y0,z0],options);
xbar=X(end,1);
zbar=X(end,3);
LIC(j,i)=f*xbar/(b+xbar)-m;
PCold=PC;
PC=floor(100*((i-1)*J+j)/(I*J));
if PC==PCold+1
disp(['Lynx InvCond (xbar=',num2str(xbar),', RMC=',num2str(RosMacCond),'): ',num2str(PC),'% done.'])
end
end
end
LIC=LIC;
figure(1)
contour(TT,SS,LIC,[0 0],'m')
hold on
contour(TT,SS,LIC,[0.3 0.3],'m--')
hold on
axis([0 1 0 sres])
PC=0; %Percentage trackers
PCold=0;
Tinit=3000; %If RosMacCond<0, run xy0 for Tinit to get on orbit, then extract orbit during Tfinal time
Tfinal=300;
if RosMacCond>0
xbar=m*b/(f-m); %xbar value in xy system
disp(['xbar=',num2str(xbar),'. RM Condition = ',num2str(RosMacCond)])
OIC=TT.*(h.*xbar^2./(B+xbar^2)+SS./v-u)+(1-TT).*(g.*xbar./(d+xbar)-u); %Owl Invasion Condition
figure(1)
xlabel('T')
ylabel('s')
clear title
titlestr=['Invasion Conditions (fp). B=',num2str(B),', b=',num2str(b),', d=',num2str(d),', g=',num2str(g),', h=',num2str(h),', m=',num2str(m),', u=',num2str(u),', v=',num2str(v)];
title(titlestr)
hold on
contour(TT,SS,OIC,[0 0],'g')
hold on
contour(TT,SS,OIC,[0.1 0.1],'g--')
hold on
axis([0 1 0 sres])
elseif RosMacCond<0
IndexPeaks=0;
for i=1:I
for j=1:J
Ts=TT(1,i);
s=SS(j,1);
x0=0.5;
y0=0.5;
z0=0;
clear X T
[T,X]=ode45(@HLOode,[0 Tinit],[x0,y0,z0],options);
x0 = X(end,1); %Get on periodic orbit and take endpoint
y0 = X(end,2);
z0 = X(end,3);
while length(IndexPeaks)<2 %Run system until we get >1 full orbit. Increase Tfinal until we do
Tfinal=Tfinal+100;
clear X T
[T,X]=ode45(@HLOode,[0 Tfinal],[x0,y0,z0],options);
Tinter=linspace(T(1),T(end),100*Tfinal);
Hinter=interp1(T,X(:,1),Tinter);
Linter=interp1(T,X(:,2),Tinter);
[HPeaks,IndexPeaks]=findpeaks(Hinter);
end
PerStart=Tinter(IndexPeaks(1));
PerEnd=Tinter(IndexPeaks(2));
Period=PerEnd-PerStart;
CycleT=Tinter(IndexPeaks(1):IndexPeaks(2));
CycleH=Hinter(IndexPeaks(1):IndexPeaks(2));
CycleL=Linter(IndexPeaks(1):IndexPeaks(2));
A=Ts.*(h.*CycleH.^2./(B+CycleH.^2)+s./v-u)+(1-Ts)*(g*CycleH./(d+CycleH)-u);
MU(j,i)=(1/Period)*trapz(A);
PCold=PC;
PC=floor(100*((i-1)*J+j)/(I*J));
if PC==PCold+1
disp(['Owl InvCond (Periodic Orbit, xbar=',num2str(xbar),', RMC=',num2str(RosMacCond),'): ',num2str(PC),'% done.'])
end
end
end
figure(1)
xlabel('T')
ylabel('s')
clear title
titlestr=['Invasion Conditions -(po). B=',num2str(B),', b=',num2str(b),', d=',num2str(d),', g=',num2str(g),', h=',num2str(h),', m=',num2str(m),', u=',num2str(u),', v=',num2str(v)];
title(titlestr)
hold on
contour(TT,SS,MU,[0 0],'g')
hold on
contour(TT,SS,MU,[10 10],'g--')
hold on
axis([0 1 0 sres])
else
disp('RosMacCond=0, zero eigenvalue in Hare-Lynx system.')
end
The function file HLOode is
function dxdt=HLOode(t,x)
global b B a d f m h v u g Ts s
dxdt=[Ts.*(x(1).*(1-x(1))-x(1).*x(2)./(b+x(1))-x(1).^2.*x(3)./(B+x(1).^2))+(1-Ts).*(-x(1).*x(2)./(b+x(1))-a.*x(1).*x(3)./(d+x(1))); f.*x(1).*x(2)./(b+x(1))-m.*x(2); Ts.*(h.*x(1).^2.*x(3)./(B+x(1).^2)+s.*x(3)./(v+x(3))-u.*x(3))+(1-Ts).*(g.*x(1).*x(3)./(d+x(1))-u.*x(3))];
end

Sign in to comment.

Answers (2)

James Tursa
James Tursa on 28 Apr 2020
Edited: James Tursa on 28 Apr 2020
Try clearing your variables before you run the script. It could be that you are inadvertently using a variable from a previous iteration.

1 Comment

I put clearvars at the start of my script, the error persists.

Sign in to comment.

If it happens with almost every script that you run, though not immediately, then it is something you are doing that is strange, because that is not behavior anyone else ever sees. Passing variables around using globals is always dangerous as hell, because it make things difficult to debug. I'm not sure where the problem arises, but I would suggest using the debugger. Set it to trap out when an error occurs. Then look very carefully at the variables involved, at how the functions are called. You may need to wander around in the stack to figure out the problem, but this is certainly not anything I have ever seen or heard of. And since you do not appear to be doing anything overtly dangerous with memory, that means you have done something nasty in the code.
If I had to make a guess, I wonder if possibly globals are indeed causing your problem. clear all should kill them off. But you have created something strange here, and the most likely cause seems to be the heavy use of globals.
So you might add an additional call to clear at the beginning to test this out:
clear GLOBAL
Though I do see this claim in the help for clear.
clear ALL removes all variables, globals, functions and MEX links.
Using the debugger is the only viable way to track it down.

1 Comment

Do you have a suggestion for how to call variables into a function file then, as I am doing with my ODE, without using global variables? I can't tell what the error is via debugging but I am not well versed with these MATLAB details yet...
I have an edit in my question which maybe specifies more what the error could be.
I took my ODE function file and put it at the end of my original script, and now the error is that 'Ts is an undefined function or variable', even though I define it in the if loop right above where I call ode45. Any idea what is going on here?

Sign in to comment.

Products

Release

R2017b

Asked:

on 28 Apr 2020

Edited:

on 29 Apr 2020

Community Treasure Hunt

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

Start Hunting!