profile pic
⌘ '
raccourcis clavier

Problem 1 [8 points] Consider the system Ax=bAx = b, where A=[0.10.30.90.30.92.70.60.70.1]A=\begin{bmatrix} 0.1 & 0.3 & 0.9\\ 0.3 & 0.9 & 2.7\\ 0.6 & 0.7 & 0.1 \end{bmatrix} and b=[1.33.91.4]Tb = \begin{bmatrix} 1.3 & 3.9 & 1.4\end{bmatrix}^T

a. [2 points] Show that AA is singular.

b. [2 points] If we were to use Gaussian elimination with partial pivoting to solve this system using exact arithmetic, show where the process fails.

c. [2 points] Solve this system in double precision using partial pivoting. Do not use Matlab’s functions. What is the solution that you obtain?

d. [2 points] Matlab’s A\b produces NaN -Inf Inf as a solution. Explain why NaN, -Inf and Inf.

Answer:

a. For AA to be singular, prove det(A)=0det(A) = 0

Using Gaussian elimination without partial pivoting

Ab=[0.10.30.91.30.30.92.73.90.60.70.11.4] R2R1Ab=[0.10.30.91.30.20.61.82.60.60.70.11.4] R33R1Ab=[0.10.30.91.30.20.61.82.60.30.22.62.5] R312R2Ab=[0.10.30.91.30.20.61.82.60.20.53.53.8] Thus Ab[0.10.30.91.30.20.61.82.60.20.53.53.8]  det(A)=a(eifh)b(difg)+c(dheg),A=[abcdefghi]  det(A)=0.1(0.63.5+1.80.5) 0.3(0.23.51.80.2)+ 0.9(0.50.20.60.2)=0\begin{aligned} A|b &= \begin{bmatrix} 0.1 & 0.3 & 0.9 & | & 1.3\\ 0.3 & 0.9 & 2.7 & | & 3.9\\ 0.6 & 0.7 & 0.1 & | & 1.4 \end{bmatrix} \\\ R_{2} - R_{1} \rightarrow A|b &= \begin{bmatrix} 0.1 & 0.3 & 0.9 & | & 1.3\\ 0.2 & 0.6 & 1.8 & | & 2.6\\ 0.6 & 0.7 & 0.1 & | & 1.4 \end{bmatrix} \\\ R_{3} - 3*R_{1} \rightarrow A|b &= \begin{bmatrix} 0.1 & 0.3 & 0.9 & | & 1.3\\ 0.2 & 0.6 & 1.8 & | & 2.6\\ 0.3 & -0.2 & -2.6 & | & -2.5 \end{bmatrix} \\\ R_3 - \frac{1}{2}*R_2 \rightarrow A|b &= \begin{bmatrix} 0.1 & 0.3 & 0.9 & | & 1.3\\ 0.2 & 0.6 & 1.8 & | & 2.6\\ 0.2 & -0.5 & -3.5 & | & -3.8 \end{bmatrix} \\\ \text{Thus } \rightarrow A|b \leftarrow &\begin{bmatrix} 0.1 & 0.3 & 0.9 & | & 1.3\\ 0.2 & 0.6 & 1.8 & | & 2.6\\ 0.2 & -0.5 & -3.5 & | & -3.8 \end{bmatrix} \\\ & \\\ det(A) = a(ei−fh)−b(di−fg)+c(dh−eg), A &=\begin{bmatrix} a & b & c \\ d & e & f\\ g & h & i \end{bmatrix} \\\ & \\\ \rightarrow det(A) = 0.1*(-0.6*3.5+1.8*0.5) - & \\\ 0.3*(-0.2*3.5-1.8*0.2) + & \\\ 0.9*(-0.5*0.2-0.6*0.2) &= 0 \end{aligned}

Lemma

AA is singular

b. With partial pivoting:

