Problem 1 [8 points] Consider the system , where and

a. [2 points] Show that 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 to be singular, prove

Using Gaussian elimination without partial pivoting

Lemma

is singular

b. With partial pivoting:

We notice that , thus invalid.

c. With partial pivoting in double precision

The decomposition of

The following portray steps to calculate (lower triangular):

note: the is close to zero, hence consistent with previous finding

To solve for with decomposition, We solve

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 , 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

Show all the steps.

Answer:

Problem 3 [5 points] (a) (3 points) Let , , and be matrices, where and are nonsingular. For an vector , describe how you would implement the formula without computing any inverses. Here, is the identity matrix.

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

Answer:

a. Given and are non-singular

  1. Step 1: Using decomposition of B, such that
  2. Step 2: Solve for in (As )
    1. solve for in via forward substitution
    2. solve for in via backward substitution
  3. Step 3: Compute
    1. This becomes
  4. Step 4: Compute
    1. Via matrix multiplication
  5. Step 5: using decomposition of C, such that
  6. Step 6: Solve for in (As )
    1. Solve for in via forward substitution
    2. Solve for in via backward substitution

With expansion, solved

b. Complexity analysis

Let total_cost be the big-O notation

Step 1 using decomposition of

Step 2 solving each and takes each, thus solving using decomposition takes

Step 3 Compute

  • MatmulOp of is
  • AddOp of is
  • Total for this step

Step 4 Compute

  • MatmulOp of is
  • AddOp of is
  • Total for this step

Step 5 using decomposition of

Step 6 solving each and using LU composition takes


Problem 4 [6 points] An Hilbert matrix, denote it by , has entries

For , generate the Hilbert matrix of order , and also generate the vector , where is a random vector. Solve the resulting system to obtain an approximate solution . (See the functions hilb and rand.)

(a) [2 points] How large can you take before the error is 100 percent?

(b) [2 points] For up to the value you find in (a), report , where , and .

(c) [2 points] As 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 before the error is 100 percent.

b. The following entails the value of and

n
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 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 increases


Problem 5 [4 points] You have to interpolate 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 ?

Answer:

a. Interpolate by a polynomial of degree five using equally spaced on in , Error as follow

where

  • is the degree of the polynomial ()
  • is derivate of

Derivate of every 4 terms is . Therefore the 6th derivative is

Here and

Therefore

b. To achieve maximum error of , We have

derivative of cycles every 4 term, thus the max value of over is 1

Thus we need to solve for in

Hence considering to use polynomial degree seven to achieve the desired error bound.


Problem 6 [3 points] You are given the values of at three points

x149
123

(a) [2 points] Construct the interpolating polynomial interpolating these data.

(b) [1 points] Using this polynomial, approximate .

Answer:

a. To construct the interpolating polynomial for these data, we will use Lagrange basis

where is the Lagrange basis polynomial, defined as

With , and data point

IMPORTANT

The interpolating polynomial

b. The approximation of


Problem 7 [7 points] Let . Interpolate this function over using

(a) [2 points] polynomial interpolation of degree at equally spaced points. Then evaluate this polynomial at equally spaced points. Denote the interpolating polynomial by . Plot

  • and versus at the interpolation points and at the points (on the same plot);
  • versus at the 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 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

implementation in matlab are as follow:

f = @(x) sin(x)./((1 + 20*x).^2);

a. The following is a snippet of interp_problem.m for polynomial interpolation of degree

% (a) Polynomial interpolation of degree n = 15 at equally spaced points
 
% Define the number of interpolation points and the degree of the polynomial
n = 15;
N = 100;
 
% Generate n+1 equally spaced points in the interval [-1, 1]
x = linspace(-1, 1, n+1);
y = f(x);
 
% Interpolate using polyfit
p_coeff = polyfit(x, y, n);
 
% Evaluate the interpolating polynomial at N equally spaced points
x_N = linspace(-1, 1, N);
p_N = polyval(p_coeff, x_N);
 
% Plot f(x) and p(x) on the same graph
figure;
plot(x_N, f(x_N), 'b-', x_N, p_N, 'r--', x, y, 'go');
legend('f(x)', 'p(x)', 'Interpolation Points');
title('f(x) and p(x) vs. x');
xlabel('x');
ylabel('y');
 
% Plot the absolute error |f(x) - p(x)| at the N points
figure;
plot(x_N, abs(f(x_N) - p_N), 'm-');
title('Absolute Error |f(x) - p(x)| vs. x');
xlabel('x');
ylabel('Error');

b. The following is a snippet of interp_problem.m for Cheybyshev points

% (b) Polynomial interpolation using Chebyshev points
 
% Generate Chebyshev points in the interval [-1, 1]
x_cheb = cos((2*(1:n+1)-1)*pi/(2*n));
y_cheb = f(x_cheb);
 
% Interpolate using polyfit
p_cheb_coeff = polyfit(x_cheb, y_cheb, n);
 
% Evaluate the interpolating polynomial at N equally spaced points
p_cheb_N = polyval(p_cheb_coeff, x_N);
 
% Plot f(x) and p(x) using Chebyshev points on the same graph
figure;
plot(x_N, f(x_N), 'b-', x_N, p_cheb_N, 'r--', x_cheb, y_cheb, 'go');
legend('f(x)', 'p(x) with Chebyshev', 'Interpolation Points');
title('f(x) and p(x) with Chebyshev vs. x');
xlabel('x');
ylabel('y');
 
% Plot the absolute error |f(x) - p(x)| using Chebyshev points at the N points
figure;
plot(x_N, abs(f(x_N) - p_cheb_N), 'm-');
title('Absolute Error |f(x) - p(x) with Chebyshev| vs. x');
xlabel('x');
ylabel('Error');

c. The following is a snippet of interp_problem.m through spline interpolation at equally spaced points.

% (c) Spline interpolation at n+1 equally spaced points
 
% Evaluate the function at n+1 equally spaced points
y_spline = f(x);
 
% Use the spline function to get the piecewise polynomial representation
pp = spline(x, y_spline);
 
% Evaluate the spline at N equally spaced points
spline_N = ppval(pp, x_N);
 
% Plot f(x) and the spline on the same graph
figure;
plot(x_N, f(x_N), 'b-', x_N, spline_N, 'r--', x, y_spline, 'go');
legend('f(x)', 'spline(x)', 'Interpolation Points');
title('f(x) and spline(x) vs. x');
xlabel('x');
ylabel('y');
 
% Plot the absolute error |f(x) - spline(x)| at the N points
figure;
plot(x_N, abs(f(x_N) - spline_N), 'm-');
title('Absolute Error |f(x) - spline(x)| vs. x');
xlabel('x');
ylabel('Error');

d. Discussion

  1. 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.
  2. Polynomial interpolation using Chebyshev points should mitigate the oscillations
  3. 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 , 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:

The linear system as follow

Solving this system to obtain the

NOTE

Thus monomial basis of this polynomial of degree two is

b. Lagrange basis

The Lagrange basis for a polynomial of degree two is given by: where

Thus

NOTE

Thus Lagrange basis of this polynomial of degree two is

c. Newton basis

The Newton basis for a polynomial of degree two is given by: where

We have

Thus

NOTE

Thus Newton basis of this polynomial of degree two is

Therefore, we prove that all three basis yield the same polynomial for degree two.