ODEs 3

ODE system

Example

The following reactions are given:

$$A + B \rightarrow C,$$

$$A + C \rightarrow D.$$

* Reactions have rate constants $k_1 = 1$ and $k_2 = 2$. * Let $A_0=B_0=1$ and $C_0=D_0=0$. * Solve to 5 seconds. * Species concentrations are given by the following rate equations:

Solve this system using the fourth order RK method:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def odeRK4(f, y0, t):
    ns = len(t)-1
    y  = np.zeros((len(t), len(y0)))
    y[0,:] = y0
    
    for k in range(ns):
        h = t[k+1] - t[k]
        S1 = f(y[k,:],t[k])
        S2 = f(y[k,:]+0.5*h*S1, t[k]+0.5*h)
        S3 = f(y[k,:]+0.5*h*S2, t[k]+0.5*h)
        S4 = f(y[k,:]+    h*S3, t[k]+    h)
        y[k+1,:] = y[k,:] + h/6*(S1 + 2*S2 + 2*S3 + S4)
        
    return y
def rhsf(ABCD, t):
    A = ABCD[0]
    B = ABCD[1]
    C = ABCD[2]
    D = ABCD[3]
    
    k1 = 1
    k2 = 2
    
    dAdt = -k1*A*B - k2*A*C
    dBdt = -k1*A*B
    dCdt =  k1*A*B - k2*A*C
    dDdt =  k2*A*C
    
    return np.array([dAdt, dBdt, dCdt, dDdt])
ABCD_initial = np.array([1,1,0,0])    
tend = 5
t = np.linspace(0,tend,100)

ABCD = odeRK4(rhsf, ABCD_initial, t)
plt.rc('font', size=14)
plt.plot(t,ABCD)
plt.xlabel('t')
plt.ylabel('Concentration')
plt.xlim([0,tend])
plt.ylim([0,1])
plt.legend(['A', 'B', 'C', 'D'], frameon=False);
rxn sys

Higher order derivatives

Question: can we solve this using the techniques we have used for first order ODE’s? If so, how?

Can you convert this one third-order equation to a system of three first-order equations?

$$y^{\prime\prime\prime}=\frac{d^3y}{dt^3} = f(y,y^{\prime},y^{\prime\prime},t).$$

Decoupled equations

$$AV = V\Lambda,$$

$$A = V\Lambda V^{-1}.$$

This analysis can be useful for solving ODEs analytically, but also for analyzing (and modifying) stability properties of ODEs.

ODE Step Size

Consider integrating $dy/dt = \tanh(t)$ * What part of the solution will dictate the step size?

Suppose we know the error $\Delta$ for a given step size $\Delta t=h_1$. * We’ll show how to get $\Delta$ below.

Suppose we set some desired error that we are okay with on a given step, like $|\Delta|\le\epsilon = atol + |y|rtol.$ * A large $y\rightarrow$ rtol controls; * A small $y\rightarrow$ atol controls.

Given a known error for a known step, how can we change our step to get the desired error? Assume we are using the RK4 method.

Adjust $h$ to get the desired error. * For a globally $4^{th}$ order method $\rightarrow$ $\Delta = \mathcal{O}(h^5)$ $\rightarrow$ $\Delta\sim h^5$. * Then

$$\frac{\Delta_2}{\Delta_1} = \frac{\epsilon}{\Delta_1} = \left(\frac{h_2}{h_1}\right)^5,$$

$$\rightarrow h_2 = h_1\left(\frac{\epsilon}{\Delta_1}\right)^{1/5}.$$

* So, guess an initial $h_1$. * If the error $\Delta_1$ is too big, then redo the step using a smaller $h$ as computed using the above equation. * If the error is too small, take the step, but do the next step with a larger $h$ computed using the equation above.

How to compute $\Delta$

Step doubling

(A)   <----h---->|<----h---->
(B)   <---------2h---------->

Felberg

See Hoffman