Ab=[0.10.30.91.30.30.92.73.90.60.70.11.4] R3R1Ab=[0.60.70.11.40.30.92.73.90.10.30.91.3] R212R1Ab=[0.60.70.11.400.552.653.20.10.30.91.3] R316R1Ab=[0.60.70.11.400.552.653.200.183333330.883333331.06666667] R313R2Ab=[0.60.70.11.400.552.653.20000.3]\begin{align} A|b &=\begin{bmatrix} 0.1 & 0.3 & 0.9 & | & 1.3\\ 0.3 & 0.9 & 2.7 & | & 3.9\\ 0.6 & 0.7 & 0.1 & | & 1.4 \end{bmatrix} \\\ R3 \leftrightarrow R1 \leftarrow A|b&=\begin{bmatrix} 0.6 & 0.7 & 0.1 & | & 1.4\\ 0.3 & 0.9 & 2.7 & | & 3.9\\ 0.1 & 0.3 & 0.9 & | & 1.3 \end{bmatrix} \\\ R2 - \frac{1}{2}R1 \leftarrow A|b&=\begin{bmatrix} 0.6 & 0.7 & 0.1 & | & 1.4\\ 0 & 0.55 & 2.65 & | & 3.2\\ 0.1 & 0.3 & 0.9 & | & 1.3 \end{bmatrix} \\\ R3 - \frac{1}{6}R1 \leftarrow A|b&=\begin{bmatrix} 0.6 & 0.7 & 0.1 & | & 1.4\\ 0 & 0.55 & 2.65 & | & 3.2\\ 0 & 0.18333333 & 0.88333333 & | & 1.06666667 \end{bmatrix} \\\ R3 - \frac{1}{3}R2 \leftarrow A|b&=\begin{bmatrix} 0.6 & 0.7 & 0.1 & | & 1.4\\ 0 & 0.55 & 2.65 & | & 3.2\\ 0 & 0 & 0 & | & -0.3 \end{bmatrix} \end{align}

We notice that R313R20=0.3R3-\frac{1}{3}R2 \rightarrow 0=-0.3, thus invalid.

c. With partial pivoting in double precision

The LULU decomposition of A=[0.10.30.90.30.92.70.60.70.1]A=\begin{bmatrix} 0.1 & 0.3 & 0.9\\ 0.3 & 0.9 & 2.7\\ 0.6 & 0.7 & 0.1 \end{bmatrix}

The following portray steps to calculate UU (lower triangular):

R3R1U=[0.60.70.10.30.92.70.10.30.9],P1=[001010100] R212R1U=[0.60.70.100.552.65000000000000040.10.30.9] R316R1U=[0.60.70.100.552.650000000000000400.183333333333333350.8833333333333333] R313R2U=[0.60.70.100.552.6500000000000004004.8109664400423476×1017]\begin{aligned} R_3 \leftrightarrow R_1 \rightarrow U &= \begin{bmatrix} 0.6 & 0.7 & 0.1\\ 0.3 & 0.9 & 2.7\\ 0.1 & 0.3 & 0.9 \end{bmatrix}, \quad P_1 = \begin{bmatrix} 0 & 0 & 1\\ 0 & 1 & 0\\ 1 & 0 & 0 \end{bmatrix} \\\ R_2 - \frac{1}{2}R_1 \rightarrow U &= \begin{bmatrix} 0.6 & 0.7 & 0.1\\ 0 & 0.55 & 2.6500000000000004\\ 0.1 & 0.3 & 0.9 \end{bmatrix} \\\ R_3 - \frac{1}{6}R_1 \rightarrow U &= \begin{bmatrix} 0.6 & 0.7 & 0.1\\ 0 & 0.55 & 2.6500000000000004\\ 0 & 0.18333333333333335 & 0.8833333333333333 \end{bmatrix} \\\ R_3 - \frac{1}{3}R_2 \rightarrow U &= \begin{bmatrix} 0.6 & 0.7 & 0.1\\ 0 & 0.55 & 2.6500000000000004\\ 0 & 0 & 4.8109664400423476 \times 10^{-17} \end{bmatrix} \end{aligned}

_note: the a33a_{33} is close to zero, hence consistent with previous finding_

L=[1000.5100.166666666666666690.333333333333333261]L=\begin{bmatrix} 1 & 0 & 0\\ 0.5 & 1 & 0\\ 0.16666666666666669 & 0.33333333333333326 & 1 \end{bmatrix}

