Practice problems for exam

Problem 1

A = [2 -2 1; 3 2 -3; 1 1 5]; A = A([3 2 1],:)
A = 3×3
1 1 5 3 2 -3 2 -2 1
[L,U,p] = lu(A,'vector')
L = 3×3
1.0000 0 0 0.6667 1.0000 0 0.3333 -0.1000 1.0000
U = 3×3
3.0000 2.0000 -3.0000 0 -3.3333 3.0000 0 0 6.3000
p = 1×3
2 3 1

Problem 2

L = [1 0 0; -2 1 0; 2 -3 1];
U = [2 1 -1; 0 -3 2; 0 0 1];
p = [3; 1; 2];
b = [2; 1; 4];
y = L\b(p) % solve with forward substitution
y = 3×1
4 10 23
x = U\y % solve with back substitution
x = 3×1
7.5000 12.0000 23.0000

Problem 3

A = [-7 8 -8; -8 9 -9; 6 -7 8];
b = [.97; 1.02; -.01];
xhat = [1; 2; 1];
bhat = A*xhat
bhat = 3×1
1 1 0
epsb = norm(bhat-b,1)/norm(bhat,1)
epsb = 0.0300
Solution 1: We use with
normA = norm(A,1)
normA = 25
bound = norm(A,1)*25*epsb
bound = 18.7500
Solution 2: We use , hence and .
Therefore .
bound = 25*norm(bhat-b,1)/norm(xhat,1)
bound = 0.3750
This is the error bound , see "Errors for linear systems".

Problem 4

t = [0; 1; 3];
y = [2; 4; 3]
y = 3×1
2 4 3
A = [t, t.^2]
A = 3×2
0 0 1 1 3 9
M = A'*A
M = 2×2
10 28 28 82
v = A'*y
v = 2×1
13 31
c = M\v % solve normal equations A*c = v
c = 2×1
5.5000 -1.5000
Instead of the normal equations we can also use the Matlab \ command:
c = A\y
c = 2×1
5.5000 -1.5000

Problem 5

f = @(x) x^3+x-11;
(a) Secant method
x0 = 3; x1 = 2;
x2 = x1 - f(x1)*(x1-x0)/(f(x1)-f(x0))
x2 = 2.0500
(b) Newton method
fp = @(x) 3*x^2 + 1; % f'(x)
x0 = 2;
x1 = x0 - f(x0)/fp(x0)
x1 = 2.0769
(c) Use fzero: Note that and have different signs. Hence there is a solution in the interval . (Since the function f is strictly increasing, and the solution is unique.)
xs = fzero(f,[2,3])
xs = 2.0743

Problem 6

Define the function :
f = @(x) [4*x(1)+x(1)^2*x(2)-4; 4*x(2)-x(1)*x(2)^2-3];
Find the Jacobian
fp = @(x) [4 + 2*x(1)*x(2), x(1)^2; -x(2)^2, 4 - 2*x(1)*x(2)];
Perform one step of the Newton method starting with :
x0 = [1;0];
b = f(x0)
b = 2×1
0 -3
A = fp(x0)
A = 2×2
4 1 0 4
d = -A\b % solve linear system A*d = -b
d = 2×1
-0.1875 0.7500
x1 = x0 + d
x1 = 2×1
0.8125 0.7500
Iterate Newton method until :
x = [1;0]
x = 2×1
1 0
while 1
b = f(x);
if norm(b,1)<=1e-10
break
end
A = fp(x);
d = -A\b;
x = x + d;
end
x
x = 2×1
0.8369 0.9316
normf = norm(b,1)
normf = 8.8818e-16
Use fsolve (with default settings):
xs = fsolve(f,[1;0])
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details>
xs = 2×1
0.8369 0.9316
norm(f(xs),1)
ans = 9.6660e-10