MATLAB Answers

How can you implement a loop inside an ODE 45 function

6 views (last 30 days)
Carmen Escalante
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

  0 Comments

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 7 Jul 2020
Which specific line of your code is listed in the error message?
Set a breakpoint on that line and look at the sizes of what you get when you evaluate the right side of the expression on that line. If it's one of the lines where you assign to one of the elements 1 through 7, it's likely you're trying to fit something that's not a scalar into that scalar element of dx.
Trying to put half a dozen eggs into one cup in a cardboard egg carton (without breaking the eggs) isn't going to work unless the egg carton was designed to hold one huge egg, like an ostrich egg, and you're trying to store chicken or quail eggs in it. But in that case the ostrich egg carton would be more like a cell in a cell array, and you're working with regular numeric arrays.

  0 Comments

Sign in to comment.