# Modeling and Simulation in Python

SymPy code for Chapter 16

Copyright 2017 Allen Downey

License: [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0)


### Mixing liquids

We can figure out the final temperature of a mixture by setting the total heat flow to zero and then solving for $T$.


```python
from sympy import *

init_printing() 
```


```python
C1, C2, T1, T2, T = symbols('C1 C2 T1 T2 T')

eq = Eq(C1 * (T - T1) + C2 * (T - T2), 0)
eq
```


```python
solve(eq, T)
```

### Analysis

We can use SymPy to solve the cooling differential equation.


```python
T_init, T_env, r, t = symbols('T_init T_env r t')
T = Function('T')

eqn = Eq(diff(T(t), t), -r * (T(t) - T_env))
eqn
```

Here's the general solution:


```python
solution_eq = dsolve(eqn)
solution_eq
```


```python
general = solution_eq.rhs
general
```

We can use the initial condition to solve for $C_1$.  First we evaluate the general solution at $t=0$


```python
at0 = general.subs(t, 0)
at0
```

Now we set $T(0) = T_{init}$ and solve for $C_1$


```python
solutions = solve(Eq(at0, T_init), C1)
value_of_C1 = solutions[0]
value_of_C1
```

Then we plug the result into the general solution to get the particular solution:


```python
particular = general.subs(C1, value_of_C1)
particular
```

We use a similar process to estimate $r$ based on the observation $T(t_{end}) = T_{end}$


```python
t_end, T_end = symbols('t_end T_end')
```

Here's the particular solution evaluated at $t_{end}$


```python
at_end = particular.subs(t, t_end)
at_end
```

Now we set $T(t_{end}) = T_{end}$ and solve for $r$


```python
solutions = solve(Eq(at_end, T_end), r)
value_of_r = solutions[0]
value_of_r
```

We can use `evalf` to plug in numbers for the symbols.  The result is a SymPy float, which we have to convert to a Python float.


```python
subs = dict(t_end=30, T_end=70, T_init=90, T_env=22)
r_coffee2 = value_of_r.evalf(subs=subs)
type(r_coffee2)
```




    sympy.core.numbers.Float




```python
r_coffee2 = float(r_coffee2)
r_coffee2
```


```python

```

c8323988806652f8c00ff5b4397f305d34f76e64
ipynb
Jupyter Notebook
notebooks/chap16sympy.ipynb
kanhaiyap/ModSimPy
af16c079ec398ff9b3822d3dcda75873ce900ced
MIT
2019-04-27T22:43:12.000Z
2019-11-11T15:12:23.000Z
notebooks/chap16sympy.ipynb
ffriass/ModSimPy
c36a476a20042acb33773e47d12aea5b0c413e60
MIT
2019-10-09T18:50:22.000Z
2022-03-21T01:39:48.000Z
notebooks/chap16sympy.ipynb
ffriass/ModSimPy
c36a476a20042acb33773e47d12aea5b0c413e60
MIT
Qwen/Qwen-72B
1. YES
2. YES