# Mathematical error when attempting to represent step function using fourier series

I am attempting to work through a very simple problem.

Determine the Fourier series expansion for the following heat PDE problem with ICS and BCS:

$$u_{t} = \alpha^2u_{xx}$$

$$u(0, t) = u(L, t) = 0$$

$$u(x, 0) = \begin{cases} 0 & 0 \leq x \leq L/2 \\ 1 & L/2 < x \leq L\end{cases}$$

Given the form of the boundary conditions, and using separation of variables, I concluded that:

$$\kappa = \frac{n\pi}{L}$$

$$u(x, t) = \sum_{n=1}^{\infty} a_n \exp(-\alpha^2\kappa^2t) \sin(\kappa x)$$

In order to use the ICs, one uses the orthogonality of sinusoids to obtain:

$$a_n = \frac{2}{L}\int_{0}^{L} u(x, 0) \sin(\kappa x)\;\textrm{d}x$$

In our case, with the given $u(x, 0)$, I obtained:

\begin{align} a_n &= \frac{2}{L}\int_{0}^{L} u(x, 0) \sin(\kappa x)\;\textrm{d}x \\ &= \frac{2}{L}\left[\int_{0}^{L/2} 0\;\textrm{d}x + \int_{L/2}^{L} \sin(\kappa x) \;\textrm{d}x\right] \\ &= \frac{2}{L}\int_{L/2}^{L} \sin(\kappa x) \;\textrm{d}x \\ &= -\frac{2}{n\pi}\cos{n\pi} \end{align}

Now, I have written some very simple python code to compute the first however many terms I'd like:

import numpy as np

#=========================================

def sum_f_series(start, end_p1, t, x, L, alpha2):
n = np.arange(start,end_p1)
n2 = n*n

K = (np.pi/L)

C1 = -1*alpha2*(K**2)*t
t_part = np.exp(C1*n2)

C2 = K
x_part = np.sin(C2*n)

a_n = -1*(2*np.pi/n)*np.cos(n*np.pi)

return np.sum(t_part*x_part*a_n)

#======================================

start = 1
end_p1 = 11 # last term + 1

a = 0
b = 1.5
L = b-a

t = 0
x = 1

alpha2 = 5

print sum_f_series(start, end_p1, t, x, L, alpha2)


The result is around $6.6$ -- a far cry from $1$, which is what the result should be (consider the initial conditions, and the fact that $1 > 0.75 = L/2$).

So, I am making an error somewhere, but I have been unable to find it. Can you help?

Your errors are on the lines for x_part and a_n. The argument to np.sin() should include x, and the coefficient for a_n by your calculation should be 2/(np.pi*n).