MATH 55 Spectral Methods Spring 9 Homework Set 5 - SOLUTIONS. Suppose you are given an n n linear system Ax = f where the matrix A is tridiagonal b c a b c. A =.........,. a n b n c n a n b n with x = (x, x,..., x n ) T and f = (f, f,..., f n ) T. (a) Write a MATLAB M-file trisolve.m which computes the solution of the tridiagonal system Ax = f given the n-vectors a, b, c, and f. Include a copy of your code. The first line of the M-file should read function x = trisolve(a,b,c,f) (b) To check your code, let n =, a, b 5 and c. Compute the solution for f = (,,...,, ) T, with the exact solution easily seen to be x = (,..., ) T. Use the diary command in MATLAB to include a copy of your results. ANS: For (a) here is the code: function x = trisolve(a,b,c,f) % % TRISOLVE : tridiagonal solver for nxn system Ax=f where % A = diag(a(:n),-) + diag(b) + diag(c(:n-),) % n = length(b); x = zeros(n,); for i = :n end m = a(i)/b(i-); b(i) = b(i)-m*c(i); f(i) = f(i)-m*f(i-); % forward reduction x(n) = f(n)/b(n); % back substitution for i = n-:-: x(i) = (f(i)-c(i)*x(i+))/b(i); end To check that it works in MATLAB I executed >> a = -*ones(,); c = a; b = 5*ones(,); f = ones(,); f()=; f()=; >> x = trisolve(a,b,c,f);
>> x x = and indeed the answer is correct.
. Consider the discrete finite difference (FD) analog of the continuous eigenproblem d u dx = λu, < x < u() = u() = with d /dx D, the centered O(h ) FD approximation to the second derivative. To this end, choose N >, let h = /N and x i = i h for i =,,..., N. Note we now have N + grid points with x = and x N =. So we seek eigenpairs (λ, v) which satisfy (D v) i = v i v i + v i+ h = λv i for i =,,..., N, v = v N =, or in matrix form, h............... v v. v N v N = λ v v. v N v N (a) Show that (λ k, v k ) is an eigenpair for k =,,..., N where λ k = ( cos kπh)/h and v k is the vector with (v k ) i = sin ikπh for i =,,..., N. Hint: Use trig identities, and note that sin kπh = sin Nkπh =. (b) For N = 8 and N = 6 separately, plot (using MATLAB s subplot) the continuous eigenfunctions found in class, and the discrete eigenvectors from (a) for k =,, N/ and N. Thus each plot should have 4 images. Use a linetype for the continuous eigenfunctions and symbols for the discrete eigenvectors. Label and title your graphs. (c) In a single plot display the first eigenvalues vs. there number, i.e., k =,,..., of the continuous problem along with the N eigenvalues for the discrete case for N = 8, 6 and. For the continuous case plot the eigenvalues using a linetype with a symbol, and in the discrete case just symbols. Label, title, and place a legend on your plot. Discuss the results. ANS: (a) Noting that sin kπh = sin Nkπh =. Fix a k, k =,..., N. For i =,..., N, v k,i v k,i = v k,i+ h = = = = sin (i )kπh sin ikπh + sin (i + )kπh h (sin ikπh cos kπh cos ikπh sin kπh) sin ikπh+(sin ikπh cos kπh+cos ikπh sin kπh) h sin ikπh cos kπh sin ikπh h ( cos kπh) sin ikπh h = λ k sin ikπh. = λ k v k,i. So we have Av k = λ k v k for k =,..., N, thus (λ k, v k ) are e-pairs for k =,..., N.
(b) Here is the code for N = 8: N=8; h=/n; x=:h:; xx=:.:; k=[ N/ N-]; for i=:4 subplot(,,i) plot(xx,sin(k(i)*pi*xx),x,sin(k(i)*pi*x), o ) grid,axis( tight ) title([ sin(,numstr(k(i)), \pix): N=,numstr(N),, k=,numstr(k(i))]); end And the graphs for N = 8 and N = 6: sin(πx): N=8, k= sin(πx): N=8, k=.8.5.6.4..5..4.6.8..4.6.8 sin(4πx): N=8, k=4 sin(7πx): N=8, k=7.5.5.5.5..4.6.8..4.6.8 sin(πx): N=6, k= sin(πx): N=6, k=.8.5.6.4..5..4.6.8..4.6.8 sin(8πx): N=6, k=8 sin(5πx): N=6, k=5.5.5.5.5..4.6.8..4.6.8
(c) Here is the code to generate the plot, and the plot itself: N=[8 6 ]; h=./n; N=N-; plot( :N(),-*(-cos((:N())*pi*h()))/(h()^), p,... :N(),-*(-cos((:N())*pi*h()))/(h()^), +,... :N(),-*(-cos((:N())*pi*h()))/(h()^), *,... :,-(pi*(:)).^, -o, markersize,6, linewidth,) grid,title( Continuous vs. Discrete spectrum of d^/dx^ ) ylabel( eigenvalue ),xlabel( mode number ),legend( N=, N=6, N=8, continuous,) Discussion: From part (b) we see that the eigenvectors of the discrete differentiation matrix are the corresponding eigenfunctions of the continuous problem evaluated at the grid points determined by the specific N. In this sense the discrete problem is clearly a very good approximation to the continuous one. On the other hand, note in the plot above that while for each N the first few discrete eigenvalues are good approximations, this is not the case for the eigenvalues corresponding to the higher modes. Thus one expects these modes in the continuous problems not to be modeled well by the discrete approximation. Note: A Taylor series expansion gives (kπ) λ k = (kπ) + ( cos kπh) h = (kπ) + (kπh) ( ( + (kπh)4 )) h 4 = (kπ) + ( ) (kπh) h (kπh)4 + = (kπ) + (kπ) h ( (kπh) 4 ( ) 4 4 (kπ) 4 = h + O(h 4 ) = O(h ) So we do have the expected accuracy of approximation, but remember there is still a term in the error that depends on the eigenvalue being approximated, (kπ) 4 /, which explains, even for larger N, why we do not see rapid convergence of the discrete eigenvalues to the continuous ones as k N. In fact, for a fixed N and k large, we see divergence in the upper spectrum. )
. Boundary Value Problems and Boundary Layers: Consider the two-point boundary value problem { ɛu + u = x +, < x < u() = u() = where ɛ > is a given parameter. The exact solution is given by u(x) = x + sinh x ɛ + sinh x ɛ sinh ɛ (a) Using your tridiagonal solver compute the solution for ɛ = and N = /h = n for n =, 4, 6, 8. Using the subplot command, plot the exact solution and the computed solution for each N on the same page, i.e. 4 plots on the same page. Also, compute the ratios u v /h for each h = /N. Discuss the results. Include a copy of your code. (b) Repeat the exercise above for ɛ =. Again, discuss the results. What has changed, i.e. what is the effect of a smaller ɛ? ANS: (a) First, here are the computational results with ɛ = : N=4 eps=. N=6 eps=..5.5 N=64 eps=. N=56 eps=..5.5 Note that the computed solution and its linear interpolation does a good job of approximating the true solution for N 8 as is demonstrated in the numerical convergence seen in the error data
N inf_err inf_err/h^ ------------------------------------------ 4.e+.885e- 4.66e-.6e+.99e- 5.977e- 6.4e+.58e-4 5.7e-.56e+ 7.86e-6 5.88e- (b) Now, here are the computational results with ɛ = : Note that the computed N=4 eps=. N=6 eps=..5.5 N=64 eps=. N=56 eps=..5.5 solution does not do a very good job of approximating the true soltuion with N = 4 in this case due to the steep gradients in the solution near the boundary. N inf_err inf_err/h^ ------------------------------------------ 4.e+ 4.54e- 7.67e-.6e+.77e-.757e+ 6.4e+.98e- 4.498e+.56e+ 7.64e-4 4.597e+ Here is the code for ɛ = : N =.^(::8); epsilon = ^(-); hold off xx = :.5:; % compute true solution uu on fine grid xx uu = *xx+-(sinh((-xx)/sqrt(epsilon))+... *sinh(xx/sqrt(epsilon)))/sinh(/sqrt(epsilon)); err=zeros(4,); % compute approx for each N, plot, and store error for k=:length(n)
n = N(k); h = /n; x = (:h:) ; a = -epsilon*ones(n-,)/(h^); c = a; b = *epsilon*ones(n-,)/(h^)+; f = *x(:n)+; v = trisolve(a,b,c,f); v = [ v ] ; subplot(,,k), plot(xx,uu,x,v, *- ), axis([-.5.5.5]), grid title([ N=,numstr(n), eps=,numstr(epsilon)]) u = *x+-(sinh((-x)/sqrt(epsilon))+*sinh(x/sqrt(epsilon)))... /sinh(/sqrt(epsilon)); err(k,) = n; err(k,) = norm(u-v,inf); err(k,) = err(k,)/(h.^); end hold off, format short e disp( N inf_err inf_err/h^ ) disp( ------------------------------------------ ) disp(err) savepdf( hw5_pa.pdf,,)
4. Consider again the BVP in problem, but now using a Chebyshev spectral method. (a) By modifying the m-file cheb.m produce an m-file [D,x] = cheb(n) that produces grid points for the interval [, ] instead of [, ], and an appropriately modified differentiation matrix D. Note that this can be done by applying the linear mapping x (x+)/ to the grid points and then applying the chain rule to compute the modified D. Include a copy of your code. (b) Repeat parts (a) and (b) of problem using a spectral scheme but now with N = n with n =,, 4, 5. Produce a separate plot with four images for each ɛ. In addition, plot the log of the infinity-norm errors for both values of ɛ together on the same plot. Discuss the results in relation to those in problem using a finite difference scheme. ANS: (a) We now recompute the BVP from problem using our spectral method for the interval [, ]. Here are the solution profiles for ɛ =., and the infinity norm errors given N=4 eps=. N=8 eps=..5.5 N=6 eps=. N= eps=..5.5 by N inf_err --------------------------- 4.e+.566e- 8.e+.99e-7.6e+.9984e-5.e+.88e-4
Now with ɛ = we have N=4 eps=. N=8 eps=..5.5 N=6 eps=. N= eps=..5.5 N inf_err --------------------------- 4.e+ 7.84e- 8.e+.877e-.6e+ 5.644e-5.e+.98e-4 Putting the results from both the FD and spectral method together we have Here is the code for cheb followed by the BVP solver: % CHEB compute D = differentiation matrix, x = Chebyshev grid % for the interval [,] from right to left function [D,x] = cheb(n) if N==, D=; x=; return, end x = cos(pi*(:n)/n) ; c = [; ones(n-,); ].*(-).^(:N) ; X = repmat(x,,n+); dx = X-X ; D = (c*(./c) )./(dx+(eye(n+))); % off-diagonal entries D = D - diag(sum(d )); % diagonal entries x = (x+)/; D=*D; % mapping to account for [- ] ->[,]
Error: FD vs. SPECTRAL for solving BVP Infinity norm error 5 5 FD eps =. FD eps =. SPEC eps =. SPEC eps =. 5 5 5 N N =.^(:5); epsilon = ^(-); xx = :.5:; % compute true solution uu on fine grid xx uu = *xx+-(sinh((-xx)/sqrt(epsilon))+ *sinh(xx/sqrt(epsilon)))/sinh(/sqrt(epsilon)); err=zeros(4,); % compute approx for each N, plot, and store error for k=:length(n) n = N(k); [D,x] = cheb(n); D = D^; D = D(:n,:n); f = *x(:n)+; v = [-epsilon*d+eye(n-)]\f; v = [ v ] ; subplot(,,k), plot(xx,uu,x,v, *- ), axis([-.5.5.]), grid title([ N=,numstr(n), eps=,numstr(epsilon)]) u = *x+-(sinh((-x)/sqrt(epsilon))+*sinh(x/sqrt(epsilon)))/sinh(/sqrt(epsilon)); err(k,) = n; err(k,) = norm(u-v,inf); end hold off, format short e disp( N inf_err ) disp( --------------------------- ) disp(err)