# How can you implement a loop inside an ODE 45 function

6 views (last 30 days)
Carmen Escalante on 7 Jul 2020
Answered: Steven Lord on 7 Jul 2020
I am trying to operate a loop inside an ODE 45 function and it keeps returning that the indeces in the left side are not compatible with the right side. It was fuctioning perfect before I added the loop so I'm guessing the error is coming from here. Any tips on how to solve this? I have also tried incorporating the mux and mul variables into the loop but continues to have the same problem. Thank you
for i=1:numzones
IphlzF = Iphl0.*exp(-al.*x(1).*(z+zstep/2).*acclF);
IphlzB = Iphl0.*exp(-al.*x(1).*(z-zstep/2).*acclF);
Iphlz= ( IphlzF + IphlzB)/2;
IphzFs(i,1) = sum(IphlzF);
IphzBs(i,1) = sum(IphlzB);
Iphzs(i,1) = sum(Iphlz);
qphz(i,1) = -(IphzFs(i,1) - IphzBs(i,1))/(x(1).*zstep);
qs(i,1) = qsm * tanh(Ysphm*qphz(i)/qsm);
qs_av = mean(qs);
upre=(qs-ms)*Yxs;
z = z + zstep;
end
mux=mumax*(x(3)/(x(3)+Kxs+((x(3)).^2)/Kixs))*(x(4)/(x(4)+Kxn+((x(4)).^2)/Kixn))*(IphzFs/(IphzFs+Kxl+(IphzFs.^2/Kixl)));
mul=qlmax*(x(3)/(x(3)+Kls+((x(3)).^2)/Kils))*(Kinl/(x(4)+Kinl)*(IphzFs/(IphzFs+Kli+(IphzFs.^2/Kili))));
%ODE
dx(1)=mux.*x(1);
dx(2)=mul.*x(1);
dx(3)=(-(1/Yxs)*dx(1))-((1/Yls)*dx(2));
dx(4)=(-(1/Yxn)*dx(1));
dx(5)=(k1*(x(3)/(x(3)+Kgas))*(x(4)/(x(4)+Kgan+((x(4)).^2))/Kigan));
dx(6)=(k2*(x(3)/x(3)+Kfas)*(x(4)/x(4)+Kfan));
dx(7)=-Kh*(dx(3));
dx=dx';
end