







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Homework 5 MA/CS 375, Fall 2005 Due December 1
(a) Generate 4 random column vectors of size 4 × 1 (b) Normalize these veectors so their length is unity. (c) Compute the volume of the parallelepiped they define. You must use the LU factorization for computing the determinant of the matrix A whose columns are the 4 random vectors (that is DO NOT use the command det(A) here!). (d) compute the condition number of A, κ(A). (e) Repeat the process 1000 times and produce a scatter plot of det(A) vs (^) κ(^1 A).
What are the maximum and minimum values of det(A)? What is the condition number for these values? How can you explain the extreme values of the condition number and the corresponding values of the determinant?
Solutions:
(a) >>A=rand(4);
(b) >>A=A*diag(1./sum(A))
(c) [L,U]=lu(A); vol=abs(prod(diag(U)));
vol = 0. 006621 ...
(d) >>cond(A) κ(A) = 12. 701 ... (e) >> d=zeros(1000,1); kappa=d;
for j=1: A=rand(4); A=A*diag(1./sqrt(sum(A.^2))); [L,U]=lu(A); d(j)=prod(diag(U)); kappa(j)=cond(A); end loglog(abs(d),kappa,’.’) [dmin,mindex]=min(abs(d)); kappamin=kappa(mindex); [dmax,maxdex]=max(abs(d)); kappamax=kappa(maxdex); The minimum determinant was 8.2691e-05 and the corresponding condition number was 3.7498e+03. The maximum determinant was 0.4247 and the corresponding condition number was 3.
10 −^4 10 −^3 10 −^2 10 −^1
10 −^3
10 −^2
10 −^1
|det(A)|
1/ κ(A)
Using polyfit to find the regression line, we get that log κA = − 0 .96433 log(| det(A)|) − 0. 73128
Matlab returns singular matrix warnings for the 12 × 12 and 16 × 16 matrices. The relative errors and relative residuals are
re4=norm(z4-z4h)/norm(z4); rr4=norm(b4-C4z4h)/norm(b4); re8=norm(z8-z8h)/norm(z8); rr8=norm(b8-C8z8h)/norm(b8); re12=norm(z12-z12h)/norm(z12); rr12=norm(b12-C12z12h)/norm(b12); re16=norm(z16-z16h)/norm(z16); rr16=norm(b16-C16z16h)/norm(b16); RE=[re4 re8 re12 re16]
9.7061e-09 0.00010243 3.6848 48.
RR=[rr4 rr8 rr12 rr16]
RR =
1.2303e-16 2.026e-16 1.4917e-16 5.1294e-
The relative residual is O(m) is bounded above by κ(A) · m.
eps*K./RE
ans =
Therefore, for a given machine epsilon, one can determine the worst possible relative error in solving the linear system by using the matrix condition number as a bound.
∂u ∂t
∂^2 u ∂x^2 for − 1 ≤ x ≤ 1 and 0 ≤ t ≤ T with boundary conditions
u(x = − 1 , t) = u(x = 1, t) = 0 ,
and initial condition u(x, t = 0) = sin πrx, − 1 ≤ x ≤ 1.
The Crank-Nicolson scheme is a popular scheme for solving the heat equation because it is accurate of order 2 both in the timestep, dt := k and in the spatial discretization interval dx := h. It is defined by the finite difference equations:
un m+1 − unm k
un m+1+1 − 2 un m+1 + un m+1− 1 h^2
unm+1 − 2 unm + unm− 1 h^2
, n = 0,... , N − 1 , m = 1,... , M − 1
where we have introduced the discretization:
unm := u(xm, tn) , xm = −1 + h ∗ m , m = 0,... , M ; tn = k ∗ n , n = 0,... , N
with h = 2/M and k = T /N. The reason for the increase in accuracy is that the above expressions give the derivatives of the function u(x, t) with respect to x and t at the point xm and the instant t = (tn + tn+1)/2 so we can think of the time derivative formula as a centered (rather than a forward) difference. You must demonstrate the accuracy of the scheme by carrying out computations for different timesteps k, and comparing your results with the exact solution given by the expression:
uexact(x, t) = e−π
(^2) r (^2) t sin πrx.
Specifically:
(a) Write the system of equations in the form
(I − δD) un+1^ = (I + δD) un^ , n = 0,... , N − 1
where δ =
k 2 h^2
, un^ = (un 1 ,... , unM − 1 )T^ (un 0 = unM = 0)
(b) Start the computation off by setting
u^0 m = sin πkxm = sin πk(−1 + h ∗ m) , m = 1,... , M − 1
(c) perform the LU factorization of the left-hand matrix LU = D−^ := (I − δD). You must define D−^ (and D+) using the command spdiags to take advantage of its sparsity when solving this system. (d) Iterate until the final time, t = T is reached. (e) Run with T = 1 and r = 1. Use M = 20, 40 , 80 , 160 and N = 10, 20 , 40 , 80.
in=2:M; m=M-1; % indices of interior points u=sin(pirx); % Get initial condition e=ones(M-1,1); % Column vector of all ones
% Left hand side matrix A=spdiags([-dele, (1+2del)e, -dele], -1:1, m, m);
[L,U]=trilu(A); % Do LU factorization coeff=1-2*del; % Lump diagonal coefficient for RHS
for n=1:N % Time stepping loop
f=coeffu+del([0;u(1:M)]+[u(2:M1);0]); %RHS u(in)=U(L\f(in)); end
exact=exp(-(pir)^2T)sin(pir*x); err=norm(u-exact,1)/norm(exact,1);
function [L,U] = trilu(A); n=length(A); am=diag(A,-1); a0=diag(A); ap=diag(A,1); for k = 1:(n-1) am(k)=am(k)/a0(k); k1=k+1; a0(k1)=a0(k1)-am(k)*ap(k); end L=speye(n)+spdiags(am,-1,n,n); U=spdiags([a0 [0;ap]],0:1,n,n);
The table of results for the specified M,N pairs is If we look at the error over a wider range of interval
M↓ N→ 10 20 40 80 20 5.6688e-01 1.1470e-01 3.2118e-02 7.1080e- 40 6.0013e-01 1.7002e-01 2.9501e-02 7.8380e- 80 6.0808e-01 1.8335e-01 4.4370e-02 7.4273e- 160 6.1005e-01 1.8665e-01 4.8054e-02 1.1210e-
size an time step, the trend is more obvious.
(^11) 1.5 2 2.5 3
2
3
log 10 (M)
log 10 (err(M,N))
log
10
(N)
− 5
−4.
− 4
−3.
− 3
−2.
− 2
−1.
− 1
−0.
The quadratic convergence in spatial discretization can be observed by fixing one of either the time step or grid spacing to a sufficiently small amount and varying the other. In the plot on the left, the number of subintervals was taken to be 1,000 and the time step was varied. In the plot on the right, the number of time steps was taken to be 1,000 and the mesh size was varied.
assumptions be modeled by the nonlinear boundary value problem:
d^2 u dx^2
with boundary conditions u(−1) = u(1) = 0. The parameter λ is sometimes called the Damk¨ohler number. For this problem take λ = (^) cosh 1^1.
i. Verify that: u(x) = − ln (λ cosh x), solves the problem.
ii. Introducing a uniform grid, xj = −1 + jh, j = 0,... , n, h = (^2) n , approximate u(xj ) by Uj and
approximate d
(^2) u dx^2 by the second order central difference formula.^ This leads to the system of n − 1 nonlinear equations:
Fj (u) ≡
Uj− 1 − 2 Uj + Uj+ h^2
where we set U 0 = Un = 0. The Jacobian derivative of a collection of n − 1 functions, Fj of n − 1 variables, U , is the (n − 1) × (n − 1) matrix J(U) whose jkth entry is the derivative of Fj with respect to Uk, Jjk = (^) ∂U∂Fjk. (We denote the n − 1-vectors whose entries are Fj and Uk by F and U.) Show that the Jacobian derivative of the function given above is a tridiagonal matrix whose entries are: Jj,j+1 = Jj+1,j =
h^2
, Jjj = −
h^2
iii. Newton’s method for solving a system of n − 1 equations in n − 1 unknowns is the direct gen- eralization of Newton’s method for a single equation which we studied in Chapter 2. Precisely, given an initial approximation, U(0), we generate subsequent approximations by solving: J(U(k)) d(k)^ = −F(U(k)),
and setting: U(k+1)^ = U(k)^ + d(k). Write a script implementing Newton’s method for the difference approximation described above. Terminate the iteration when: ‖d(k)‖ ≤ 10 −^8. Use U(0)^ = 0. Be sure to treat J as a sparse matrix when solving. Record the number of iterations required and print out ‖d(k)‖ for each k. Does the convergence appear to be quadratic? Carry out the computations for n = 10, 20 , 40. Compute the maximum error, that is the maximum absolute difference between Uj and u(xj ), in each case. Do you observe second order convergence with decreasing h? Plot the solutions in each case.
Solution: du dx
= − tanh(x),
d^2 u dx^2
= tanh^2 (x) − 1
exp(2u) = exp(−2 ln[λ cosh(x)]) = exp
ln[
λ^2 cosh^2 (x)
λ^2 cosh^2 (x)
λ^2
sech^2 (x)
exp(2u) =
λ^2
[1 − tanh^2 (x)] = −
λ^2
d^2 u dx^2
Solutions: Given
Fj (u) ≡
Uj− 1 − 2 Uj + Uj+ h^2
We obtain the Jacobian by differentiating with respect to the each of the elements of the vector u.
Jjk
∂Fj (u) ∂Uk
∂Uk
Uj− 1 − 2 Uj + Uj+ h^2
∂Uj ∂Uk
= δjk
Jjk =
δ(j−1),k − 2 δjk + δ(j+1),k h^2
Clearly, nonzero entries can only occur in the range j − 1 ≤ k ≤ j + 1. Evaluating for the Kronecker delta functions gives the desired results.
Jj,j+1 = Jj+1,j =
h^2
, Jjj = −
h^2
The script for this problem is
function [iter,nd,err]=nlbvp(n,tol)
np1=n+1; nm1=n-1; in=2:n; x=linspace(-1,1,np1)’; h=x(2)-x(1); h2=hh; lambda=sech(1); lambda2=lambdalambda;
e=ones(nm1,1); D2bands=[e -2*e e]/h2; D2=spdiags(D2bands, -1:1, nm1, nm1);
exact=-log(lambda*cosh(x)); % Exact solution
uin=0*e; % Interior points (initial value)
d=e; nd=norm(d); iter=0;
−^01 −0.5 0 0.5 1
x
u(x)
n=
exact computed
−^01 −0.5 0 0.5 1
x
u(x)
n=
exact computed
−^01 −0.5 0 0.5 1
x
u(x)
n=
exact computed −^01 −0.5 0 0.5 1
x
u(x)
n=
exact computed