






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!







Numerical Computing F’05 A. Kapila Assignment 3 Solutions
(a) If a triangular matrix has a zero entry on the main diagonal, then the matrix is necessarily singular. (b) If the rows of a square matrix are linearly dependent, then so are the columns. (c) The multipliers mij in Gaussian elimination with partial pivoting are all no larger than 1 in magnitude, so the entries of the successive reduced matrices cannot grow in size. (d) For nonsingular A, the number of solutions to Ax = b are determined by b.
(a) True, because the determinant of a triangular matrix is simply the product of the elements on the main diagonal. (b) True. Recall that if the rows of A are dependent, then the homogeneous system Ax = 0 has a nontrivial solution x. Now, if v(i)^ are the columns of A, then Ax = 0 can be expanded as
x 1 v(1)^ + x 2 v(2)^ + · · · + x 1 v(n)^ = 0,
i.e., a nontrivial linear combination of the columns of A sums to zero. This is precisely the condition for the columns of A to be linearly dependent. (c) False. In the k th elimination step, row p, where p > k, is replaced by Rp − mpkRk, where mpk is the corresponding multiplier. Thus the new elements of Rp may well become larger than those of the old Rp, but the growth rate is limited because mpk ≤ 1. (d) False. For nonsingular A the solution is unique for each b.
(^) , b =
(a) Find L and U, the lower and upper triangular factors of A. (b) Note that Ax = b can be written as LU x = b, and hence split into the two triangular systems Lc = b, U x = c. Solve Lc = b for c by forward substitution, and then, U x = c for x by backward substitution. (c) Verify the correctness of your answer by checking to see of Ax does indeed equal b.
(a) We carry out the elimination process and keep track of the elimination multipliers. In step 1 the multipliers are m 21 = 2/ 4 , m 31 = 0. The operations R 2 → R 2 − m 21 R 1 and R 3 → R 3 − m 31 R 1 change the matrix A to
A′^ =
In step two the lone multiplier is m 32 = 1/ 2. The operation R 3 → R 3 − m 32 R 2 leads to
This being the last elimination step, the above matrix is in fact U. Recalling how the matrix L is constructed out of the unit matrix, with mij replacing the corresponding zeros, we have
It is a simple matter to confirm that the product LU does indeed equal A. (b) We now have Lc = b. Forward substitution immediately gives
c =
Then U x = c yields
x =
CHECK: This solution does indeed satisfy the equation Ax = b.
Ax(i)^ = b(i), i = 1, 2 , · · · n,
where b(i)^ is a column vector whose n elements are all zero except the i -th element, which is unity. The best way to do so is to first factorize A as LU and then use the procedure used in problem 2 above.
(a) Write a Matlab code that computes A−^1 in the manner outlined above. You may do so by modifying the LU-factorizing code factor.m supplied on the course home page. Apply your code to find the inverse A−^1 where A is a 5-th order nonsingular matrix of your choice. Check the correctness of your answer by computing the matrix product AA−^1 to see if the result is a unit matrix. Be sure to provide all scripts and output.
Hint. You may use the Matlab command A=rand(5) to generate a matrix of order 5 with random entries. (b) Find the operations count involved in solving the n -th order system Ax = b by (i) Gaussian elimination and (ii) by using the inverse matrix. For large n, how do the two methods compare?
Note. Operations count is defined by different authors differently. The text only considers the so-called MD count, i.e., the count of multiplications and divisions. In class we have been counting the total number of operations, including additions and subtractions. Indicate clearly which choice you are making.
(a) See the attached Matlab script a3prob3.m, and the solution output. (b) From the attached scripts LUfactor.m and a3prob3.m, the following bits of code have been printed. These allow us to come up with operation counts. First, consider the work done in solving Ax = b by Gaussian elimination. This is equivalent to LU -decomposition followed by a forward and a backward substitution. The following code refers to LU -decomposition in LUfactor.m.
d^2 T dx^2
Here, T is temperature, ◦^ C, x is distance along the rod, m, H is the heat transfer coefficient between the rod and the ambient air, m−^2 , and Ta is the temperature of the surrounding air, ◦^ C. This equation can be approximated by a set of linear algebraic equations by assuming that the rod is divided into a number of nodes, and replacing the second derivative by a finite-difference approximation. This approximation at the node i,
d^2 T dx^2
Ti+1 − 2 Ti + Ti− 1 h^2 leads to the following linear equation at node i,
−Ti− 1 + (2 + h^2 H)Ti − Ti+1 = Hh^2 Ta. (2)
This equation can be written for each interior node of the rod (see figure below) resulting in a system of linear algebraic equations for the nodal temperatures. The temperatures at the first and last nodes are fixed by the boundary conditions.
(a) Develop an analytical solution for the differential equation for a 10-m rod with the following data.
Ta = 20, T (x = 0) = 40, T (x = 10) = 200, H = 0. 02.
(b) You are to develop a numerical solution for the same parameter values as above, with 9 interior nodes (h = 1 m). i. First, write the problem in a matrix form, Ax = b, where x is a 9-vector of unknown nodal temperatures, and A and b are determined from the given data. ii. What do you notice about the form of A? iii. Solve the system by a numerical method of your choice; you may use Gauss0.m given on the home page, for example, or the Matlab command x = A\b. iv. Plot on the same graph the analytical and numerical solutions, and comment on any differ- ences that you see.
(a) With T = Ta + θ, the differential equation (1) can be written as
d^2 θ dx^2
= Hθ.
Its general solution is θ = C 1 e
√ Hx (^) + C 2 e− √ Hx,
or, in terms of T, T = Ta + C 1 e
√ Hx (^) + C 2 e − √ Hx.
The constants C 1 and C 2 are found by applying the boundary conditions at x = 0 and x = 10. The upshot is the analytical solution, which, after some manipulation, is best expressed in terms of the hyperbolic sine as
T = Ta + [T (10) − Ta] sinh(
Hx) + [T (0) − Ta] sinh{
H(10 − x)} sinh(
(b) i. The difference equation (2), when applied to i = 1 · · · 9 , leads to a system of 9 linear equations, which can be put in matrix form as
Ax = b where A is given by 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H − 1 0 0 0 0 0 0 0 − 1 2 + h^2 H
x =
, and b =
Hh^2 Ta + T 0 Hh^2 Ta Hh^2 Ta Hh^2 Ta Hh^2 Ta Hh^2 Ta Hh^2 Ta Hh^2 Ta Hh^2 Ta + T 10
ii. A is a tridiagonal matrix. iii. See attached Matlab script a3prob4.m. iv. The graph is shown below. The agreement between the exact and numerical solutions is excellent, even though the grid is rather coarse.
(^400 1 2 3 4 5 6 7 8 9 )
60
80
100
120
140
160
180
200
x
Temperature
Numerical Exact
end
%
disp('Matrix L');
disp(L); % Display L
disp('Matrix U');
disp(U); % Display U
disp('Matrix L*U');
disp(LU); % Display LU to see if A is
recovered
disp('Matrix A');
disp(A);
% script a3prob3.m
%
% Computes Ainv, the inverse of A, by using LU decomposition.
% The ith column xi of Ainv is found by solving
%
% A xi = bi
%
% where bi is the ith column of the identity matrix I.
%
% Provide A
n=5;
A=rand(n);
B=eye(n); % B is identity matrix of order n
Ainv=[]; % Ainv starts out as an empty matrix
%
% Call LUfactor.m
LUfactor
%
% For b = successive columns of I, solve LUx=b
% First solve Cc=b by forward substitution
for i=1:n
b=B(1:n,i); for j=1:n sum=0; for k=1:j- sum = sum+L(j,k)*c(k); end c(j)=(b(j)-sum)/L(j,j); end
%
% Now solve Ux=c by backward substitution
%
for j=n:-1: sum=0; for k=j+1:n sum=sum+U(j,k)*x(k); end x(j)=(c(j)-sum)/U(j,j); end
% Append the solution just found as a column of Ainv
Ainv=[Ainv,x'];
end
disp('Matrix Ainv')
disp(Ainv)
II=A*Ainv;
display('Matrix A*Ainv')
% script a3prob4.m
%
%
H=0.02; h=1; Ta=20; T0=40; T10=200;
A=zeros(9,9);
%
for i=1:
A(i,i)=2+hhH; b(i)=Hhh*Ta;
end
for i=2:
A(i,i-1)=-1; A(i,i+1)=-1;
end
A(1,2)=-1;
A(9,8)=-1;
b(1)=b(1)+T0;
b(9)=b(9)+T10;
%
T=A\b;
T=[T0;T;T10];
space=[0:10]
plot(space,T,'ro')
hold on
HH=sqrt(H);
Texact=Ta+( (T10-Ta)sinh(HHspace)+(T0-Ta)sinh(HH(10-space)) )/sinh
(10*HH);
plot(space,Texact,'b')
legend('Numerical','Exact')
xlabel('x'); ylabel('Temperature')