To solve for xx with LULU decomposition, We solve L(Ux)=PbL(Ux)=Pb

x=[14.00699300699310.489510489510483.3846153846153832]\rightarrow x=\begin{bmatrix} 14.006993006993 & -10.48951048951048 & 3.3846153846153832\end{bmatrix}

d. Since A is singular, it doesn’t have an inverse. Matlab uses LU decomposition, and as we explored above, a pivot element is found to be zero or close to zero (matrix is probably ill-conditioned), which leads to 0x1+0x2+0x3=non negative value0x_1 + 0x_2 + 0x_3=\text{non negative value}, which results in NaN. For the second value -Inf, the division is small. Inf is due to division by zero


Problem 2 [2 points] Apply Gaussian elimination with partial pivoting on the following matrix A=[1000111001111011111111111]A=\begin{bmatrix} 1 & 0 & 0 & 0 & 1\\ −1 & 1 & 0 & 0 & 1\\ −1 & −1 & 1 & 0 & 1\\ −1 & −1 & −1 & 1 & 1\\ −1 & −1 & −1 & −1 & 1 \end{bmatrix}

Show all the steps.

Answer:

A=[1000111001111011111111111]A=\begin{bmatrix} 1 & 0 & 0 & 0 & 1\\ −1 & 1 & 0 & 0 & 1\\ −1 & −1 & 1 & 0 & 1\\ −1 & −1 & −1 & 1 & 1\\ −1 & −1 & −1 & −1 & 1 \end{bmatrix}

R2+R1 and R3+R1 and R4+R1 and R5+R1A=[1000101002011020111201112]R2+R1 \text{ and } R3+R1\text{ and } R4+R1\text{ and } R5+R1\rightarrow A=\begin{bmatrix} 1 & 0 & 0 & 0 & 1\\ 0 & 1 & 0 & 0 & 2\\ 0 & −1 & 1 & 0 & 2\\ 0 & −1 & −1 & 1 & 2\\ 0 & −1 & −1 & −1 & 2 \end{bmatrix}

R3+R2 and R4+R2 and R5+R2A=[1000101002001040011400114]R3+R2 \text{ and } R4+R2\text{ and } R5+R2\rightarrow A=\begin{bmatrix} 1 & 0 & 0 & 0 & 1\\ 0 & 1 & 0 & 0 & 2\\ 0 & 0 & 1 & 0 & 4\\ 0 & 0 & −1 & 1 & 4\\ 0 & 0 & −1 & −1 & 4 \end{bmatrix}

R4+R3 and R5+R3A=[1000101002001040001800018]R4+R3 \text{ and } R5+R3\rightarrow A=\begin{bmatrix} 1 & 0 & 0 & 0 & 1\\ 0 & 1 & 0 & 0 & 2\\ 0 & 0 & 1 & 0 & 4\\ 0 & 0 & 0 & 1 & 8\\ 0 & 0 & 0 & −1 & 8 \end{bmatrix}

R5+R4A=[10001010020010400018000016]R5+R4\rightarrow A=\begin{bmatrix} 1 & 0 & 0 & 0 & 1\\ 0 & 1 & 0 & 0 & 2\\ 0 & 0 & 1 & 0 & 4\\ 0 & 0 & 0 & 1 & 8\\ 0 & 0 & 0 & 0 & 16 \end{bmatrix}

Problem 3 [5 points] (a) (3 points) Let AA, BB, and CC be n×nn × n matrices, where BB and CC are nonsingular. For an nn-vector bb, describe how you would implement the formula x=C1(A+I)(A+B1)b.x = C^{-1} (A + I)(A + B^{−1})b. without computing any inverses. Here, II is the n×nn × n identity matrix.

(b) (2 points) What is the complexity of your approach in terms of big-O notation?

Answer:

