### LEON MANGALISO SIHLONGONYANE

## Question 01
Ordinary Differential Equations:
***

* Function: $y = f(x)$

* Order $n$: $y^{(n)} = \frac{d^{n}f(x)}{dx^{n}}$

* Homogeinity: $\alpha \frac{d^{n}f(x)}{dx^{n}}+ \beta \frac{d^{n-1}f(x)}{dx^{n-1}} + ... + f(x) =k= 0$ if $k \neq 0$ there is no homogeinity.

* General solution: $y(x)$ from $\alpha\,r^{n}+\beta\,r^{n-1}+...+1 = 0$.
***

* For $2^{nd}$ order, we may have general solution as $y(x)= k_1\,e^{r_1\,x}+k_2\,e^{r_2\,x}$ were $a\,r^2+b\,r+1 = 0$ and $r= \frac{-b \pm \sqrt{b^2-4\,a\,c}}{2\,a}$ with $a=\alpha$ and $b=\beta$

* Example:$ 2\frac{d^{2}f(x)}{dx^{2}}+ 3 \frac{df(x)}{dx} + f(x) = 2y^{(2)}+3y^{(1)}+1 =0$. So we $2\,r^2+3\,r+1 = 0$ and $a=2\,,b=3$.
* Solution: $y(x) = k_1\,e^{-0.5\,x}+k_2\,e^{-x}$ since $r_1 = -0.5$ and $r_2 = -1$.

One find coefficients in the general solution by applying initial conditions.

We use SageMath examples

```python
x=var('x')  #making variable x
f = function('f')(x) #function that takes the varaible
de=f + 3*diff(f,x)+2*diff(f,x,2)==0
solve_ode = desolve(de,f)   #solving the ode
show(solve_ode) #shows general solution
```

```python
show(f + 3*diff(f,x)+2*diff(f,x,2)==0)  #displaying ODE
```

```python
a=desolve(de,f,show_method=True) #test:linearity, separable, constant coeffients, exact, so on.
a
```

    [_K1*e^(-1/2*x) + _K2*e^(-x), 'constcoeff']

```python
solve_ode=desolve(de,f,[0,3,1],show_method=False) # initial conditions to find particular solution.
solve_ode
```

    8*e^(-1/2*x) - 5*e^(-x)

```python
plot(solve_ode, (x,0,5)) #plotting solution of ODE.
```

```python

```

## Question 02

**Goal:** To prove that $a(a+1)(2a+1)$ is always divisible by $6$.
***
* For $P(a)$: we pick $a=1$: $$a(a+1)(2a+1)=1(1+1)(2(1)+1)=6$$ Clearly $6$ divides $6$.

* Assume true for $P(a=k)$: $$k(k+1)(2k+1)= 6q$$ for $q \in \mathbb{Z}$

* Proof for $P(a=k+1)$:
\begin{align}
(k+1)(k+2)(2k+3) &=& 2\,k^3 + 9\,k^2+13\,k + 6 \\
LHS&=& 6\,q -3\,k^2-k +9\,k^2 + 6 \\
\end{align}

Were $2k^3=6\,q-3\,k^2-k$ from $n=k$

$\therefore$ LHS$= 6\,q + 6\,k^2 +12\,k +6= 6(q+k^2+2\,k + 1)=6\,Z$,were $Z \in \mathbb{Z}$. Hence, $6$ divides $a(a+1)(2a+1)$

## Question 03

