# Week 1 worksheet: Introduction to elliptic PDEs

This worksheet contains a number of exercises covering both the analytical and numerical aspects of the course. This means that some parts require you to solve the problem by hand, i.e. with pen and paper, while other parts require you to write code. It should usually be obvious which parts require which.

#### Suggested reading

You will see lists of links to further reading and resources throughout the worksheets, in sections titled **Learn more:**. These will include links to the Python documentation on the topic at hand, or links to relevant book sections or other online resources. Unless explicitly indicated, these are not mandatory reading, although of course we strongly recommend that you consult them!

#### Displaying solutions

Solutions will be released after the workshop, as a new `.txt` file in the same GitHub repository. After pulling the file to Noteable, **run the following cell** to create clickable buttons under each exercise, which will allow you to reveal the solutions.


```python
%run scripts/create_widgets.py W01
```






    <IPython.core.display.Javascript object>


    Buttons created!


*How it works: You will see cells located below each exercise, each containing a command starting with `%run scripts/show_solutions.py`. You don't need to run those yourself; the command above runs a script which automatically runs these specific cells for you. The commands in each of these cells each create the button for the corresponding exercise. The Python code to achieve this is contained in `scripts/show_solutions.py`, and relies on [IPython widgets](https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20Basics.html) --- feel free to take a look at the code if you are curious.*


```javascript
%%javascript
MathJax.Hub.Config({
    TeX: { equationNumbers: { autoNumber: "AMS" } }
});
```


    <IPython.core.display.Javascript object>


## Exercise 1

Consider the the function
$$
u(x,y) = x^4 - 6x^2y^2 + y^4
$$

### Part a)

Verify that $u(x,y)$ satisfies the Laplace equation.




```python
import sympy as sp
x,y = sp.symbols('x y')
u = x**4-6*x**2*y**2+y**4
dx = sp.diff(u,x)
dx2 = sp.diff(dx,x)
dy = sp.diff(u,y)
dy2 = sp.diff(dy,y)
print(f'{dx2}  and {dy2}')
if dx2+dy2 ==0:
    print(f'The function {u} satisfies the elliptical Laplacian Partial Differential Equation')
```

    12*x**2 - 12*y**2  and -12*x**2 + 12*y**2
    The function x**4 - 6*x**2*y**2 + y**4 satisfies the elliptical Laplacian Partial Differential Equation


### Part b)

Generate a surface plot of $u(x,y)$ in the square given by $0\le x\le1$ and $0\le y\le 1$.

- Check the [numpy.meshgrid](https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html) documentation


```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def u(x,y):
    return x**4-6*x**2*y**2+y**4

values_x = np.linspace(0,1,100)
values_y = np.linspace(0,1,100)
X,Y = np.meshgrid(values_x,values_y)
Z = u(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
#ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
surf = ax.plot_surface(X, Y, Z,cmap = cm.coolwarm,linewidth=0, antialiased=False)
plt.show()

def y_zero_x_in_range_from_zero_to_one():
    y_zero = 0
    x_zero = np.linspace(0,1,10)
    z_values = []
    for i in x_zero:
        z_values.append(u(i,y_zero))
    plt.plot(x_zero,z_values, label = 'y = 0')
    print('The boundary condition for u(x,0) = x**4 on the range 0<=x<=1')
    plt.xlabel('x-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
y_zero_x_in_range_from_zero_to_one()

def y_one_x_in_range_from_zero_to_one():
    y_one = 1
    x_one= np.linspace(0,1,10)
    z_values = []
    for i in x_one:
        z_values.append(u(i,y_one))
    plt.plot(x_one,z_values,label = 'y = 1')
    print('The boundary condition for u(x,1) = x**4-6*x**2*+1 on the range 0<=x<=1')
    plt.xlabel('x-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
y_one_x_in_range_from_zero_to_one()

def x_one_y_in_range_from_zero_to_one():
    y_one = np.linspace(0,1,10)
    x_one= 1
    z_values = []
    for i in y_one:
        z_values.append(u(x_one,i))
    plt.plot(y_one,z_values,label = 'x = 1')
    print('The boundary condition for u(1,y) = x**4-6*x**2*+1 on the range 0<=y<=1')
    plt.xlabel('y-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
x_one_y_in_range_from_zero_to_one()

def x_zero_y_in_range_from_zero_to_one():
    y_one = np.linspace(0,1,10)
    x_one= 0
    z_values = []
    for i in y_one:
        z_values.append(u(x_one,i))
    plt.plot(y_one,z_values, label = 'x = 0')
    print('The boundary condition for u(0,y) = x**4 on the range 0<=y<=1')
    plt.xlabel('y-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
x_zero_y_in_range_from_zero_to_one()

def e():
    x_value = np.linspace(0,1,100)
    y_value = []
    for i in x_value:
        y_value.append(i**4-6*i**2+1)
    plt.plot(x_value,y_value, label = 'e^-x')
    plt.xlabel('x_axis')
    plt.ylabel('y_axis')
    return plt.show()
```

### Part c)

Give the boundary conditions fulfilled by $u(x,y)$ on the boundaries of the square given by $0\le x\le1$ and $0\le y\le 1$.




```python
%run scripts/show_solutions.py W01-ex1_partc
```

## Exercise 2

Consider the the function
$$
u(x,y) = e^{-x} \sin(y)
$$

### Part a)

Verify that $u(x,y)$ satisfies the Laplace equation.