a. Given BB and CC are non-singular

  1. Step 1: Using LULU decomposition of B, such that B=LUB=LU
  2. Step 2: Solve for yy in By=bBy=b (As y=B1by=B^{-1}b)
    1. solve for zz in Lz=bLz=b via forward substitution
    2. solve for yy in Uy=zUy=z via backward substitution
  3. Step 3: Compute z=(A+B1)bz=(A+B^{-1})b
    1. This becomes z=Ab+yz=Ab+y
  4. Step 4: Compute w=(A+I)zw = (A+I)z
    1. Via matrix multiplication w=Az+z\rightarrow w=Az + z
  5. Step 5: using LULU decomposition of C, such that C=LUC=LU
  6. Step 6: Solve for xx in Cx=wCx=w (As x=C1wx=C^{-1}w)
    1. Solve for zz' in Lz=wLz'=w via forward substitution
    2. Solve for xx in Ux=zUx=z' via backward substitution

With expansion, solved x=C1(A+I)(A+B1)b.x = C^{-1} (A + I)(A + B^{−1})b.

b. Complexity analysis

Let total_cost be the big-O notation

Step 1 using LULU decomposition of BB total_cost=O(n3)\rightarrow \text{total\_cost}=O(n^3)

Step 2 solving each Lz=bLz=b and Uy=zUy=z takes O(n2)O(n^2) each, thus solving Lz=bLz=b using LULU decomposition takes O(2n2)O(2n^2) total_cost=O(n3)+O(2n2)\rightarrow \text{total\_cost}=O(n^3) + O(2n^2)

Step 3 Compute z=(A+B1)bz=(A+B^{-1})b

  • MatmulOp of AbAb is O(n2)O(n^2)
  • AddOp of Ab+yAb+y is O(n)O(n)
  • Total for this step O(n2)+O(n)O(n^2) + O(n) total_cost=O(n3)+O(3n2)+O(n)\rightarrow \text{total\_cost}=O(n^3) + O(3n^2) + O(n)

Step 4 Compute w=(A+I)zw = (A+I)z

  • MatmulOp of AbAb is O(n2)O(n^2)
  • AddOp of Ab+yAb+y is O(n)O(n)
  • Total for this step O(n2)+O(n)O(n^2) + O(n) total_cost=O(n3)+O(4n2)+O(2n)\rightarrow \text{total\_cost}=O(n^3) + O(4n^2) + O(2n)

Step 5 using LULU decomposition of CC total_cost=O(2n3)+O(4n2)+O(2n)\rightarrow \text{total\_cost}=O(2n^3) + O(4n^2) + O(2n)

Step 6 solving each Lz=wLz'=w and Ux=zUx=z' using LU composition takes O(2n2)O(2n^2) total_cost=O(2n3)+O(6n2)+O(2n)\rightarrow \text{total\_cost}=O(2n^3) + O(6n^2) + O(2n)


Problem 4 [6 points] An n×nn × n Hilbert matrix, denote it by HH, has entries hij=1(i+j1),i,j=1,...,n.h_{ij} = \frac{1}{(i+j-1)}, i, j = 1, . . . , n.

For n=2,3,...n = 2, 3, . . . , generate the Hilbert matrix of order nn, and also generate the nn-vector b=Hxb = Hx, where xx is a random vector. Solve the resulting system Hx=bHx = b to obtain an approximate solution x^\hat{x}. (See the functions hilb and rand.)

(a) [2 points] How large can you take nn before the error x^xx\frac{\Vert{\hat{x} - x}\Vert}{\Vert{x}\Vert} is 100 percent?

(b) [2 points] For nn up to the value you find in (a), report rb\frac{\Vert{r}\Vert}{\Vert{b}\Vert} , where r=bHx^r = b − H\hat{x}, and x^xx\frac{\Vert{\hat{x} - x}\Vert}{\Vert{x}\Vert}.

(c) [2 points] As nn increases, how does the number of correct digits in the computed solution relate to the condition number of the matrix? See the cond function.

Submit your Matlab program producing the above results. Name the Matlab file hilb_problem.m.

Answer:

The following hilb_problem.m is used:

