title: Application of Boundary Conditions in finite difference solution for the heat equation and Crank-Nicholson
body: <p>The code below solves the 1D heat equation that represents a rod whose ends are kept at zero temparature with initial condition 10*np.sin(np.pi*x). </p>
  
  <p>How are the Dirichlet boundary conditions (zero temp at both ends) worked into this calculation? I was told the upper, lower rows of matrix A contain two non-zero elements, and the missing third element <em>is</em> the Dirichlet condition. But I do not understand by which mechanism this condition affects the calculation. With missing elements in A, how can u_{0} or u_{n} be zero?</p>
  
  <p>The finite difference method below uses Crank-Nicholson.</p>
  
  <pre><code>import numpy as np
  import scipy.linalg
  
  # Number of internal points
  N = 200
  
  # Calculate Spatial Step-Size
  h = 1/(N+1.0)
  k = h/2
  
  x = np.linspace(0,1,N+2)
  x = x[1:-1] # get rid of the '0' and '1' at each end
  
  # Initial Conditions
  u = np.transpose(np.mat(10*np.sin(np.pi*x)))
  
  # second derivative matrix
  I2 = -2*np.eye(N)
  E = np.diag(np.ones((N-1)), k=1)
  D2 = (I2 + E + E.T)/(h**2)
  
  I = np.eye(N)
  
  TFinal = 1
  NumOfTimeSteps = int(TFinal/k)
  
  for i in range(NumOfTimeSteps):
      # Solve the System: (I - k/2*D2) u_new = (I + k/2*D2)*u_old
      A = (I - k/2*D2)
      b = np.dot((I + k/2*D2), u)
      u = scipy.linalg.solve(A, b)
  </code></pre>