For loop does not change values of matrix

조회 수: 1 (최근 30일)
Jort Puiman
Jort Puiman 2023년 7월 31일
편집: Dyuman Joshi 2023년 7월 31일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by