```python
import sympy as sp
x,y = sp.symbols('x y')
u = sp.exp(-x)*sp.sin(y)
dx = sp.diff(u,x)
dx2 = sp.diff(dx,x)
dy = sp.diff(u,y)
dy2 = sp.diff(dy,y)
print(f'{dx2}  and {dy2}')
if dx2+dy2 ==0:
    print(f'The function {u} satisfies the elliptical Laplacian Partial Differential Equation')
```

    exp(-x)*sin(y)  and -exp(-x)*sin(y)
    The function exp(-x)*sin(y) satisfies the elliptical Laplacian Partial Differential Equation


### Part b)

Generate a surface plot of $u(x,y)$ in the square given by $0\le x\le1$ and $0\le y\le \pi$.


```python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def u(x,y):
    return np.exp(-x)*np.sin(y)

values_x = np.linspace(0,1,100)
values_y = np.linspace(0,1,100)
X,Y = np.meshgrid(values_x,values_y)
Z = u(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
#ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.plot_surface(X, Y, Z,cmap = cm.coolwarm,linewidth=0, antialiased=False)


plt.show()

def y_zero_x_in_range_from_zero_to_one():
    y_zero = 0
    x_zero = np.linspace(0,1,10)
    z_values = []
    for i in x_zero:
        z_values.append(u(i,y_zero))
    plt.plot(x_zero,z_values, label = 'y = 0')
    print('The boundary condition for u(x,0) = 0 on the range 0<=x<=1')
    plt.xlabel('x-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
y_zero_x_in_range_from_zero_to_one()

def y_one_x_in_range_from_zero_to_one():
    y_one = 1
    x_one= np.linspace(0,1,10)
    z_values = []
    for i in x_one:
        z_values.append(u(i,y_one))
    plt.plot(x_one,z_values,label = 'y = 1')
    print('The boundary condition for u(x,1) = exp(-x)*sin(1) on the range 0<=x<=1')
    plt.xlabel('x-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
y_one_x_in_range_from_zero_to_one()

def x_one_y_in_range_from_zero_to_one():
    y_one = np.linspace(0,1,10)
    x_one= 1
    z_values = []
    for i in y_one:
        z_values.append(u(x_one,i))
    plt.plot(y_one,z_values,label = 'x = 1')
    print('The boundary condition for u(1,y) = exp(-1)*sin(x) on the range 0<=y<=1')
    plt.xlabel('y-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
x_one_y_in_range_from_zero_to_one()

def x_zero_y_in_range_from_zero_to_one():
    y_one = np.linspace(0,1,10)
    x_one= 0
    z_values = []
    for i in y_one:
        z_values.append(u(x_one,i))
    plt.plot(y_one,z_values, label = 'x = 0')
    print('The boundary condition for u(0,y) = sin(x) on the range 0<=y<=1')
    plt.xlabel('y-axis')
    plt.ylabel('z_axis')
    plt.legend()
    return plt.show()
x_zero_y_in_range_from_zero_to_one()

def e():
    x_value = np.linspace(0,1,100)
    y_value = []
    for i in x_value:
        y_value.append(np.exp(-i)*np.sin(1))
    plt.plot(x_value,y_value, label = 'e^-x')
    plt.xlabel('x_axis')
    plt.ylabel('y_axis')
    return plt.show()

```


```python
%run scripts/show_solutions.py W01-ex2_partb
```

### Part c)

Give the boundary conditions fulfilled by $u(x,y)$ on the boundaries of the square given by $0\le x\le1$ and $0\le y\le \pi$.


```python

```




```python

```


```python
%run scripts/show_solutions.py W01-ex2_partc
```


## Exercise 3

Show that the function
$$
u(x,t) = e^{-kt} \cos(mx) \cos(nt)
$$
is a solution to the partial differential equation
$$
c^2 \pdderiv{u}{x} = \pdderiv{u}{t} + 2k \pderiv{u}{t}
$$
if the constants with parameters $k$, $m$, $n$ and $c$ are related by the equation
$$
n^2 + k^2 = c^2 m^2
$$






```python
import math
import numpy as np
import matplotlib.pyplot as plt
x,t,k,n,m,c = sp.symbols('x t k n m c')
u = sp.exp(-k*t)*sp.cos(m*x)*sp.cos(n*t)
dx = sp.diff(u,x)
dx2 = sp.diff(dx,x)
dt = sp.diff(u,t)
dt2 = sp.diff(dy,t)
#print(f'{dt2}  and {dt} and {dx2}')

print( dt2 +2*k*dt)
print(c**2*dx2)

```

    2*k*(-k*exp(-k*t)*cos(m*x)*cos(n*t) - n*exp(-k*t)*sin(n*t)*cos(m*x))
    -c**2*m**2*exp(-k*t)*cos(m*x)*cos(n*t)



```python
%run scripts/show_solutions.py W01-ex3
```

146ef0724c9d19e03fba1bf33083d80a63e08167
ipynb
Jupyter Notebook
laboratories/W01_PDE3_Elliptic_PDEs.ipynb
oliver779/PDE3
cdca09d610478573c1dae498196f299ebd39a082
CC0-1.0
laboratories/W01_PDE3_Elliptic_PDEs.ipynb
oliver779/PDE3
cdca09d610478573c1dae498196f299ebd39a082
CC0-1.0
laboratories/W01_PDE3_Elliptic_PDEs.ipynb
oliver779/PDE3
cdca09d610478573c1dae498196f299ebd39a082
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES