Recall, a rate equation looks like this: $$\frac{dy}{dt} = f(y,t).$$

- $y$ is the dependent variable and is a function of $t$, that is $y(t)$.
- $t$ is the independent variable.
- $f(y,t)$ is some function of $y$ and $t$; that is, some formula written in terms of $y$ and possibly $t$.
- We call this function the
*rate*or the*right hand side function*(RHS).

- We call this function the
- There is also an initial condition $$y(0)=y_0.$$

The exact solution is
$$C(t) = C_0e^{-t/\tau},$$
where $C_0$ is the initial concentration.

- Solve this in in Python with the Explicit Euler method with $\tau=1$, $C_0=1$ and $t_{end}=5$.
- Solve this in Excel with the Explicit Euler method $$f_{i+1} = f_i + \Delta t f(y_i,t_i),$$ $$C_{i+1} = C_i + \Delta t \left(-\frac{C_i}{\tau}\right).$$

In [ ]:

```
```

`odeint`

¶- New function
`odeint`

`from scipy.integrate import odeint`

- Questions:
- What arguments will this need?
- What will it return?
- Will you need to define a function and what form will it have?
- What does the
*solution*look like? (Like, what do you get as an*answer*?) - What were the things we needed/did in Excel?

- Try to solve the above ODE with $\tau=1$, $C_0=1$ and $t_{end}=5$.

- define the function
`f(y,t)`

- set the desired solution times
`t`

- set the initial condition
`C0`

- call
`odeint`

as`C=odeint(f, C0, times)`

`from scipy.integrate import odeint`

- plot the results

Solve this problem with $\tau=1$, $C_0=1$, and t_end=5.

In [4]:

```
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.integrate import odeint
```

In [5]:

```
#------ Step 1, define the function
#------ Step 2, set times to get the solution at
#------ Step 3, set initial condition
#------ Step 4, solve the problem
#------ Step 5 plot the answer
#------ Step 6: does the answer make sense? Analyse it.
```

Solve the rate equation for $v(t)$ at discrete points $t$ and plot the result:

$$\frac{dv}{dt} = g - cv^2.$$- $g=9.81$, $c=1$.
- $v_0 = 0$.
- Solve to $t=2$.

In [ ]:

```
```

What if we have multiple rate equations in multiple variables?

If we have multiple rate equations in multiple unknowns, we solve the problem in the same way, but using vector notation.

- This is similar to what we do for
`fsolve`

.

Solve for the velocity of a falling object: \begin{align} \frac{dv}{dt} &= g - cv^2, \\ \frac{dx}{dt} &= v. \end{align}

Here, g is gravitational acceleration, x is position, v is velocity, and c is a drag coefficient.

We first cast this in the form $$\frac{d\vec{y}}{dt} = \vec{f}(\vec{y},t).$$

This is equivalent to \begin{align} \frac{dy_0}{dt} &= f_0(y_0,y_1,t) \\ \frac{dy_1}{dt} &= f_1(y_0,y_1,t) \end{align}

For our example problem $v\equiv y_0$, $x\equiv y_1$, $g-cv^2=f_0(v,x,t)$, and $v=f_1(v,x,t)$

- Note, similiar to
`fsolve`

:- Pass in an array of variables,
- use a readable name,

- Recover variables from the array,
- Compute each rate in terms of the variables,
- Return an array of the rates.

- Pass in an array of variables,

- c = 1 m$^{-1}$
- g = 9.81 (m/s$^2$)
- t_end = 2 (s)
- $v_0 = 0$ (m/s)
- $x_0 = 0$ (m)

In [6]:

```
#------ Step 1, define the function
#------ Step 2, set times to get the solution at
#------ Step 3, set initial condition
#------ Step 4, solve the problem, recover v, x arrays
#------ Step 5 plot the answer
#------ Step 6: does the answer make sense? Analyse it.
```

Solve the following rate equation for a coal particle in a furnace. $$ \frac{dT}{dt} = \left(\frac{hA}{m c_p}(T_f - T) + \frac{\sigma A}{m c_p}(T_f^4-T^4)\right).$$

The initial particle temperature is $T_0=500$ K.

Use D=100 $\mu$m, tend = 0.05 s.

The following data is given:

Variable | Value | Units |
---|---|---|

$\rho_p$ | 1000 | kg/m$^3$ |

$c_p$ | 1380 | J/kg$\cdot$K |

$k$ | 0.1 | W/m$\cdot$K |

$Nu$ | 2 | -- |

$\sigma$ | 5.67E-8 | W/m$^2$K$^4$ |

$T_f$ | 1500 | K |

Also, the area, mass and Nusselt number $Nu$ are given, respectively, by: $$ A = \pi D^2, $$ $$ m = \frac{\pi}{6}D^3\rho_p,$$ $$ Nu = \frac{hD}{k}.$$

In [ ]:

```
```

We are performing a chemical reaction as follows. $$\mbox{Rxn 1: }\phantom{xxx} A+B\rightarrow C $$ $$\mbox{Rxn 2: }\phantom{xxx} B+C\rightarrow D $$ Here, symbols $A$, $B$, $C$, $D$ denote species concentrations in mol/L.

The initial concentrations are $A_0=1$, $B_0=1$, $C_0=0$, $D_0=0$. Also, $k_1=1\,L/mol\cdot s$, and $k_2=1.5\,L/mol\cdot s$.

The concentrations obey the following rate equations:

\begin{align} \frac{dA}{dt} &= -k_1AB, \\ \frac{dB}{dt} &= -k_1AB - k_2BC, \\ \frac{dC}{dt} &= k_1AB - k_2BC, \\ \frac{dD}{dt} &= k_2BC. \end{align}Solve for the concentrations of $A$, $B$, $C$, and $D$ as functions of time to $t=3$ s.

Plot the concentrations of A, B, C, and D as functions of time on the same plot. Label the axes as "time (s)" and “concentration (mol/L)”.

In [ ]:

```
```

In [ ]:

```
```