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 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=1−1−1−1−101−1−1−1001−1−10001−111111
Show all the steps.
Answer:
A=1−1−1−1−101−1−1−1001−1−10001−111111
R2+R1 and R3+R1 and R4+R1 and R5+R1→A=1000001−1−1−1001−1−10001−112222
R3+R2 and R4+R2 and R5+R2→A=1000001000001−1−10001−112444
R4+R3 and R5+R3→A=1000001000001000001−112488
R5+R4→A=10000010000010000010124816
Problem 3 [5 points]
(a) (3 points) Let A, B, and C be n×n matrices, where B and C are nonsingular. For an n−vector b, describe how you would implement the formula x=C−1(A+I)(A+B−1)b. without computing any inverses. Here, I is the n×n identity matrix.
(b) (2 points) What is the complexity of your approach in terms of big-O notation?
Answer:
a. Given B and C are non-singular
Step 1: Using LU decomposition of B, such that B=LU
Step 2: Solve for y in By=b (As y=B−1b)
solve for z in Lz=b via forward substitution
solve for y in Uy=z via backward substitution
Step 3: Compute z=(A+B−1)b
This becomes z=Ab+y
Step 4: Compute w=(A+I)z
Via matrix multiplication→w=Az+z
Step 5: using LU decomposition of C, such that C=LU
Step 6: Solve for x in Cx=w (As x=C−1w)
Solve for z′ in Lz′=w via forward substitution
Solve for x in Ux=z′ via backward substitution
With expansion, solved x=C−1(A+I)(A+B−1)b.
b. Complexity analysis
Let total_cost be the big-O notation
Step 1 using LU decomposition of B→total_cost=O(n3)
Step 2 solving each Lz=b and Uy=z takes O(n2) each, thus solving Lz=b using LU decomposition takes O(2n2)→total_cost=O(n3)+O(2n2)
Step 3 Compute z=(A+B−1)b
MatmulOp of Ab is O(n2)
AddOp of Ab+y is O(n)
Total for this step O(n2)+O(n)→total_cost=O(n3)+O(3n2)+O(n)
Step 4 Compute w=(A+I)z
MatmulOp of Ab is O(n2)
AddOp of Ab+y is O(n)
Total for this step O(n2)+O(n)→total_cost=O(n3)+O(4n2)+O(2n)
Step 5 using LU decomposition of C→total_cost=O(2n3)+O(4n2)+O(2n)
Step 6 solving each Lz′=w and Ux=z′ using LU composition takes O(2n2)→total_cost=O(2n3)+O(6n2)+O(2n)
Problem 4 [6 points] An n×n Hilbert matrix, denote it by H, has entries
hij=(i+j−1)1,i,j=1,...,n.
For n=2,3,... , generate the Hilbert matrix of order n, and also generate the n−vector b=Hx, where x is a random vector. Solve the resulting system Hx=b to obtain an
approximate solution x^. (See the functions hilb and rand.)
(a) [2 points] How large can you take n before the error ∥x∥∥x^−x∥ is 100 percent?
(b) [2 points] For n up to the value you find in (a), report ∥b∥∥r∥ , where r=b−Hx^, and ∥x∥∥x^−x∥.
(c) [2 points] As n 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:
a. largest n=12 before the error ∥x∥∥x^−x∥ is 100 percent.
b. The following entails the value of ∥b∥∥r∥ and ∥x∥∥x^−x∥
n
∥b∥∥r∥
∥x∥∥x^−x∥
1
0.00000000000000000000000000000000
0.00000000000000000000000000000000
2
0.00000000000000000000000000000000
0.00000000000000013220372219891702
3
0.00000000000000000000000000000000
0.00000000000000363350625815651572
4
0.00000000000000000000000000000000
0.00000000000006709266750580992637
5
0.00000000000000007733975117624287
0.00000000000747821082933078000054
6
0.00000000000000013934207506736382
0.00000000023960543432895825359428
7
0.00000000000000010660570398371085
0.00000000837749558262967895463873
8
0.00000000000000007165565184570407
0.00000009992506975169996005028294
9
0.00000000000000007076549838447114
0.00000608952488692639798140973303
10
0.00000000000000012662840530707719
0.00002450986238666613242472361311
11
0.00000000000000011997633780813789
0.00379971054180424641297242338567
12
0.00000000000000006503338066505365
0.25404291536273732043937911839748
c. As n 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 n increases
Problem 5 [4 points] You have to interpolate 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 10−8?
Answer:
a. Interpolate sin(x) by a polynomial of degree five using equally spaced on in [0,1], Error as follow
f(x)−pn(x)=E(x)=(n+1)!fn+1(ξ)∏i=0n(x−xi)
where
n is the degree of the polynomial (n=5)
fn+1(ξ) is (n+1)-th derivate of f
Derivate of sin(x) every 4 terms is sin(x),cos(x),−sin(x),−cos(x). Therefore the 6th derivative is −cos(x)
Here h=nb−a=51 and M=max0≤t≤1∣−cos(t)∣=1−cos(1)=2sin2(21)
The interpolating polynomial P(x)=24(x−4)(x−9)+152(x−1)(9−x)+403(x−4)(x−1)
b. The approximation of P(1.5)=24(1.5−4)(1.5−9)+152(1.5−1)(9−1.5)+403(1.5−4)(1.5−1)=1.1875
Problem 7 [7 points] Let f(x)=(1+20x)2sin(x). Interpolate this function over x∈[−1,1] using
(a) [2 points] polynomial interpolation of degree n=15 at equally spaced points.
Then evaluate this polynomial at N=100 equally spaced points.
Denote the interpolating polynomial by p(x). Plot
f(x) and p(x) versus x at the interpolation points and at the N points (on the same plot);
∣f(x)−p(x)∣ versus x at the N points.
You can use the polyfit function. See the linspace function.
(b) [2 points] Repeat (a) but now using Chebyshev points.
(c) [2 points] Repeat (a) but now using spline interpolation at n+1 equally spaced points. See the spline function.
(d) [1 points] Discuss the accuracies of your results.
Submit your plots (6 in total) and the Matlab code producing them. Name your Matlab file interp_problem.m.
Answer
f(x) implementation in matlab are as follow:
a. The following is a snippet of interp_problem.m for polynomial interpolation of degree n=15
b. The following is a snippet of interp_problem.m for Cheybyshev points
c. The following is a snippet of interp_problem.m through spline interpolation at n+1 equally spaced points.
d. Discussion
The polynomial interpolation using equally spaced points might show oscillations near endpoints due to Runge phenomenon (oscillations near the endpoints of the interpolated interval become pronounced). We saw oscillation in the error graph here.
Polynomial interpolation using Chebyshev points should mitigate the oscillations
The spline interpolation will provide a piecewise polynomial that should fit the function smoothly and might offer better accuracy than polynomial interpolation
Problem 8 [4 points] Given the three data points (−1,1),(0,0),(1,1), determine the interpolating polynomial of degree two using:
a. [1 point] monomial basis
b. [1 point] Lagrange basis
c. [1 point] Newton basis
[1 point] Show that the three representations give the same polynomial.
Answer:
a. Monomial basis
The monomial basis for a polynomial of degree two is given by: p(x)=a0+a1∗x+a2∗x2
The linear system as follow
a0−a1+a2=1a0=0a0+a1+a2=1
Solving this system to obtain the a0=0,a1=0,a2=1
NOTE
Thus monomial basis of this polynomial of degree two is p(x)=x2
b. Lagrange basis
The Lagrange basis for a polynomial of degree two is given by: p(x)=∑j=02yjLj(x)=f(x0)L0(x)+f(x1)L1(x)+f(x2)L2(x)
where
L0(x)=(x0−x1)(x0−x2)(x−x1)(x−x2)=2x(x−1)L1(x)=(x1−x0)(x1−x2)(x−x0)(x−x2)=−x(x−1)L2(x)=(x2−x0)(x2−x1)(x−x0)(x−x1)=2x(x+1)
Thus p(x)=1∗2x(x−1)+0∗(−x(x−1))+2x(x+1)=x2
NOTE
Thus Lagrange basis of this polynomial of degree two is p(x)=x2
c. Newton basis
The Newton basis for a polynomial of degree two is given by: p(x)=f(x0)+(x−x0)f[x0,x1]+(x−x0)(x−x1)f[x0,x1,x2]
where
f[x0,x1]=x1−x0f(x1)−f(x0)=0+10−1=−1f[x0,x1,x2]=x2−x0f[x1,x2]−f[x0,x1]=1+11+1=1
We have f[x1,x2]=x2−x1f(x2)−f(x1)=1−01−0=1
Thus p(x)=1+(x+1)(−1)+(x+1)(x)∗2=1−x−1+(x2+x)=x2
NOTE
Thus Newton basis of this polynomial of degree two is p(x)=x2
Therefore, we prove that all three basis yield the same polynomial for degree two.