For loop does not change values of matrix
8 views (last 30 days)
Show older comments
Jort Puiman
on 31 Jul 2023
Edited: Dyuman Joshi
on 31 Jul 2023
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 Comment
Dyuman Joshi
on 31 Jul 2023
Edited: Dyuman Joshi
on 31 Jul 2023
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.
Accepted Answer
Walter Roberson
on 31 Jul 2023
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!