1 function A = poisson_matrix(integerN, x0, xN)
3 ## In the numerical solution of the poisson equation,
7 ## in one dimension, subject to the boundary conditions,
12 ## over the interval [x0,xN], we need to compute a matrix A which
13 ## is then multiplied by the vector of u(x0), ..., u(xN). The entries
14 ## of A are determined from the second order finite difference formula,
15 ## i.e. the second order forward Euler method.
19 ## * ``integerN`` - An integer representing the number of
20 ## subintervals we should use to approximate `u`. Must be
21 ## greater than or equal to 2, since we have at least two
22 ## values for u(x0) and u(xN).
24 ## * ``f`` - The function on the right hand side of the poisson
27 ## * ``x0`` - The initial point.
29 ## * ``xN`` - The terminal point.
33 ## * ``A`` - The (N+1)x(N+1) matrix of coefficients for u(x0),
41 [xs,h] = partition(integerN, x0, xN);
43 ## We cannot evaluate u_xx at the endpoints because our
44 ## differentiation algorithm relies on the points directly to the left
46 differentiable_points = xs(2:end-1);
48 ## These are the coefficient vectors for the u(x0) and u(xn)
49 ## constraints. There should be N zeros and a single 1.
50 the_rest_zeros = zeros(1, integerN);
51 u_x0_coeffs = cat(2, 1, the_rest_zeros);
52 u_xN_coeffs = cat(2, the_rest_zeros, 1);
54 ## Start with the u(x0) row.
57 for x = differentiable_points
58 ## Append each row obtained from the forward Euler method to A.
59 u_row = forward_euler(2, xs, x);
63 ## Finally, append the last row for xN and negate the whole thing (see
64 ## the definition of the poisson problem).
65 A = - cat(1, A, u_xN_coeffs);