Spectral Methods in MATLAB

So far wehave treated just simple homogeneous Dirichlet boundary conditions u( 1) = 0, as well as periodic boundary conditions. Of course, many problems require more than this, and in this chapter we outline some of the techniques available.
There are two basic approaches to boundary conditions for spectral collocation methods:
Restrict attention to interpolants that satisfy the boundary conditions; or
Do not restrict the interpolants, but add additional equations to enforce the boundary conditions.
So far we have only used method (I), but method (II) is more flexible and is often better for more complicated problems. (It is related to the so-called tau methods that appear in the field of Galerkin spectral methods.)
We begin with another example involving method (I). In Program 13 (p. 64) we solved u xx = e 4x on [ ?1, 1] subject to u( ?1) = u(1) = 0. Consider now instead the inhomogeneous problem
Method (I) can be applied in this case too, with embarrassing ease. Since the equation is linear and the second derivative of x is zero, we can simply solve the problem with u( 1) = 0 and then add ( x + 1)/2 to the result. See Program 32.
<b class="bold">% p32.m - solve u_xx = exp(4x), u(-1)=0, u(1)=1 (compare p13.m) N = 16; [D, x] = cheb(N); D2 = D^2; D2 = D2(2:N,2:N); f = exp(4*x(2:N)); u = D2\f;