```python
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact 
```

$%\newcommand{\fou}[2]{\widehat{#1}^{(#2)}}$
$\newcommand{\fou}[2]{\widehat{#1}}$

# Item XI

$%\newcommand{\fou}[2]{\widehat{#1}^{(#2)}}$
$\newcommand{\fou}[2]{\widehat{#1}}$
Use the Fourier transfrom to solve the following PDE:

\begin{align}
u_t - k u_{xx} &= 0 \text{ and } t>0
\\u(x,0) &= f(x) \, , \,  x \in \mathbb{R}
\end{align}
Assume $f$ and $u \in L^{2}(\mathbb{R})$.


---

We apply the Fourier transform with respect to the variable $x$ on both equations.

In the first one:
\begin{align}
\fou{u_t}{x} - k \fou{u_{xx}}{x} &= 0
\\ \fou{u_t}{x} - k(2\pi i \xi)^2 \fou{u}{x} &= 0
\\ \fou{u_t}{x} + k(2\pi \xi)^2 \fou{u}{x} &= 0 \, .
\end{align}
We now use the following property (see this [link](https://www.math.ubc.ca/~feldman/m267/pdeft.pdf)):
$$
\fou{\frac{\partial u}{\partial t}}{x}(\xi,t) = \frac{\partial \fou{u}{x}(\xi,t)}{\partial t} \,.
$$ and
\begin{align}
\\ \left(\fou{u}{x}\right)_t + k(2\pi \xi)^2 \fou{u}{x} &= 0 
\\ \left(\fou{u}{x}\right)_t &= -k(2\pi \xi)^2 \fou{u}{x} \, .
\end{align}
The solution to this ordinary PDE is:
\begin{align}
\fou{u}{x}(\xi,t) = c(\xi)e^{-k(2\pi \xi)^2 t} 
\end{align}

We can see that at $t=0$:
\begin{align}
\fou{u}{x}(\xi,0) &= c(\xi)
\\ \fou{f}{x}(\xi) &= c(\xi)
\end{align}

Finally, the solution is:
\begin{align}
\fou{u}{x}(\xi,t) &= \fou{f}{x}(\xi)e^{-k(2\pi \xi)^2 t} 
\\ u(x,t) &= \int_{-\infty}^{\infty} \fou{f}{x}(\xi)e^{-k(2\pi \xi)^2 t} e^{2\pi i \xi x} d\xi
\\ &= \int_{-\infty}^{\infty} \fou{f}{x}(\xi)e^{-k(2\pi \xi)^2 t} e^{2\pi i \xi x} d\xi
\end{align}


```python
def discretize(fn,xi,xf,N=101):
    xs = np.linspace(xi,xf,num=N)
    fs = fn(xs)
    return fs

# The discrete fourier transform
def discrete_fourier_transform(fs,derivate=0,inverse=False):
    N = fs.shape[0]
    ns = np.arange(N)
    if inverse:
        ts = [(1.0/N)*np.sum(fs*np.exp(1j*2*np.pi*k/N*ns)) for k in range(N)]
    else:
        ts = [np.sum(fs*np.exp(-1j*2*np.pi*k/N*ns)) for k in range(N)]
    return np.array(ts)
```


```python
def solve_pde(f,K,xi,xf,tf,N=300,M=50):
    fx = discretize(f,xi,xf,N=N) # u(x,0)
    ees = np.arange(N)/(xf-xi)
    fxt = discrete_fourier_transform(fx)
    ts = np.linspace(0,tf,num=M)
    funct = lambda t: fxt*np.exp(-K*(2*np.pi*ees)**2*t)
    uts = [discrete_fourier_transform(funct(t),inverse=True) for t in ts]
    return uts
```


```python
X_I = -4
X_F = 4
KK = 0.2
T_F = 10.0

# We test with a function, say:
F_MAX = 1
fn = lambda x: np.exp(-x**2.0)
us = solve_pde(fn,K=KK,xi=X_I,xf=X_F,tf=T_F)
xs = np.linspace(X_I,X_F,num=us[0].shape[0])

def plot_at_time(i=0):
    plt.plot(xs,us[i],'-')
    plt.ylim((0,F_MAX))
    plt.grid()
    plt.title("$u(x,t=%f)$"%(T_F*i/(len(us)-1.0)))
    plt.show()

interact(plot_at_time,i=(0,len(us)-1),continuous_update=False)
```


    interactive(children=(IntSlider(value=0, description='i', max=49), Output()), _dom_classes=('widget-interact',…





    <function __main__.plot_at_time(i=0)>




```python

```

10913745bd48fff48fc651adabae2bb62000c409
ipynb
Jupyter Notebook
t1_questions/item_11.ipynb
autopawn/cc5-works
63775574c82da85ed0e750a4d6978a071096f6e7
MIT
t1_questions/item_11.ipynb
autopawn/cc5-works
63775574c82da85ed0e750a4d6978a071096f6e7
MIT
t1_questions/item_11.ipynb
autopawn/cc5-works
63775574c82da85ed0e750a4d6978a071096f6e7
MIT
Qwen/Qwen-72B
1. YES
2. YES