```python
%matplotlib inline
```


```python
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sn

import pycollocation
```

Consider the following boundary value problem:
\begin{align}
    \frac{d T_1}{d_A} =& -q,\ T_1(0) = T_{1,0} \\
    \frac{d T_2}{d_A} =& -\frac{1}{2}q,\ T_2(A_{hx}) = T_{2,A_{hx}} \\
\end{align}
where $q = (T_1 - T_2)U$.

This boundary value problem describes a counter current heat exchanger; a hot liquid enters a device and exchanges heat across a metal plate with a cold liquid traveling through the device in the opposite direction. Here, $T_1$ is the temperature of the hot stream, $T_2$ is the temperature of the cold stream, $q$ is the rate of heat transfer from the hot fluid to the cold fluid, $U$ is the overall heat transfer coefficient and $A_hx$ is the total area of the heat exchanger.


```python
# provide numerical values for the parameters
params = {'T10': 130, 'T2Ahx': 70, 'U': 1.0}

def q(T1, T2, U):
    return (T1 - T2) * U

def rhs(A, T1, T2, U, **params):
    return [-q(T1, T2, U), -0.5 * q(T1, T2, U)]

def bcs_lower(A, T1, T2, T10, **params):
    return [T1 - T10]

def bcs_upper(A, T1, T2, T2Ahx, **params):
    return [T2 - T2Ahx]
```


```python
problem = pycollocation.problems.TwoPointBVP(bcs_lower, bcs_upper, 1, 2, params, rhs)
```


```python
def initial_mesh(domain, num, problem):
    As = np.linspace(domain[0], domain[1], num)
    T1s = np.repeat(0.5 * (problem.params['T10'] + problem.params['T2Ahx']), num)
    return As, T1s, T1s

```


```python
polynomial_basis = pycollocation.basis_functions.PolynomialBasis()
solver = pycollocation.solvers.Solver(polynomial_basis)

basis_kwargs = {'kind': 'Chebyshev', 'domain': [0, 5.0], 'degree': 15}

As, T1s, T2s = initial_mesh(basis_kwargs['domain'], 1000, problem)
T1_poly = polynomial_basis.fit(As, T1s, **basis_kwargs)
T2_poly = polynomial_basis.fit(As, T2s, **basis_kwargs)
initial_coefs = np.hstack([T1_poly.coef, T2_poly.coef])

solution = solver.solve(basis_kwargs, initial_coefs, problem)

```


```python
T1_soln, T2_soln = solution.evaluate_solution(As)
plt.plot(As, T1_soln)
plt.plot(As, T2_soln)
plt.show()
```


```python
T1_normalized_resids, T2_normalized_resids = solution.normalize_residuals(As)
plt.plot(As, np.abs(T1_normalized_resids))
plt.plot(As, np.abs(T2_normalized_resids))
plt.yscale('log')
plt.show()
```


```python

```

92a85d22bfcd61ba8c76fcae590008fe715f153c
ipynb
Jupyter Notebook
examples/heat-exchanger.ipynb
davidrpugh/bvp-solver
9376f3488a992dc416cfd2a4dbb396d094927569
MIT
2015-10-23T15:54:51.000Z
2021-06-23T15:31:17.000Z
examples/heat-exchanger.ipynb
davidrpugh/bvp-solver
9376f3488a992dc416cfd2a4dbb396d094927569
MIT
2015-03-26T15:35:39.000Z
2016-09-13T06:36:55.000Z
examples/heat-exchanger.ipynb
davidrpugh/bvp-solver
9376f3488a992dc416cfd2a4dbb396d094927569
MIT
2015-04-08T12:45:32.000Z
2021-07-19T08:56:50.000Z
Qwen/Qwen-72B
1. YES
2. YES