-
Notifications
You must be signed in to change notification settings - Fork 2
/
chemical_reaction.py
43 lines (35 loc) · 1002 Bytes
/
chemical_reaction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import matplotlib.pyplot as plt
c1 = [50.0]
c2 = [25.0]
c3 = [0.0]
kl = 0.025
k2 = 0.01
time_difference = 0.1
t = 0.0
last_time = 15.0
i = 0
print("\nTime\tC1\tC2\tC3")
while t <= last_time:
print(f"{t:.2f}\t{c1[i]:.2f}\t{c2[i]:.2f}\t{c3[i]:.2f}")
c1_next = c1[i] + (k2 * c3[i] - kl * c1[i] * c2[i]) * time_difference
c2_next = c2[i] + (k2 * c3[i] - kl * c1[i] * c2[i]) * time_difference
c3_next = c3[i] + 2.0 * (kl * c1[i] * c2[i] - k2 * c3[i]) * time_difference
c1.append(c1_next)
c2.append(c2_next)
c3.append(c3_next)
i += 1
t += time_difference
if t >= 2.0:
time_difference = 0.2
if t == 6.0:
time_difference = 0.4
# Plotting the concentrations over time
time_points = [time for time in range(len(c1))]
plt.plot(time_points, c1, label='C1')
plt.plot(time_points, c2, label='C2')
plt.plot(time_points, c3, label='C3')
plt.xlabel('Time')
plt.ylabel('Concentration')
plt.title('Chemical Reaction Simulation')
plt.legend()
plt.show()