```python
import numpy as np
import sympy as s
```


```python
from sympy import init_printing
init_printing(use_latex = True)
```


```python
x , nu , t = s.symbols('x nu t')
phi = (s.exp(-(x - 4*t)**2/(4 * nu * (t + 1)))) + (s.exp(-(x - 4*t - 2*s.pi)**2/(4 * nu * (t + 1))))
phi
```


```python
phiprime = phi.diff(x)
phiprime
```


```python
from sympy.utilities.lambdify import lambdify
u = (-2 * nu/phi) * phiprime + 4
u
```


```python
ufunc = lambdify((t , x , nu) , u)
print(ufunc(1 , 4 , 3))
```

    3.49170664206445



```python
from matplotlib import pylab
nx = 200
nt = 200
dx = 2*np.pi/(nx - 1)
nu = 0.07
dt = dx * nu

x = np.linspace(0 , 2 * np.pi , nx)
un = np.empty(nx)
t = 0
u = np.asarray([ufunc(t , x0 , nu) for x0 in x])
u
```




    array([4.        , 4.0315738 , 4.06314759, 4.09472139, 4.12629518,
           4.15786898, 4.18944277, 4.22101657, 4.25259036, 4.28416416,
           4.31573796, 4.34731175, 4.37888555, 4.41045934, 4.44203314,
           4.47360693, 4.50518073, 4.53675452, 4.56832832, 4.59990211,
           4.63147591, 4.66304971, 4.6946235 , 4.7261973 , 4.75777109,
           4.78934489, 4.82091868, 4.85249248, 4.88406627, 4.91564007,
           4.94721387, 4.97878766, 5.01036146, 5.04193525, 5.07350905,
           5.10508284, 5.13665664, 5.16823043, 5.19980423, 5.23137803,
           5.26295182, 5.29452562, 5.32609941, 5.35767321, 5.389247  ,
           5.4208208 , 5.45239459, 5.48396839, 5.51554218, 5.54711598,
           5.57868978, 5.61026357, 5.64183737, 5.67341116, 5.70498496,
           5.73655875, 5.76813255, 5.79970634, 5.83128014, 5.86285394,
           5.89442773, 5.92600153, 5.95757532, 5.98914912, 6.02072291,
           6.05229671, 6.0838705 , 6.1154443 , 6.14701809, 6.17859189,
           6.21016569, 6.24173948, 6.27331328, 6.30488707, 6.33646087,
           6.36803466, 6.39960846, 6.43118225, 6.46275605, 6.49432985,
           6.52590364, 6.55747744, 6.58905123, 6.62062503, 6.65219882,
           6.68377261, 6.71534638, 6.74692008, 6.77849348, 6.81006563,
           6.84163265, 6.87317848, 6.90463692, 6.93573494, 6.96534702,
           6.98884198, 6.98731033, 6.88594413, 6.42420025, 5.05281199,
           2.94718801, 1.57579975, 1.11405587, 1.01268967, 1.01115802,
           1.03465298, 1.06426506, 1.09536308, 1.12682152, 1.15836735,
           1.18993437, 1.22150652, 1.25307992, 1.28465362, 1.31622739,
           1.34780118, 1.37937497, 1.41094877, 1.44252256, 1.47409636,
           1.50567015, 1.53724395, 1.56881775, 1.60039154, 1.63196534,
           1.66353913, 1.69511293, 1.72668672, 1.75826052, 1.78983431,
           1.82140811, 1.85298191, 1.8845557 , 1.9161295 , 1.94770329,
           1.97927709, 2.01085088, 2.04242468, 2.07399847, 2.10557227,
           2.13714606, 2.16871986, 2.20029366, 2.23186745, 2.26344125,
           2.29501504, 2.32658884, 2.35816263, 2.38973643, 2.42131022,
           2.45288402, 2.48445782, 2.51603161, 2.54760541, 2.5791792 ,
           2.610753  , 2.64232679, 2.67390059, 2.70547438, 2.73704818,
           2.76862197, 2.80019577, 2.83176957, 2.86334336, 2.89491716,
           2.92649095, 2.95806475, 2.98963854, 3.02121234, 3.05278613,
           3.08435993, 3.11593373, 3.14750752, 3.17908132, 3.21065511,
           3.24222891, 3.2738027 , 3.3053765 , 3.33695029, 3.36852409,
           3.40009789, 3.43167168, 3.46324548, 3.49481927, 3.52639307,
           3.55796686, 3.58954066, 3.62111445, 3.65268825, 3.68426204,
           3.71583584, 3.74740964, 3.77898343, 3.81055723, 3.84213102,
           3.87370482, 3.90527861, 3.93685241, 3.9684262 , 4.        ])




```python
from matplotlib import pyplot
pyplot.figure(figsize=(11, 7), dpi=100)
pyplot.plot(x, u, marker='o', lw=2)
pyplot.xlim([0, 2 * np.pi])
pyplot.ylim([0, 10]);
```


```python
for n in range(nt):
    un = u.copy()
    for i in range(1, nx-1):
        u[i] = un[i] - un[i] * dt / dx *(un[i] - un[i-1]) + nu * dt / dx**2 *\
                (un[i+1] - 2 * un[i] + un[i-1])
    u[0] = un[0] - un[0] * dt / dx * (un[0] - un[-2]) + nu * dt / dx**2 *\
                (un[1] - 2 * un[0] + un[-2])
    u[-1] = u[0]
        
u_analytical = np.asarray([ufunc(nt * dt, xi, nu) for xi in x])
```


```python
pyplot.figure(figsize=(11, 7), dpi=100)
pyplot.plot(x,u, marker='o', lw=2, label='Computational')
pyplot.plot(x, u_analytical, label='Analytical')
pyplot.xlim([0, 2 * np.pi])
pyplot.ylim([0, 10])
pyplot.legend();
```

799fe4e8920d6cc5559ed5806c2db839b088e25b
ipynb
Jupyter Notebook
CFD/CFD4.ipynb
ali-kin4/JupyterNotes-1
aeb944b43da4f731b46f25758f804110dd421fec
MIT
CFD/CFD4.ipynb
ali-kin4/JupyterNotes-1
aeb944b43da4f731b46f25758f804110dd421fec
MIT
2021-05-17T07:45:07.000Z
2021-05-17T09:25:10.000Z
CFD/CFD4.ipynb
ali-kin4/JupyterNotes-part1
aeb944b43da4f731b46f25758f804110dd421fec
MIT
Qwen/Qwen-72B
1. YES
2. YES