For loop does not change values of matrix

6 次查看(过去 30 天)
Hi everyone,
I want to model the concentration gradient in a centrifuge over distance and time. However, I am struggling getting the wall description to work. I divided the centrifuge in cells, and I want to fill each cell at the wall until a certain concentration cmax is reached. Then, I want to place the excess in a previous cell. For this, I made a for-loop that uses the precalculated concentration, and reassesses every cell from the wall to the center. When running the code, I see that the for-loop does not change anything. Could anyone help me with this problem and explain to me why the for-loop does not change anything? Below the function, also I inserted the files I use.
function [dcdt] = odepaper(t, c, Nr, dr, vgs, cmax,cini,n)
dcdt=zeros(Nr,1);
r = zeros(Nr,1);
v = zeros(Nr,1);
excess= zeros(Nr,1);
%% Loop
for i=2:Nr
r(i) = r(i-1)+dr;
v(i) = (1-c(i))^n*vgs;
vin = (1-cini)^n*vgs;
rin = 0.5*dr;
if i == 1 % Initial conditions
dcdt(1)=(1/dr)*((rin*cini*vin)/r(i)-c(i)*v(i));
elseif i == Nr % Wall conditions
dcdt(Nr) = (1/dr)*((r(i-1)*c(i-1)*v(i-1))/r(i))+excess(i);
else % Everything in between
dcdt(i) = (1/dr)*((r(i-1)*c(i-1)*v(i-1))/r(i)-c(i)*v(i))+excess(i);
end
end
%% Mass replacement
for i = Nr:-1:2 % Force system to give back excess to previous cells
c(i) = c(i)+excess(i);
if c(i)>cmax
excess(i) = c(i)-cmax;
c(i) = cmax;
c(i-1) = c(i-1)+excess(i);
end
end
end
  1 个评论
Dyuman Joshi
Dyuman Joshi 2023-7-31
编辑:Dyuman Joshi 2023-7-31
It does change something. You can remove the semicolons and then run the code to see the outputs and changes the code does, as it is written to.
However, the 2nd for loop does not have any effect on the outcome of the ODE function.
I assume you want to store the array excess for each iteration of the solver?
Edit - Or maybe change the order of the loops.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-7-31
You initialize excess to zero.
Your first for loop reads from excess but never writes to it, so it is going to read back zeros each time.
Then the first for loop ends.
The second for loop sometimes writes into excess.
Then the second for loop ends.
The first for loop has already ended so the first for loop cannot be using the modified values.
  1 个评论
Jort Puiman
Jort Puiman 2023-7-31
Ah yes, you're completely right. I put the second for-loop before my ODE and now I see that the mass is replaced to previous cells. Thank you!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by