<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chapter 8 :: Coding For Chemists</title>
    <link>https://codingforchemistsbook.com/book_material/chapter-8/index.html</link>
    <description>Data There is no data required for Chapter 8.&#xA;Alternatively, individual files can be found in the Data section.&#xA;Code from chapter &#39;&#39;&#39; A code to numerically solve a reaction mechanism that is irreversible but has two intermediates using solve_ivp from scipy.integrate Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.0.0 - 250217 - initial version &#39;&#39;&#39; import scipy.integrate as spi from codechembook.quickPlots import quickScatter # First we need to define a function containing our differential rate laws. def TwoIntermediatesDE(t, y, k): &#39;&#39;&#39; Differential rate laws for a catalytic reaction with two intermediates: A + cat -&gt;(k1) X X + B -&gt;(k2) Y Y -&gt;(k3) C + cat REQUIRED PARAMETERS: t (float): the cutrent time in the simulation. Not explicitly used but needed by solve_ivp y (list of float): the current concentration k (list of float): the rate RETURNS: (list of float): rate of change of concentration in the order of y &#39;&#39;&#39; A, B, cat, X, Y, C = y # unpack concentrations to convenient variables k1, k2, k3 = k # unpack rates dAdt = -k1 * A * cat dBdt = -k2 * X * B dcatdt = -k1 * A * cat + k3 * Y dXdt = k1 * A * cat - k2 * X * B dYdt = k2 * X * B - k3 * Y dCdt = k3 * Y return [dAdt, dBdt, dcatdt, dXdt, dYdt, dCdt] # Set up initial conditions and simulation parameters y0 = [1.0, 1.0, 0.2, 0.0, 0.0, 0.0] # concetrations (mM) [A, B, cat, X, Y, C] k = [5e1, 1e1, 5e0] # rate (1/s) time = [0, 10] # simulation start and end times (s) # Invoke solve_ivp and store the result object solution = spi.solve_ivp(TwoIntermediatesDE, time, y0, args = [k]) # Plot the results quickScatter(x = solution.t, # need a list of six identical time axes y = solution.y, name = [&#39;[A]&#39;, &#39;[B]&#39;, &#39;[cat]&#39;, &#39;[X]&#39;, &#39;[Y]&#39;, &#39;[C]&#39;], xlabel = &#39;Time (s)&#39;, ylabel = &#39;Concentration (mM)&#39;, output = &#39;png&#39;) &#39;&#39;&#39; A code to numerically solve a first order kinetics problem using solve_ivp from scipy.integrate Written by: Ben Lear and Chris Johnson (authors@codechembook.com) v1.0.0 - 250217 - initial version &#39;&#39;&#39; import numpy as np import scipy.integrate as spi from plotly.subplots import make_subplots # First we need to define a function containing our differential rate law. def FirstOrderDE(t, y, k): &#39;&#39;&#39; Differential rate law for first-order kinetics REQUIRED PARAMETERS: t (float): the cutrent time in the simulation. Not explicitly used but needed by solve_ivp y (float): the current concentration k (float): the rate Returns: (float): the rate of change of concentration with time &#39;&#39;&#39; dydt = -k * y return dydt # Set up initial conditions and simulation parameters y0 = 1.0 # concetration (mM) k = 1.0 # rate (1/s) time = [0, 10] # simulation start and end times (s) # Invoke solve_ivp and store the result object as solution solution = spi.solve_ivp(FirstOrderDE, time, [y0], args = [k]) # Compute the known analytical solution at each simulation time point anal_y = np.exp(-1 * k * solution.t) # Plot the numerical and analytical solutions and the difference between them fig = make_subplots(2, 1) fig.add_scatter(x = solution.t, y = solution.y[0], mode = &#39;markers&#39;, name = &#39;Numerical&#39;) # The data from the numerical solution fig.add_scatter(x = solution.t, y = np.exp(-1 * k * solution.t), mode = &#39;lines&#39;, name = &#39;Exact&#39;) # The corresponding analytical solution fig.add_scatter(x = solution.t, y = 100 * (solution.y[0] - anal_y) / anal_y, name = &#39;Error&#39;, row = 2, col = 1) # The percent difference between them # Update the plot appearance fig.update_xaxes(title = &#39;Time (s)&#39;) fig.update_yaxes(title = &#39;Concentration (mM)&#39;, row = 1) fig.update_yaxes(title = &#39;Percent Difference&#39;, row = 2) fig.update_layout(template = &#39;simple_white&#39;) fig.show(&#39;png&#39;) Solutions to Exercises Targeted exercises Simulating the kinetics of one elementary reaction using scipy.solve ivp Exercise 0 Working from the code presented in Section Simulating_the_kinetics_of_one_elementary_reaction_using_scipy.solve_ivp, write a script that plots the maximum error in the simulation versus the relative tolerance specification within scipy.solve_ivp(). You can use the rtol keyword to set this. Use a range of rtol values that span $10^{-1}$ to $10^{-9}$ changing by factors of 10.</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <managingEditor>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</managingEditor>
    <webMaster>authors@codingforchemistsbook.com (Benjamin Lear and Christopher Johnson)</webMaster>
    <atom:link href="https://codingforchemistsbook.com/book_material/chapter-8/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>