S.N.
5/13/2022
-Project is divided into two distinct parts
-Part one uses octave and matrices to balance chemical reactions
-Part two uses python and RK4 to approximate chemical reaction rates
Before performing any chemical reaction, it is important to have a balanced chemical equation
This allows for an appropriate molar ratio of reagents
Some reactions can be balanced by inspection, while others are difficult (libretexts.org)
\[ \begin{aligned} &\text{easy:} &NaCl & \rightarrow Na^+ + Cl^- \\ \\ &\text{difficult:} & 4CO_{2} + 2H_{2}O & \rightarrow 2C_{2}H_{2} + 5O_{2} \end{aligned} \]
An equation can be created for each element in the reaction (Sanchez, 2016).
\[ \begin{aligned} &X_{1}CO_{2} + X_{2}H_{2}O \rightarrow X_{3}C_{2}H_{2} + X_{4}O_{2} \\ \\ &C: 1X_{1} = 2X_{3} \\ \\ &O: 2X_{1} + 1X_{2} = 2X_{4} \\ \\ &H: 2X_{2} = 2X_{3} \end{aligned} \]
\[ \begin{bmatrix} 1 & 0 & -2 \\ 2 & 1 & 0 \\ 0 & 2 & -2 \end{bmatrix} \begin{bmatrix} X_{1} \\ X_{2} \\ X_{3} \end{bmatrix} =\begin{bmatrix} 0 \\ 2 \\ 0 \end{bmatrix} \]
disp('Begin by creating matrix A next to I3')
A = [1, 0, -2; 2, 1, 0; 0, 2, -2]; #creates matrix A
I = [1, 0, 0; 0, 1, 0; 0, 0, 1]; #identity matrix 3
b = [0;2;0]; #matrix containing free values of X4
C = [A,I] #setup to find `inv(A)`
[M,N] = size(A);
#eliminates lower triangle down then right
for j = 1:N-1 #column goes from 1 to the second to last column
for i = j+1: M #row goes column+1 to the last row
Column_Number = j;
Row_Number = i;
m = C(i,j)/C(j,j);
C(i,:) = C(i,:) - m*C(j,:);
end
end
# eliminates upper triangular up then left
for j = M:-1:2 #goes backwards from far right to second column
for i = j-1:-1:1
Column_Number = j;
Row_Number = i;
m = C(i,j)/C(j,j);
C(i,:) = C(i,:) - m*C(j,:);
end
end
#makes every diagonal entry in A 1
for i = 1:N
C(i,:) = C(i,:)/C(i,i); #row divided by diagonal entry
end
#D is inverse matrix, or the last three columns of the matrix
[Q,R] = size(C); #size of inverse matrix
disp('Perform Gauss-Jordan elimination then extract inv(A) from
the right half of C')
D = C(:,R-2:R) #extracts inverse matrix from C
disp('Solve using x=inv(A)*b')
E = D*b #Solution vector
disp('make solutions into integers by dividing by lowest entry.
Note: the denominator of each element in fraction form is
the coefficient for X4')
#turns every solution into integers
E = E/min(E)
C = setup to find inverse, D = inv(A), E is Ax=b solutions
C =
1 0 -2 1 0 0
2 1 0 0 1 0
0 2 -2 0 0 1
D =
0.2000 0.4000 -0.2000
-0.4000 0.2000 0.4000
-0.4000 0.2000 -0.1000
E =
0.8000
0.4000
0.4000
The rate of chemical reactions can be modeled with ODE's (libretexts.org) and approximated with RK4 using python
\[ \begin{aligned} A &\rightarrow B \\ \\ \frac{d[A]}{dt} &= -k[A] \\ \\ \int_{A_{0}}^{A_{t}} \frac{1}{[A]}dA &= -k \int_{0}^{t}dt \\ \\ [A_{t}] &= [A_{0}]e^{-kt} \end{aligned} \]
The RK4 code from HW4 works perfectly in this instance
The octave program successfully found inv(A) and equation coefficients
The python program successfully approximated the analytical solution, however it took at least 20 nodes to visually appear accurate.
Homework 4
Homework 6
Ch7.3 Part 4 Notes