```python
b= animate([plot(lambda x:sin(x**2 +3*x -2)**2 -a*cos(x) ,x,-2,2,color='red') for a in sxrange(0,1,.2) ])
c=animate([plot(lambda x: x^x -x+c,x,-2,2,color='green') for c in sxrange(0,1,.2)])
lattice= [ [i,j] for i in [-2..2] for j in [-2..2] ]
lpoint= points(lattice, rgbcolor = (0, 0, 0), pointsize = 20)
```

    verbose 0 (3835: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 99 points.
    verbose 0 (3835: plot.py, generate_plot_points) Last error message: 'can't convert complex to float'
    verbose 0 (3835: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 99 points.
    verbose 0 (3835: plot.py, generate_plot_points) Last error message: 'can't convert complex to float'
    verbose 0 (3835: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 99 points.
    verbose 0 (3835: plot.py, generate_plot_points) Last error message: 'can't convert complex to float'
    verbose 0 (3835: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 99 points.
    verbose 0 (3835: plot.py, generate_plot_points) Last error message: 'can't convert complex to float'
    verbose 0 (3835: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 99 points.
    verbose 0 (3835: plot.py, generate_plot_points) Last error message: 'can't convert complex to float'

```python
def r(c):
    lattice= [ [i*c+1,j*c+1] for i in [-2..2] for j in [-2..2] ]
    return plot(lambda x:sin(x**2 +3*x -2)**2 -c*cos(x) ,x,-2,2,color='red') + plot(lambda x: x^x -x+c,x,-2,2,color='green') + points(lattice, rgbcolor = (0, 0, 0), pointsize = 20)
animate(r(c)  for c in sxrange(0,1,.2))
```

## Question 4.

```python
@interact
def _(R=slider(2,3,5)): #primes
    var('y')
    bound= 25
    P = implicit_plot( x^2 + y^2 == R^2, (x, -5,-3,2,3,5), (y, -5,-3,-2,2,3, 5 ))
    lattice_pts = [ [i,j] for i in [floor(-sqrt(bound))..ceil(sqrt(bound))] for j in [floor(-sqrt(bound))..ceil(sqrt(bound))] ]
    plot_lattice_pts = points(lattice_pts, rgbcolor = (0, 0, 0), pointsize = 10)
    show(P+plot_lattice_pts)
```

## Question 05

```python
def sq_sum(num):  #DEFINING FUNCTION AS DICTIOANRY
    qsum = {}
    for a in [0..num]:
        for b in [0..num]:
            m = a^2 + b^2
            if m>0:
                if m in qsum:
                    qsum[m].append((a,b))
                else:
                    qsum.update({m:[(a,b),(b,a)]})
    return qsum
```

```python
sq_sum(5)
```

    {1: [(0, 1), (1, 0), (1, 0)],
     4: [(0, 2), (2, 0), (2, 0)],
     9: [(0, 3), (3, 0), (3, 0)],
     16: [(0, 4), (4, 0), (4, 0)],
     25: [(0, 5), (5, 0), (3, 4), (4, 3), (5, 0)],
     2: [(1, 1), (1, 1)],
     5: [(1, 2), (2, 1), (2, 1)],
     10: [(1, 3), (3, 1), (3, 1)],
     17: [(1, 4), (4, 1), (4, 1)],
     26: [(1, 5), (5, 1), (5, 1)],
     8: [(2, 2), (2, 2)],
     13: [(2, 3), (3, 2), (3, 2)],
     20: [(2, 4), (4, 2), (4, 2)],
     29: [(2, 5), (5, 2), (5, 2)],
     18: [(3, 3), (3, 3)],
     34: [(3, 5), (5, 3), (5, 3)],
     32: [(4, 4), (4, 4)],
     41: [(4, 5), (5, 4), (5, 4)],
     50: [(5, 5), (5, 5)]}

```python
qEven={m:qsum[m] for m in qsum if is_even(m)}
qOdd={m:qsum[m] for m in qsum if is_odd(m)}
qOdd
```

    {1: [(0, 1), (1, 0), (1, 0)],
     9: [(0, 3), (3, 0), (3, 0)],
     25: [(0, 5), (5, 0), (3, 4), (4, 3), (5, 0)],
     5: [(1, 2), (2, 1), (2, 1)],
     17: [(1, 4), (4, 1), (4, 1)],
     13: [(2, 3), (3, 2), (3, 2)],
     29: [(2, 5), (5, 2), (5, 2)],
     41: [(4, 5), (5, 4), (5, 4)]}

```python
qEven
```

    {4: [(0, 2), (2, 0), (2, 0)],
     16: [(0, 4), (4, 0), (4, 0)],
     2: [(1, 1), (1, 1)],
     10: [(1, 3), (3, 1), (3, 1)],
     26: [(1, 5), (5, 1), (5, 1)],
     8: [(2, 2), (2, 2)],
     20: [(2, 4), (4, 2), (4, 2)],
     18: [(3, 3), (3, 3)],
     34: [(3, 5), (5, 3), (5, 3)],
     32: [(4, 4), (4, 4)],
     50: [(5, 5), (5, 5)]}

```python
table([list(qEven.keys()),list(qOdd.keys())],header_column=[r'$Even$',r"$Odd$"]).transpose()
```

```python
sq_prime_test=lambda num: {i^2+j^2: is_prime(i^2+j^2) for i in sq_sum(num) for j in qsum if is_prime(i) and is_prime(j)}
```

```python
sq_prime_test(5)  #test if sum of prime a prime. It is not always true, as shown by the examples of 'false'.
```

    {8: False,
     29: True,
     293: True,
     173: True,
     845: False,
     1685: False,
     50: False,
     314: False,
     194: False,
     866: False,
     1706: False,
     578: False,
     458: False,
     1130: False,
     1970: False,
     338: False,
     1010: False,
     1850: False,
     1682: False,
     2522: False,
     3362: False}

```python
sq_prime_db={i^2+j^2:(i,j) for i in qsum for j in qsum if is_prime(i^2+j^2)}  #dataBase for sum of primes. It is not clear since they can take any numbers of even or odd.
sq_prime_db
```

    {2: (1, 1),
     17: (4, 1),
     257: (16, 1),
     5: (2, 1),
     101: (10, 1),
     677: (26, 1),
     401: (20, 1),
     97: (9, 4),
     641: (25, 4),
     41: (5, 4),
     857: (29, 4),
     1697: (41, 4),
     337: (16, 9),
     181: (10, 9),
     757: (26, 9),
     1237: (34, 9),
     881: (25, 16),
     281: (5, 16),
     1097: (29, 16),
     1301: (26, 25),
     29: (5, 2),
     293: (17, 2),
     173: (13, 2),
     701: (26, 5),
     89: (8, 5),
     349: (18, 5),
     1181: (34, 5),
     1049: (32, 5),
     389: (17, 10),
     269: (13, 10),
     941: (29, 10),
     353: (8, 17),
     613: (18, 17),
     2789: (50, 17),
     2357: (41, 26),
     233: (13, 8),
     569: (20, 13),
     1193: (32, 13),
     2081: (41, 20),
     1997: (34, 29),
     2837: (41, 34)}

```python
table([list(sq_prime_db.keys()),list(sq_prime_db.values())],header_column=[r'$primeKeys$',r"$sumValues$"]).transpose()
```

```python
r3_mod_3={2*i:mod(2*i,3) for i in qsum.keys() if (mod(2*i,3) in qsum)}   #even modulo 3
is_3_mod_3=set(r3_mod_3.items())
```

```python
odd_mod_3={2*i+1:mod(2*i+1,3) for i in qsum.keys() if (mod(2*i+1,3) in qsum)}   #Odd modulo 3
set_odd_mod_3=set(odd_mod_3.items())
```

```python
table([list(r3_mod_4.items()),list(odd_mod_4.items())],header_column=[r'$even~sum,mod4$',r"$odd~sum, mod4$"]).transpose()
```

## Question 06

**1.**

**Goal:** Given $x,y \in \mathbb{R}$ and $3\,x +2\,y \leq 5$. Show that if $x > 1$, then $y < 1$.
***
**By direct proof.**

We start with $3\,x +2\,y \leq 5\,$, then we get $$x \leq \frac{5-2\,y}{3}$$.

But since we given $x > 1$, then $$ 1 < x \leq \frac{5-2\,y}{3}$$
This implies $0 < 1-y$. Finaly, $y < 1$.
***

**2.**

**Goal:** Given $x \in \mathbb{R}$ and $ x \neq 4$. Show that if $\frac {2\,x-5}{x-4}=3$ then $x = 7$.
***
**(a)**

*  P:   $\frac {2\,x-5}{x-4}$ is equal to $3$
*  Q:    $x$ is equal to $7$.

***

$P \implies Q$ not same as $Q \implies P$, i.e, the implication is not true, mathematical. We cannot start with $x=7$ true to prove the first part of the proposition is true,i.e $\frac {2\,x-5}{x-4}=3$. Therefore, the proof method was wrong.

**(b)**

**By direct proof.**

We start with $\frac {2\,x-5}{x-4} = 3$ provided that $x \neq 4$ to avoid dividing by zero. We compute as follows $$(2\,x-5)=3(x-4)=3\,x-12 \implies 12-5=3\,x - 2\,x \implies 7 = x$$

**3.**

**Goal:** Given $x,y \in \mathbb{R}$ and $ x \neq 3$. Show that if $ x^2\,y = 9\,y$ . Then $y =0$.
***
**(a)**

The proof is wrong. The conclusion that  $x \neq 3 \implies x^2 \neq 9$ cannot be right since we can get $(-3)^2-9=0$ for $x=-3 \neq 3$. So the next step, would not allow, i.e dividing $x^2-9$ both sides of $(x^2-9)y=0$.

**Proof.**

We start $(x^2-9)\,y = 0$. Since $x \neq 3$

**goal:** given $x \neq 3$. if w start with $x^2\,y=9\,y$, then we end up with $y=0$.

For this case, we can use examples to argue. Non trivial solutions of $x^2\,y=9\,y$ are $x=3$ or $x=-3$. But $x \neq 3$. We pick $x=-3 \neq 3$, then $((-3)^2-9)y=(9-9)y=0\,y=0$ this implies $y=0$. We done.

## Question 07

```python
sig=lambda i:sum(divisors(i))   #defining function of divisors
sig_list=lambda j: {j: sig(j) for j in [1..j]}
sig_db_10=tu_list(10)  #database for 10. sum divisors of 10.
```

```python
sig_db_10
```

    {1: 1, 2: 3, 3: 4, 4: 7, 5: 6, 6: 12, 7: 8, 8: 15, 9: 13, 10: 18}

```python
def sigma_n(bound=10):  #defining function of sum divisors'
    sig_db_10=sig_list(bound)
    list1=[i for i in sig_db if sig_db_10[i]==2*i]  #creates list if even
    for k in list1:
        dv=divisors(k)
        list2=0
    for num in dv:
        list2 +=1/num
    return num, list2
```

```python
sigma_n()  # gives n=even and reciprocal is 2.
```

    (6, 2)

```python
sum(divisors(10))
```

    18

```python
#list(sig_db_10.items())
```

```python
table([list(sig_db_10.keys()),list(sig_db_10.values()), sigma_n()],header_column=[r'$n$',r"$sum$",r" sum&reciprocal"])
```

 sum&reciprocal

```python

```


<figure>
  <IMG SRC="gfx/Logo_norsk_pos.png" WIDTH=100 ALIGN="right">
</figure>

# Operators and commutators
*Roberto Di Remigio*, *Luca Frediani*

We will be exercising our knowledge of operators and commutator algebra. These are extremely useful exercises, as you
will see these type of manipulations recurring throughout the rest of the course.
A note on notation:

* an operator will be designed by putting an hat on top of any letter:
 \begin{equation}
  \hat{A},\,\hat{O},\,\hat{b},\,\hat{\gamma}
 \end{equation}
* the commutator of two operators is defined as:
 \begin{equation}
  [\hat{A}, \hat{B}] = \hat{A}\hat{B} - \hat{B}\hat{A}
 \end{equation}
* the position and momentum operator are defined as:
 \begin{equation}
  \hat{x}_i = x_i\cdot \quad\quad \hat{p}_i = -\mathrm{i}\hbar\frac{\partial}{\partial x_i}
 \end{equation}
 where $i$ refers to any of the three Cartesian components, i.e. $i = x, y, z$
* the Canonical Commutation Relations (CCR) are:
 \begin{alignat}{3}
  [x_i, x_j] = 0; \quad& [p_i, p_j] = 0; \quad& [x_i, p_j] = \mathrm{i}\hbar \delta_{ij}
 \end{alignat}
 where the _Kronecker $\delta$ symbol_ is defined as:
 \begin{equation}
  \delta_{ij} =
  \begin{cases}
   1 & \text{if } i = j \\
   0 & \text{if } i \neq j
  \end{cases}
 \end{equation}
* Dirac braket notation. We will interpret the following symbols as:
 \begin{equation}
  \langle \psi | \phi \rangle = \int \mathrm{d} \mathbf{r} \psi^*(\mathbf{r})\phi(\mathbf{r})
 \end{equation}
 \begin{equation}
  \langle \psi | \hat{A} | \phi \rangle = \int\mathrm{d} \mathbf{r} \psi^*(\mathbf{r})\hat{A}\phi(\mathbf{r})
 \end{equation}

## Using SymPy

[SymPy] is a Python library for symbolic mathematics. It can be used to evaluate derivatives, definite and indefinite integrals, differential equations and much more.
As an example, the following code will evaluate the derivative of $\exp(x^2)$ and print it to screen:

```Python
from sympy import *
x, y, z = symbols('x y z')
init_printing(use_unicode=True)
diff(exp(x**2), x)
```

[SymPy]: http://www.sympy.org/en/index.html

```python
from sympy import *
# Define symbols
x, y, z = symbols('x y z')
# We want results to be printed to screen
init_printing(use_unicode=True)
# Calculate the derivative with respect to x
diff(exp(x**2), x)
```

There is an extensive [tutorial] that you can refer to. Another useful example is the calculation
of definite and indefinite integrals using SymPy. Consider the following code snippet:
```Python
# An indefinite integral
integrate(cos(x), x)
```
This will calculate the _primitive_ function of $\cos(x)$:
\begin{equation}
\int \cos(x)\mathrm{d}x = \sin(x) + C
\end{equation}

[tutorial]: http://docs.sympy.org/latest/tutorial/index.html

```python
integrate(cos(x), x)
```

This code snippet will instead calculate the definite integral of the same function
in a given interval:
\begin{equation}
\int_{-\pi/2}^{\pi/2} \cos(x)\mathrm{d}x =[\sin(x)]_{-\pi/2}^{\pi/2} = 2
\end{equation}
```Python
# A definite integral
integrate(cos(x), (x, -\pi/2., pi/2))
```

```python
integrate(cos(x), (x, -pi/2., pi/2.))
```

SymPy is quite powerful. It can handle expression with multiple variables and be used to simplify complicated expressions. You are encouraged to experiment with SymPy whenever needed in the following exercises.

## Exercise 1: The importance of commuting

 Let us have two operators $\hat{A}$ and $\hat{B}$. Further assume that their commutator
 is known to be: $[\hat{A}, \hat{B}] = c$, where $c$ is a scalar (a complex number in
 the general case).
 Is the following true?
 \begin{equation}
  \langle \psi| \hat{A}\hat{B} |\phi \rangle = \langle \psi| \hat{B}\hat{A} | \phi \rangle
 \end{equation}
 To convince yourself, try to calculate:
 \begin{equation}
  \langle \sin(x) |\hat{x}\hat{p}_x | \cos(x) \rangle; \quad\quad \langle \sin(x) |\hat{p}_x\hat{x} | \cos(x) \rangle
 \end{equation}

## Exercise 2: Commutator identities

 Prove the following commutator identities:
  \begin{align}
  &[\hat{A}, \hat{A}] = 0 \\
  &[\hat{A}, \hat{B}] = - [\hat{B}, \hat{A}] \\
  &[\hat{A}+\hat{B}, \hat{C}] = [\hat{A}, \hat{C}] + [\hat{B}, \hat{C}] \\
  &[\hat{A}, \hat{B}\hat{C}] = [\hat{A}, \hat{B}]\hat{C} + \hat{B}[\hat{A},\hat{C}] \\
  &[\hat{A}, [\hat{B}, \hat{C}]] + [\hat{B}, [\hat{C}, \hat{A}]] + [\hat{C}, [\hat{A}, \hat{B}]] = 0
  \end{align}
The last one is known as _Jacobi identity_.

## Exercise 3: Some more commutators

 Having proved the commutator identities, calculate the following commutators:
 \begin{align}
  &[\hat{p}_x, \hat{x}^2] \\
  &[\hat{y}\hat{p}_z - \hat{z}\hat{p}_y, \hat{z}\hat{p}_x - \hat{x}\hat{p}_z] \\
  &[\hat{a}, \hat{a}^\dagger]
 \end{align}
 where:
 \begin{alignat}{2}
  \hat{a} = \frac{\hat{x} + \mathrm{i}\hat{p}_x}{\sqrt{2}}; \quad & \quad \hat{a}^\dagger = \frac{\hat{x} - \mathrm{i}\hat{p}_x}{\sqrt{2}}
 \end{alignat}

## Exercise 4: Normalization

 In quantum mechanics, physical states are represented by mathematical objects called _wavefunctions_. Wavefunctions are
 functions of the coordinates: $\psi(\mathbf{r})$. Not all functions can aspire to become wavefunctions. As wavefunctions represent
 _probability densities_, a very important requirement they must satisfy is to be _normalizable_:
 \begin{equation}
  \langle \psi|\psi\rangle = \int \mathrm{d} \mathbf{r} \psi^*(\mathbf{r}) \psi(\mathbf{r}) < \infty
 \end{equation}
 i.e. the integral above must be _finite_. Notice that the property of normalizability depends on the _domain_ of the function
 and the limits of the integration above.
 Are the following functions normalizable?
 \begin{align}
  \psi(x) &= e^{-\frac{x^2}{2}} \quad x\in[-\infty, +\infty] \\
  \psi(x) &= e^{-x} \quad x\in[0, +\infty] \\
  \psi(x) &= e^{-x} \quad x\in[-\infty, +\infty] \\
  \psi(x) &= e^{\mathrm{i}x} \quad x\in[-\infty, +\infty] \\
  \psi(x) &= e^{\mathrm{i}x} \quad x\in[-\pi, +\pi]
 \end{align}

## Exercise 5: Self-adjointedness 1

 For any operator on our vector space of functions, we can define its _adjoint_ operator (also called _Hermitian conjugate_).
 Given the operator $\hat{A}$, the operator $\hat{A}^\dagger$ is its adjoint if and only if, for any pair of vectors $\psi, \phi$ the following
 holds true:
 \begin{equation}
  \langle \psi | \hat{A} | \phi \rangle^* = \langle \phi | \hat{A}^\dagger | \psi \rangle
 \end{equation}
 Of all the operators that can exist, a class is particularly interesting in quantum mechanics: the _self-adjoint_ operators.
 An operator $\hat{A}$ is said to be self-adjoint if and only if, for any pair of vectors $\psi, \phi$ we have:
 \begin{equation}
  \langle \psi | \hat{A} | \phi \rangle^* = \langle \phi | \hat{A} | \psi \rangle
 \end{equation}
 that is to say: $\hat{A} = \hat{A}^\dagger$. Why are self-adjoint operators so important? Because they have a series of important
 properties that make them useful in representing physical observables, such as position, momentum, energy etc.

 In this exercise, we will prove that the momentum operator is self-adjoint. To simplify the matter, we will just prove it for $\hat{p}_x$.
 First let's write down explicitly $\langle \psi | \hat{p}_x | \phi \rangle$:
 \begin{equation}
  \int \mathrm{d} x \psi^*(x)\left[-\mathrm{i}\hbar \frac{\partial}{\partial x} \phi(x)\right]
 \end{equation}
 We then take the complex conjugate:
 \begin{equation}
  \int \mathrm{d} x \psi(x)\left[\mathrm{i}\hbar \frac{\partial}{\partial x} \phi^*(x)\right]
 \end{equation}
 and this need to be equal to:
 \begin{equation}
 \int \mathrm{d} x \phi^*(x)\left[-\mathrm{i}\hbar \frac{\partial}{\partial x} \psi(x)\right]
 \end{equation}
 Is the operator $-\hbar\frac{\partial}{\partial x}$ self-adjoint?

 **Warning** You can't just move $\phi^*(x)$ to the left!! The momentum operator is a derivative with respect to $x$!

 **Hint 1** Use integration by parts on the last expression. Remember that we are integrating on the whole set of real numbers i.e. $\int$ means $\int_{-\infty}^{+\infty}$.

 **Hint 2** The functions in our vector space go to zero at infinity, so that $[\psi(x)\phi^*(x)]_{-\infty}^{+\infty}= 0$

## Exercise 6: Self-adjointedness 2

 Now that you know the tricks of the trade, which of the following operators are self-adjoint?
 \begin{align}
  & \hat{p}_x^2 = -\hbar^2 \frac{\partial^2}{\partial x^2} \\
  & \hat{l}_x = \hat{y}\hat{p}_z - \hat{z}\hat{p}_y \\
  & \hat{a} = \frac{\hat{x} + \mathrm{i}\hat{p}_x}{\sqrt{2}} \\
  & \hat{a}^\dagger =  \frac{\hat{x} - \mathrm{i}\hat{p}_x}{\sqrt{2}}
 \end{align}

```python

```


The integrals in nonlinear finite element equations are computed
by numerical integration rules in computer programs, so the
formulas for the variational form is directly transferred to numbers.
mathcal{I}_t is of interest to understand the nature of the system of difference equations
that arises from the finite element method in nonlinear problems and
to compare with corresponding expressions arising from finite difference
discretization. We shall dive into this problem here. To see the
structure of the difference equations implied by the finite element method,
we have to find symbolic expressions for the integrals, and this is
extremely difficult since the integrals involve the unknown function
in nonlinear problems. However, there are some techniques that allow
us to approximate the integrals and work out symbolic formulas that
can compared with their finite difference counterparts.

We shall address the 1D model problem ([nonlin:alglevel:1D:pde](#nonlin:alglevel:1D:pde)) from
the beginning of the section [nonlin:alglevel:1D](#nonlin:alglevel:1D).
The finite difference discretization is shown in the section [nonlin:alglevel:1D:fd](#nonlin:alglevel:1D:fd), while the variational form based on
Galerkin's method is developed in the section [nonlin:alglevel:1D:fe](#nonlin:alglevel:1D:fe).
We build directly on formulas developed in the latter section.

## Finite element basis functions

Introduction of finite element basis functions $\basphi_i$ means setting

$$
\baspsi_i = \basphi_{\nu(i)},\quad i\in\If,
$$

where degree of freedom number $\nu(i)$ in the mesh corresponds to
unknown number $i$ ($c_i$).
In the present example, we use all the basis functions except the
last at $i=N_n-1$, i.e., $\If = \{0,\ldots,N_n-2\}$, and $\nu(j)=j$.
The expansion of $u$ can be taken as

$$
u = D + \sum_{j\in\If} c_j\basphi_{\nu(j)},
$$

but it is more common in a finite element context to use a boundary
function $B=\sum_{j\in\Ifb}U_j\basphi_j$, where $U_j$ are prescribed
Dirichlet conditions for degree of freedom number $j$ and $U_j$ is
the corresponding value.

$$
u = D\basphi_{N_n-1} + \sum_{j\in\If} c_j\basphi_{\nu(j)}\thinspace .
$$

In the general case with $u$ prescribed as $U_j$ at some nodes $j\in\Ifb$,
we set

$$
u = \sum_{j\in\Ifb} U_j\basphi_j +
\sum_{j\in\If}c_j\basphi_{\nu(j)},
$$

where $c_j = u(x^{\nu(j)})$. That is, $\nu(j)$ maps unknown number $j$ to
the corresponding node number $\nu(j)$ such that $c_j = u(x^{\nu(j)})$.

## The group finite element method

### Finite element approximation of functions of $u$

Since we already expand $u$ as $\sum_j\basphi_j u(\xno{j})$, we may use the
same approximation for other functions as well. For example,

$$
f(u)\approx \sum_{j} f(\xno{j})\basphi_j,
$$

where $f(\xno{j})$ is the value of $f$ at node $j$. Since $f$ is a function
of $u$, $f(\xno{j})=f(u(\xno{j}))$. Introducing
$u_j$ as a short form for $u(\xno{j})$, we can write

$$
f(u)\approx \sum_{j} f(u_{j})\basphi_j\thinspace .
$$

This approximation is known as the *group finite element method*
or the *product approximation* technique. The index $j$ runs over
all node numbers in the mesh.

The principal advantages of the group finite element method are two-fold:

1. Complicated nonlinear expressions can be simplified to increase
   the efficiency of numerical computations.

2. One can derive *symbolic forms* of the difference equations arising
   from the finite element method in nonlinear problems.
   The symbolic form is useful for comparing finite element and finite
   difference equations of nonlinear differential equation problems.

Below, we shall explore point 2 to see exactly how the finite element
method creates more complex expressions in the resulting
linear system (the difference equations) that the finite difference
method does. mathcal{I}_t turns out that is very difficult to see
what kind of terms in the difference equations that arise from
$\int f(u)\basphi_i\dx$ without using the group finite element
method or numerical integration utilizing the nodes only.

Note, however, that an expression like $\int f(u)\basphi_i\dx$ causes
no problems in a computer program as the integral is calculated by
numerical integration using an existing approximation of $u$ in
$f(u)$ such that the integrand can be sampled at any spatial point.

### Simplified problem

Our aim now is to derive symbolic expressions for the difference
equations arising from the finite element method in nonlinear
problems and compare the expressions with those arising in the
finite difference method. To this end,
let us simplify the model problem and set $a=0$, $\dfc=1$,
$f(u)=u^2$, and have Neumann conditions at both ends such that we
get a very simple nonlinear problem $-u^{\prime\prime}=u^2$,
$u'(0)=1$, $u'(L)=0$.
The variational form is then

$$
\int_0^L u^{\prime}v^{\prime}\dx = \int_0^L u^2v\dx
- v(0),\quad\forall v\in V\thinspace .
$$

The term with $u^{\prime}v^{\prime}$ is well known so the only new feature is
the term $\int u^2v\dx$.

To make the distance from finite element equations to finite
difference equations as short as possible, we shall substitute $c_j$
in the sum $u=\sum_jc_j\basphi_j$ by $u_j=u(\xno{j})$ since $c_j$ is
the value of $u$ at node $j$. (In the more general case
with Dirichlet conditions as well, we have a sum
$\sum_jc_j\basphi_{\nu(j)}$ where $c_j$ is replaced by $u(\xno{\nu(j)})$.
We can then introduce some other counter $k$ such that it is
meaningful to write $u=\sum_k u_k\basphi_k$, where $k$ runs over appropriate
node numbers.)
The quantity $u_j$ in $\sum_ju_j\basphi_j$
is the same as $u$ at mesh point number $j$
in the finite difference method, which is commonly denoted $u_j$.

### Integrating nonlinear functions

Consider the term $\int u^2v\dx$ in the variational formulation
with $v=\basphi_i$ and $u=\sum_k\basphi_ku_k$:

$$
\int_0^L (\sum_ku_k\basphi_k)^2\basphi_i\dx\thinspace .
$$

Evaluating this integral for P1 elements (see [nonlin:exer:fu:fem:int](#nonlin:exer:fu:fem:int)) results in the expression

$$
\frac{h}{12}(u_{i-1}^2 + 2u_i(u_{i-1} + u_{i+1}) + 6u_i^2
+ u_{i+1}^2),
$$

to be compared with the simple value $u_i^2$ that would arise in
a finite difference discretization when $u^2$ is sampled at
mesh point $x_i$. More complicated $f(u)$ functions
in the integral $\int_0^L f(u)\basphi_i\dx$
give rise to much more lengthy expressions, if it is possible to
carry out the integral symbolically at all.

### Application of the group finite element method

Let us use the group finite element method to derive the terms in
the difference equation corresponding to $f(u)$ in the differential
equation. We have

$$
\int_0^L f(u)\basphi_i\dx \approx
\int_0^L (\sum_j \basphi_jf(u_j))\basphi_i\dx
= \sum_j \left(\int_0^L \basphi_i\basphi_j\dx\right) f(u_j)\thinspace .
$$

We recognize this expression as the mass matrix $M$, arising from
$\int\basphi_i\basphi_j\dx$, times the
vector $f=(f(u_0),f(u_1),\ldots,)$: $Mf$. The associated terms
in the difference equations are, for P1 elements,

$$
\frac{h}{6}(f(u_{i-1}) + 4f(u_i) + f(u_{i+1}))\thinspace .
$$

Occasionally, we want to interpret this expression in terms of finite
differences, and to this end a rewrite of this expression is convenient:

$$
\frac{h}{6}(f(u_{i-1}) + 4f(u_i) + f(u_{i+1}))
= h[f(u) - \frac{h^2}{6}D_xD_x f(u)]_i\thinspace .
$$

That is, the finite element treatment of $f(u)$ (when using a group
finite element method) gives the same term as in a finite difference
approach, $f(u_i)$, minus a diffusion term which is the
2nd-order discretization of $\frac{1}{6}h^2f''(x_i)$.

We may lump the mass matrix through integration with the Trapezoidal
rule so that $M$ becomes diagonal in the finite element method.
In that case the $f(u)$ term in the differential equation
gives rise to a single term $hf(u_i)$, just as in the finite difference
method.

## Numerical integration of nonlinear terms by hand

Let us reconsider a term $\int f(u)v\dx$ as treated in the previous
section, but now we want to integrate this term numerically.
Such an approach can lead to easy-to-interpret formulas if we apply
a numerical integration rule that samples the integrand at the node
points $\xno{i}$ only, because at such points, $\basphi_j(\xno{i})=0$
if $j\neq i$, which leads to great simplifications.

The term in question takes the form

$$
\int_0^L f(\sum_k u_k\basphi_k)\basphi_i\dx\thinspace .
$$

Evaluation of the integrand at a node $\xno{\ell}$ leads to a
collapse of the sum $\sum_k u_k\basphi_k$ to one term because

$$
\sum_k u_k\basphi_k(\xno{\ell}) = u_\ell\thinspace .
$$

$$
f(\sum_k u_k\underbrace{\basphi_k(\xno{\ell})}_{\delta_{k\ell}})
\underbrace{\basphi_i(\xno{\ell})}_{\delta_{i\ell}}
= f(u_\ell)\delta_{i\ell},
$$

where we have used the Kronecker delta: $\delta_{ij}=0$ if $i\neq j$ and
$\delta_{ij}=1$ if $i=j$.

Considering the Trapezoidal rule for integration, where the integration
points are the nodes, we have

$$
\int_0^L f(\sum_k u_k\basphi_k(x))\basphi_i(x)\dx
\approx h\sum_{\ell=0}^{N_n} f(u_\ell)\delta_{i\ell} - \mathcal{C}
= hf(u_i)\thinspace .
$$

This is the same representation of the $f$ term as in the finite
difference method.
The term $\mathcal{C}$ contains the evaluations of the integrand
at the ends with weight $\frac{1}{2}$, needed to make a true Trapezoidal rule:

$$
\mathcal{C} = \frac{h}{2}f(u_0)\basphi_i(0) + \frac{h}{2}f(u_{N_n-1})\basphi_i(L)\thinspace .
$$

The answer $hf(u_i)$ must therefore be multiplied by $\frac{1}{2}$ if
$i=0$ or $i=N_n-1$. Note that $\mathcal{C}=0$ for $i=1,\ldots,N_n-2$.

One can alternatively use the Trapezoidal rule on the reference cell and
assemble the contributions. mathcal{I}_t is a bit more labor in this context,
but working on the reference cell is safer as that approach is
guaranteed to handle discontinuous derivatives of finite element
functions correctly (not important in this particular example),
while the rule above was derived with the
assumption that $f$ is continuous at the integration points.

The conclusion is that it suffices to use the Trapezoidal rule if
one wants to derive the difference equations in the finite element
method and make them similar to those arising in the finite difference
method. The Trapezoidal rule has sufficient accuracy for P1 elements, but
for P2 elements one should turn to Simpson's rule.

## Finite element discretization of a variable coefficient Laplace term

Turning back to the model problem ([nonlin:alglevel:1D:pde](#nonlin:alglevel:1D:pde)), it
remains to calculate the contribution of the $(\dfc u^{\prime})^{\prime}$
and boundary terms
to the difference equations. The integral in the variational form
corresponding to $(\dfc u^{\prime})^{\prime}$ is

$$
\int_0^L \dfc(\sum_k c_k\baspsi_k)\baspsi_i^{\prime}\baspsi_j^{\prime}\dx\thinspace .
$$

Numerical integration utilizing a value of $\sum_k c_k\baspsi_k$ from
a previous iteration must in general be used to compute the integral.
Now our aim is to integrate symbolically, as much as we can, to obtain
some insight into how the finite element method approximates
this term.
To be able to derive symbolic expressions, we must either turn to
the group finite element method or numerical integration in the
node points. Finite element basis functions $\basphi_i$ are now used.

### Group finite element method

We set $\dfc(u)\approx \sum_k\alpha(u_k)\basphi_k$, and then
we write

$$
\int_0^L \dfc(\sum_k c_k\basphi_k)\basphi_i^{\prime}\basphi_j^{\prime}\dx
\approx
\sum_k (\underbrace{\int_0^L \basphi_k\basphi_i^{\prime}\basphi_j^{\prime}\dx}_{L_{i,j,k}})
\dfc(u_k) = \sum_k L_{i,j,k}\dfc(u_k)\thinspace .
$$

Further calculations are now easiest to carry out in the reference
cell. With P1 elements we can compute
$L_{i,j,k}$ for the two $k$ values that are relevant on the reference
cell. Turning to local indices, one gets

$$
L_{r,s,t}^{(e)} =
\frac{1}{2h}\left(\begin{array}{rr}
1 & -1\\
-1 & 1
\end{array}\right),\quad t=0, 1,
$$

where $r,s,t=0,1$ are indices over local degrees of
freedom in the reference cell
($i=q(e,r)$, $j=q(e,s)$, and $k=q(e,t)$). The
sum $\sum_k L_{i,j,k}\dfc(u_k)$ at the cell level becomes
$\sum_{t=0}^1 L_{r,s,t}^{(e)}\dfc(\tilde u_t)$, where $\tilde u_t$
is $u(\xno{q(e,t)})$, i.e., the value of $u$ at local node number $t$ in
cell number $e$. The element matrix becomes

$$
\begin{equation}
\frac{1}{2} (\dfc(\tilde u_0) + \dfc(\tilde u^{(1)}))
\frac{1}{h}\left(\begin{array}{rr}
1 & -1\\
-1 & 1
\end{array}\right)\thinspace .
\label{nonlin:alglevel:1D:fe:Laplace:Ae} \tag{1}
\end{equation}
$$

As usual, we employ
a left-to-right numbering of cells and nodes.
Row number $i$ in the global matrix gets contributions from
the first row of the element matrix in cell $i$ and the last
row of the element matrix in cell $i-1$.
In cell number $i-1$ the sum
$\dfc(\tilde u_0) + \dfc(\tilde u^{(1)})$ corresponds to
$\dfc(u_{i-1}) + \dfc(u_i)$. The same sum becomes
$\dfc(u_{i}) + \dfc(u_{i+1})$ in cell number $i$.
We can with this insight assemble the contributions to row number $i$
in the global matrix:

$$
\frac{1}{2h}(-(\dfc(u_{i-1}) + \dfc(u_i)),\quad
\dfc(u_{i-1}) + 2\dfc(u_i) + \dfc(u_{i+1}),\quad
\dfc(u_{i}) + \dfc(u_{i+1}))\thinspace .
$$

Multiplying by the vector of unknowns $u_i$ results in a formula that
can be arranged to

$$
\begin{equation}
-\frac{1}{h}(\frac{1}{2}(\dfc(u_i) + \dfc(u_{i+1}))(u_{i+1}-u_i)
-  \frac{1}{2}(\dfc(u_{i-1}) + \dfc(u_{i}))(u_{i}-u_{i-1})),
\label{nonlin:alglevel:1D:fe:Laplace:diffeq} \tag{2}
\end{equation}
$$

which is nothing but the standard finite difference discretization
of $-(\dfc(u)u^{\prime})^{\prime}$ with an arithmetic mean of $\dfc(u)$ (and
the usual factor $h$ because of the integration in the finite element method).

### Numerical integration at the nodes

Instead of using the group finite element method and exact integration
we can turn to the Trapezoidal rule for computing
$\int_0^L \dfc(\sum_k u_k\basphi_k)\basphi_i^{\prime}\basphi_j^{\prime}\dx$, again at
the cell level since that is most convenient when we deal with discontinuous
functions $\basphi_i'$:

$$
\int_{-1}^1 \alpha(\sum_t\tilde u_t\refphi_t)\refphi_r'\refphi_s'\frac{h}{2}dX
= \int_{-1}^1 \dfc(\sum_{t=0}^1
\tilde u_t\refphi_t)\frac{2}{h}\frac{d\refphi_r}{dX}
\frac{2}{h}\frac{d\refphi_s}{dX}\frac{h}{2}dX\nonumber
$$

$$
= \frac{1}{2h}(-1)^r(-1)^s \int_{-1}^1 \dfc(\sum_{t=0}^1 u_t\refphi_t(X))dX
\nonumber
$$

$$
\approx \frac{1}{2h}(-1)^r(-1)^s\dfc (
\sum_{t=0}^1\refphi_t(-1)\tilde u_t) + \dfc(\sum_{t=0}^1\refphi_t(1)\tilde u_t)
\nonumber
$$

$$
\begin{equation}
 = \frac{1}{2h}(-1)^r(-1)^s(\dfc(\tilde u_0) + \dfc(\tilde u^{(1)}))\thinspace .
\label{nonlin:alglevel:1D:fe:Laplace:Ae:Trapez} \tag{3}
\end{equation}
$$

The element matrix in ([3](#nonlin:alglevel:1D:fe:Laplace:Ae:Trapez))
is identical to the one in
([1](#nonlin:alglevel:1D:fe:Laplace:Ae)), showing that the
group finite element method and Trapezoidal integration are
equivalent with a standard finite discretization of a
nonlinear Laplace term $(\dfc(u)u^{\prime})^{\prime}$ using an arithmetic mean for
$\dfc$: $[D_x\overline{x}D_xu]_i$.

**Remark about integration in the physical $x$ coordinate.**

We might comment on integration in the physical coordinate system too.
The common Trapezoidal rule in the section [Numerical integration of nonlinear terms by hand](#nonlin:alglevel:1D:fe:f)
cannot be used to integrate derivatives like $\basphi_i^{\prime}$, because
the formula is derived under the assumption of a continuous integrand.
One must instead use the more basic version of the Trapezoidal rule
where all the trapezoids are summed up. This is straightforward, but
I think it is even more straightforward to apply the Trapezoidal
rule on the reference cell and assemble the contributions.

The term $\int auv\dx$ in the variational form is linear and gives
these terms in the algebraic equations:

$$
\frac{ah}{6}(u_{i-1} + 4u_i + u_{i+1})
= ah[u - \frac{h^2}{6}D_xD_x u]_i\thinspace .
$$

The final term in the variational form is the Neumann condition
at the boundary: $Cv(0)=C\basphi_i(0)$. With a left-to-right numbering
only $i=0$ will give a contribution $Cv(0)=C\delta_{i0}$ (since
$\basphi_i(0)\neq 0$ only for $i=0$).

**Summary.**

For the equation

$$
-(\dfc(u)u^{\prime})^{\prime} +au = f(u),
$$

P1 finite elements results in difference equations where

 * the term $-(\dfc(u)u^{\prime})^{\prime}$ becomes $-h[D_x\overline{\dfc(u)}^xD_x u]_i$
   if the group finite element method or Trapezoidal integration is applied,

 * $f(u)$ becomes $hf(u_i)$ with Trapezoidal integration or the
   "mass matrix" representation $h[f(u) - \frac{h}{6}D_xD_x f(u)]_i$
   if computed by a group finite element method,

 * $au$ leads to the "mass matrix" form $ah[u - \frac{h}{6}D_xD_x u]_i$.

As we now have explicit expressions for the nonlinear difference
equations also in the finite element method, a Picard or Newton method
can be defined as shown for the finite difference method.  However,
our efforts in deriving symbolic forms of the difference equations in
the finite element method was motivated by a desire to see how
nonlinear terms in differential equations make the finite element and
difference method different. For practical calculations in computer
programs we apply numerical integration, normally the more accurate
Gauss-Legendre quadrature rules, to the integrals directly. This
allows us to easily *evaluate* the nonlinear algebraic equations for a
given numerical approximation of $u$ (here denoted $u^{-}$).  To
*solve* the nonlinear algebraic equations we need to apply the Picard
iteration method or Newton's method to the variational form directly,
as shown next.