hilb_problem.m
function hilb_problem()
    n = 1;
    while true
        % Generate Hilbert matrix of order n
        H = hilb(n);
 
        % Generate random vector x
        x = rand(n, 1);
 
        % Compute b = Hx
        b = H * x;
 
        % Solve the system Hx = b
        x_hat = H \ b;
 
        % Compute the relative error
        error = norm(x_hat - x) / norm(x);
        fprintf("error=%d, n=%d\n", error, n)
        % If the error is 100 percent, break
        if error >= 1
            break;
        end
 
        n = n + 1;
    end
 
    fprintf('\n=============\n\nThe largest n before the error is 100 percent is: %d\n\n=============\n', n-1);
 
    for i = 1:n-1
        H = hilb(i);
        x = rand(i, 1);
        b = H * x;
        x_hat = H \ b;
 
        r = b - H * x_hat;
        rel_resid = norm(r) / norm(b);
        rel_error = norm(x_hat - x) / norm(x);
 
        %fprintf('%d %.16f\n',i, rel_resid)
        fprintf('| %d | %.32f | %.32f |\n', i, rel_resid, rel_error);
    end
 
    cond_num = cond(H);
    fprintf('The condition number of the matrix for n = %d is: %f\n', n-1, cond_num);
end

a. largest n=12n=12 before the error x^xx\frac{\Vert{\hat{x} - x}\Vert}{\Vert{x}\Vert} is 100 percent.

b. The following entails the value of rb\frac{\Vert{r}\Vert}{\Vert{b}\Vert} and x^xx\frac{\Vert{\hat{x} - x}\Vert}{\Vert{x}\Vert}

nrb\frac{\Vert{r}\Vert}{\Vert{b}\Vert}x^xx\frac{\Vert{\hat{x} - x}\Vert}{\Vert{x}\Vert}
10.000000000000000000000000000000000.00000000000000000000000000000000
20.000000000000000000000000000000000.00000000000000013220372219891702
30.000000000000000000000000000000000.00000000000000363350625815651572
40.000000000000000000000000000000000.00000000000006709266750580992637
50.000000000000000077339751176242870.00000000000747821082933078000054
60.000000000000000139342075067363820.00000000023960543432895825359428
70.000000000000000106605703983710850.00000000837749558262967895463873
80.000000000000000071655651845704070.00000009992506975169996005028294
90.000000000000000070765498384471140.00000608952488692639798140973303
100.000000000000000126628405307077190.00002450986238666613242472361311
110.000000000000000119976337808137890.00379971054180424641297242338567
120.000000000000000065033380665053650.25404291536273732043937911839748

c. As nn increases, the condition number increases, which means the matrix becomes more ill-conditioned. This means fewer digits in the computed solution are correct.

IMPORTANT

The number of correct digits in the computed solution decreases due to the increase in the condition number as nn increases


Problem 5 [4 points] You have to interpolate sin(x)sin(x) by a polynomial of degree five using equally spaced points in [0, 1].

(a) [2 points] What (absolute) error would you expect if you use this polynomial?

(b) [2 points] Using equally spaced points, what degree polynomial would you use to achieve a maximum error of 10810^{-8}?

Answer:

a. Interpolate sin(x)sin(x) by a polynomial of degree five using equally spaced on in [0,1][0,1], Error as follow

f(x)pn(x)=E(x)=fn+1(ξ)(n+1)!i=0n(xxi)f(x) - p_n(x) = E(x) = \frac{f^{n+1}(\xi)}{(n+1)!}\prod_{i=0}^{n}{(x-x_i)}

where

  • nn is the degree of the polynomial (n=5n=5)
  • fn+1(ξ)f^{n+1}(\xi) is (n+1)-th(n+1)\text{-th} derivate of ff

Derivate of sin(x)sin(x) every 4 terms is sin(x),cos(x),sin(x),cos(x)sin(x), cos(x), -sin(x), -cos(x). Therefore the 6th derivative is cos(x)-cos(x)

Here h=ban=15h=\frac{b-a}{n}=\frac{1}{5} and M=max0t1cos(t)=1cos(1)=2sin2(12)M = max_{0\leq t\leq 1}|-cos(t)| = 1 - cos(1) = 2sin^2(\frac{1}{2})

Therefore E(x)=f(x)sin(x)M4(n+<