Coupled Differential Equations
Contents
Coupled Differential Equations#
Everything we’ve done so far has involved functions that depend only on independent variables:
e.g.,
Sometimes, functions can depend on other functions. e.g. given \(y_1(x)\) & \(y_2(x)\)
are coupled ODEs. Sometimes, we can solve by substitution (e.g. take derivative of equation 1 above and substitute into equation 2) but often will want/need to solve simultaneously.
Chemical Engineering Example#
Tanks in series example
No feed to first tank; \(F_1\) and \(F_{out}\) are volumetric flow rates controlled by resistance valves and are proportional to water height in each tank ( which is a function of time)
System is coupled because \(F_1\) depends on both \(h_1(t)\) and \(h_2(t)\).
Tank 1 MB:
where \(\tau \equiv A_iR_i \ [=] \ time\) is the “residence time”
Tank 2 MB:
Convert two first-order diff eq to a second order diff eq#
One approach you can take is to calculate the derivative of the \(1^{st}\) MB and using it to eliminate \(h_2(t)\)
And (from the \(1^{st}\) MB):
Plug those into MB #2
simplify:
whew! Now we need to solve for \(h_1(t)\) and then plug back in to find \(h_2(t)\)
Too much work! Can we solve for \(h_1(t)\) and \(h_2(t)\) simultanously?
Solving as coupled differential equations#
Now let’s set
Then we have:
Look familiar?
Let’s foray into derivative matrices before returning to our tank problem
Property:
Vector derivative example#
\(\underline{\text{Ex}}\): If \(\vec{y}(x) = \begin{bmatrix}4t & 3t^2 \\ 2t^3 & t \end{bmatrix}\)
\(\vec{y'}(x) = \begin{bmatrix}4 & 6t \\ 6t^2 & 1 \end{bmatrix}\)
Continuing!#
Goal is to solve \(\vec{y'} = \frac{d}{dt} \vec{y} = \arr{A}\vec{y}\) for \(\vec{y}(t)\)
\(y_1(t)\) and \(y_2(t)\) are coupled functions, not basis functions.
Let’s assume \(\vec{y}\) takes the form \(\vec{y} = \vec{x}e^{\lambda t}\) wher \(\vec{x}\) is a vector of constants
Plugging back into \(\vec{y'} = \arr{A}\vec{y}\) gives
\(\therefore\)Any set of \(y_1(t) = x_1e^{\lambda t}\) and \(y_2(t) = x_2e^{\lambda t}\) that fits \(\lambda\vec{x} = \arr{A} \vec{x}\) are solutions. Therefore, finding the eigenvalues and eigenvectors of \(\arr{A}\) will yield solution.
Example#
By assuming that \(\vec{y} = \vec{x}e^{\lambda t}\), we can find the eigenvalues and vectors of \(\arr{A}\) to identify \(\vec{x}\) and \(\lambda\) to solve system.
Find \(\lambda\) and \(\vec{x}\) by solving
Find eigenvector \(\vec{x}^{(1)}\) associated with \(\lambda_1 = -2\).
solve
Set \(x_1 = 1\) (arbitrary) then \(x_2 = -\frac{1}{3}\)
\(\therefore \vec{x}^{(1)} = \begin{bmatrix} 1 \\ -\frac{1}{3} \end{bmatrix}\) and \(\vec{y} = \begin{bmatrix} 1 \\ -\frac{1}{3}\end{bmatrix} e^{-2t}\) is a solution.
Enough?
No. Two coupled \(1^\circ\) ODEs will yield two solutions with two arbitrary constants.The rest of the solution comes from \(2^{nd}\) eigenvalue and eigenvector
Find eigenvector \(\vec{x}^{(2)}\) associated with \(\lambda_2 = -1\)
\(\therefore x^{(2)} = \begin{bmatrix} 1 \\ -\frac{1}{2} \end{bmatrix} \) and \(\vec{y} = \begin{bmatrix} 1 \\ -\frac{1}{2} \end{bmatrix} e^{-t}\) is a solution
The general solution to \(\vec{y}' = \arr{A}\vec{y}\) is
We have two constants in total from two integrations. We began with two \(1^\circ\) ODEs
Let’s check our answer:
Plug into one original equation:
And the other:
Now, let’s return to our tanks:
\(\vec{h}' = \arr{A}\vec{h}\) where
Assume solution of form \(\vec{h}= \vec{x}e^{\lambda t}\)
Solve by inserting values for \(R_2,R_2,\tau_1\) and \(\tau_2 \rightarrow\) then find eigenvalues (\(\lambda_1\) and \(\lambda_2\)) and eigenvectors (\(\vec{x}^{(1)}\) and \(\vec{x}^{(2)}\)) of \(\arr{A}\)
General solution will be
For example, given:
Find eigenvalues
Find \(\vec{x}^{(1)}\) associated with \(\lambda_1 = -0.06 \ hr^{-1}\)
Find \(\vec{x}^{(2)}\) associated with \(\lambda_2 = -0.34\ hr^{-1}\)
General solution is:
Numerical calculation of eigenvalues/eigenvectors#
Remember we can get the eigenvectors/eigenvalues from scipy.
import numpy as np
# get the eigenvalues/eigenvectors and print them
# hint np.linalg.eig
A = np.array([[-0.1, 0.1],
[0.1,-0.3]])
eigval, eigvec = np.linalg.eig(A)
print(eigval)
print(eigvec)
print(eigvec[:,0]/eigvec[1,0])
[-0.05857864 -0.34142136]
[[ 0.92387953 -0.38268343]
[ 0.38268343 0.92387953]]
[2.41421356 1. ]
Numerical solutions to coupled differential equations#
No real change from solve_ivp!
Try solving this with solve_ivp using the initial conditions \(h_1(t=0)=5\)m and \(h_2(t=0)=7\)m
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
import numpy as np
A1 = 5 #m^2
R1 = 0.1 # hr/m^2
tau1 = A1*R1
A2 = 5 #m^2
R2 = 1 #hr/m^2
tau2 = A2*R2
h0 = [4,7] #m
def hprime(t, h):
A = np.array([[-1/tau1,1/tau1],
[R2/R1/tau2,-1/tau2*(1+R2/R1)]])
return A@h
sol = solve_ivp(hprime, [0,10], h0)
plt.plot(sol.t, sol.y.T,'o-')
plt.xlabel('Time [hr]')
plt.ylabel('Tank Height [m]')
Text(0, 0.5, 'Tank Height [m]')