MATLAB: Index exceeds matrix dimensions.

matrix

Hello dear Matlab Community,
I would like to request your help again (it's really difficult to resist, seeing how helpful you were in the last days…and how long it takes me to not figure out the solution of these problems!)
I know that this post looks quite alike one of yesterday but I unfortunately got stuck again: now my initial condition y0 for solving a system of two ODE is an array with two values.
But when running the code below I get: Index exceeds matrix dimensions. Error in NewTry>myODE (line 30) Tp=y(2);
I suspect that this might be related to a syntax issue and the way the initial condition is written as "y0" in my ODE solving equation because if I replace y0 with it s actual initla values (as follows) matlab solves the problem without complaining.
[L,y]=ode45(@(L,y) myODE(L,y,Aap,Acp),Pool(k:k+1),[30,5.2]);
So I don't understand why Tp can't be associated to the second element of the array of y when y0 has clearly been declared with two elements…
Merci!!
a =1;
b =2;
Pool = [0,a,b];
y0=[30,5.2];
LAll=[];
yAll=[];
for k=1:numel(Pool)-1
switch k
case rem(k,2)==1
Aap=a; Acp=Aap;
otherwise
Aap=2*b; Acp=0;
end
[L,y]=ode45(@(L,y) myODE(L,y,Aap,Acp),Pool(k:k+1),y0);
LAll=cat(1,LAll,L);
yAll=cat(1,yAll,y);
y0=y(end,1);
end
plot(LAll,yAll(:,:));
function dy = myODE(L,y,Aap,Acp)
global u
u=y(1);
global Tp
Tp=y(2);
dudL = myODE2(L,u,Tp,Aap,Acp);
dTpdL = myODE1(L,u,Tp,Aap,Acp);
dy =[dudL;dTpdL];
end
function dudL = myODE2(~,u,Tp,Aap,~)
Psatp = 0.13*exp(18-3800/(Tp+227.03));
Phi= 1-exp(-(47*u^2+0.1*Tp*u^1.06));
Ppw= Psatp*Phi;
dudL=-(Aap)*(Ppw/(Tp+273.15)-0.19);
end
function dTpdL=myODE1(L,u,Tp,Aap,Acp)
dTpdL=(140-Tp)*Acp+(100-Tp)*Aap-Aap/(Tp+273);
end

Best Answer

y0=y(end,:);
instead of
y0=y(end,1);