Run "if" logic on a row

4 views (last 30 days)
Robert Demyanovich
Robert Demyanovich on 20 Jun 2021
Commented: Joel Lynch on 20 Jun 2021
I have the following code for the reaction interface between acid and base (A-acid, B-base,S-salt):
dS=k1*cA(i+1,:).*cB(i+1,:)*dt;
IDX = (cA(i+1,:)<=NC | cB(i+1,:)<=NC); % Logical indexing
%if in a column for this time iteration either cA or cB is less than NC
%then set the value of dS equal to zero (i.e. no reaction occurred at
%that location
dS(IDX)=0;
where cA and cB are arrays, dS is a vector with the same number of columns as cA and cB and dt is the time increment.
Now I want to analyze each element of the current row (i+1, the time iteration) in cA and cB based on what the corresponding value is in the column (space increment) of the vector dS. If dS equals zero, I want to set cA and cB to one set of values (no reaction has occurred). If dS is greater than zero, then reaction occurred and a different line of code is used to set the value of the concentration in that cell in either cA or cB.
If dS > 0
cA(i+1,:)=cA(i+1,:)-dS;
cB(i+1,:)=cB(i+1,:)-dS;
if dS = 0, seems to be more tricky. In this case the acid and base concentrations are set by the water dissociation constant, Kw. The code needs to determine which array (cA or cB) has the higher value for the concentration. Then the concentration of the one with the lower value is calculated based on Kw and the higher concentration. So if cA = .02 and cB = .0000001 in equivalent cells in cA and cB (equivalent based on row/column), then cA = 0.02 and cB = kW/cA.
Coming from a visual basic (VB) background, I have a hard time understanding how to handle many cells at once as MatLab can do. Clearly, it's more efficient and I don't want to use VB logic to code this issue.

Accepted Answer

Joel Lynch
Joel Lynch on 20 Jun 2021
Edited: Joel Lynch on 20 Jun 2021
The trick is to recognize that you can use & / | (and/or) to chain multiple logical operations together. In this case, you can get logical inidices that match both dS=0 and cA>cB.
% indicies of columns where dS==0 AND cA>cB
idx = dS==0 & cA(i+1,:)>cB(i+1,:);
cB(i+1,idx) = kW/cA(i+1,idx);
% inidices of colums where dS==0 AND cB>cA
idx = dS==0 & cA(i+1,:)<cB(i+1,:);
cA(i+1,idx) = kW/cB(i+1,idx);
The various operators used in matlab are summarized here, and can be useful when learning the language.
  6 Comments
Robert Demyanovich
Robert Demyanovich on 20 Jun 2021
I had a feeling that was the problem. I actually did change the first dS line to include i+1, but not the others. So, at least I'm starting to get a feel for how this works.
No more errors, but the results are now way off. I need to figure out why my logic is wrong.
Yes, I am stepping through a chemical reaction that is virtually instantaneous. When I assume that the concentrations at the reaction interface are zero (because of the instantaneous rxn), I get the results that are to be expected. I'm now trying to change that to include Kw, and the results are as I said way off. I'm surprised because I'm trying to set the concentrations at the reaction plane to 1E-07 (pH =7) for both acid and base instead of concentrations of zero. Since the starting concentrations are 5 orders of magnitude higher, I would have thought this is pretty darn close to zero so the results should be almost identical. Must be a logic error somewhere.
I was not aware of the built-in stiff ODE solver. It's true that if I am not careful about the number of steps chosen for both time and space, the algorithm blows up. I guess the ODE solver would fix this. I only purchased the bare bones MatLab - so I'm not sure if the built in solver is available to me.
Joel Lynch
Joel Lynch on 20 Jun 2021
ODE solvers should be included in the base software. MATLAB has some good documentation on how to choose and implement an ode scheme. I would be vary careful using euler integration, unless you know the system is non-stiff.
Questions on chemical kinetics are probably outside the scope of this forum, but a standard way to test stiff ODE's is to continually reduce your time-step size. If your value converges to a constant result, stiffness probably isn't the most immediate problem.
Good luck!

Sign in to comment.

More Answers (0)

Categories

Find more on Chemistry in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!