0% found this document useful (0 votes)
17 views19 pages

HW 4 Questions

Uploaded by

engrkumailabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views19 pages

HW 4 Questions

Uploaded by

engrkumailabbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Homework 4  Graded

Student
SHAYA MASHHOOR ALHARTHI
 View or edit group

Total Points
24 / 50 pts
Question 1
(no title) 14 / 19 pts

1.1 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.2 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.3 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.4 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.5 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.6 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.7 (no title) 2 / 2 pts

 + 2 pts Correct

+ 0 pts Incorrect

1.8 (no title) 0 / 2 pts

+ 2 pts Correct

 + 0 pts Incorrect

1.9 (no title) 0 / 1 pt


+ 1 pt Correct

 + 0 pts Incorrect
1.10 (no title) 0 / 2 pts
+ 2 pts Correct

 + 0 pts Incorrect

Question 2
(no title) 4 / 10 pts
− 0 pts Correct

− 1 pt Computational error

 − 2 pts Produced an O (h 3 ) formula instead of h 4 .

− 2 pts Used the wrong set of points for the formula.

 − 4 pts Incorrectly set up equations for coefficients

− 10 pts No submission

 Two things: first, you are missing a factor of h in the denominator of each of your coefficients. You produced a
formula for hf ′ (x 0 ) , not f ′ (x 0 ) .
​ ​

Second, you produced a system of 5 equations in 4 unknowns, where one generally cannot expect a solution. The
coefficients you found are not a solution to the system of equations you derived either.

Question 3
(no title) 5 / 10 pts
− 0 pts Correct

− 1 pt Did not describe optimal h 's dependence on x 0 .

 − 5 pts Incorrect round-off error form

− 10 pts No Submission

 The round-off error should just be 2ϵ


h ; there should not be a factor of f .

In addition, using the approximation f ′′ (ξ 0 ) ≈ f ′′ (x 0 ) is not a good approximation because the f ′′ is multiplied
​ ​

by h ; when x 0 = 2 , for instance, you are asserting that the truncation error is essentially zero, even for very large
values of h (which is what your formula asserts is the optimum). Instead, you need to use the bound ∣f ′′ (ξ 0 )∣ ≤

M = supx∈[x0 ,x0 +h] ∣f ′′ (x )∣, substitute in this upper bound, and optimise in h .
​ ​

Question 4
(no title) 1 / 11 pts
− 0 pts Correct

 − 1 pt Incorrect error term

− 2 pts MATLAB Implementation issue

− 3 pts Incorrect central numerical differentiation coefficients

− 3 pts Incorrect one-sided numerical differentiation coefficients

− 3 pts Used first-order equation for endpoints

− 4 pts Did not differentiate at endpoints

 − 9 pts Did not submit DiffMatrixOrder2.m

− 11 pts No Submission

 First, the error term should have a factor of 6 instead of a factor of 2 from the Taylor expansion. Second, you don't
seem to have uploaded `DiffMatrixOrder2.m`, and the graph of the error suggests that you have done something
drastically wrong. Without being able to look at your code, I can't award any partial credit.
Q1
19 Points

Q1.1
2 Points

A foruth-degree polynomial P (x) satisfies Δ4 P (0)


= 24, Δ3 P (0) = 6,
Δ2 P (0) = 0, where ΔP (x) = P (x + 1) − P (x). Compute Δ2 P (10)
144
240
1000
1140

Q1.2
2 Points

The polynomial p(x) satisfying p(0) = 1, p(2) = 1, p′ (0) = 1, p′ (2) = 3 is


3x3 9x2
2
​ − 2
+ 3x + 1

3 7x2
x − 2
+ 3x + 1

x3 3x2
2
​ − 2
​+x+1
3 5x2
x − 2
+x+1

Remark For Hermite interpolation, you need to know the construction using
the basis function (Theorem 3.9 in the textbook) and the divided difference
method.
Q1.3
2 Points

Consider a polynomial P (x) that interpolates the data


(0, f(0)), (0, f ′ (0)), (h, f(h)), (h, f ′ (h)). Assume any order derivative of f(x)
is a bounded function. Which of the following best describes the order of the
error f(x) − P (x) for x ∈ [0, h]?

O(h2 )
O(h3 )
O(h4 )
O(h5 )

Q1.4
2 Points

Determine the natural cubic spline S that interpolates the data f(0) =
0, f(1) = 1, f(2) = 2
x3 3x2
2
​ − 2
​ + 2x
2
x −1
x
3x − x2

Q1.5
2 Points

A natural cubic spline S on [0, 2] is defined by

S(x) = {
S0 (x) = 1 + 2x − x3 ,
​ 0≤x<1
S1 (x) = 2 + b(x − 1) + c(x − 1)2 + d(x − 1)3 , 1≤x≤2
​ ​

Find b, c and d.

b = 1, c = 2, d = −1
b = −1, c = −3, d = −1
b = −1, c = −3, d = 1
b = 1, c = −3, d = 1
Q1.6
2 Points

A natural cubic spline S on [2, 3] is defined by

S(x) = {
S0 (x) = 1 + B(x − 1) − D(x − 1)3 ,
​ 1≤x<2
S1 (x) = 1 + b(x − 2) − 34 (x − 2)2 + d(x − 2)3 , 2≤x≤3
​ ​

​ ​

If S interpolates the data (1,1), (2,1), and (3,0), find B, D, b, and d.


b = 14 , d = 14 , B = −1, D = 1
​ ​

b = 14 , d = 12 , B = −1, D =
​ ​
1
4

b = − 12 , d = 14 , B = 14 , D =
​ ​ ​
1
4

b = 1, d = 12 , B = 14 , D =
​ ​
1
4

Q1.7
2 Points

A clamped cubic spline S for a function f is defined on [2, 3] by

S(x) = {
S0 (x) = 3(x − 1) + 2(x − 1)2 − (x − 1)3 ,
​ 1≤x<2
S1 (x) = a + b(x − 2) + c(x − 2)2 + d(x − 2)3 , 2≤x≤3
​ ​

Given f ′ (1) = f ′ (3), find a, b, c, and d.


a = 14 , b = 14 , c = −1, d = 1
​ ​

1
a = 4, b = 4, c = −1, d = 3 ​

a = 14 , b = 4, c = −1, d = 1

a = 4, b = 4, c = −1, d = 1
Q1.8
2 Points

Suppose that N(h) is an approximation to M for every h > 0 and that M =


N(h) + K1 h + K2 h2 + K3 h3 + ... for some constant K1 , K2 , K3 , .... Use the
​ ​ ​ ​ ​ ​

values N(h) = 1, N( h2 ) = 32 to produce an O(h2 ) approximation to M


​ ​

1
3
2

2
5
2

None of the above

Q1.9
1 Point

Suppose that N(h) is an approximation to M for every h > 0 and that M =


2 3
N(h) + K1 h + K2 h + K3 h + ... for some constant K1 , K2 , K3 , .... Use the
​ ​ ​ ​ ​ ​

values N(h) = 1, N( h2 ) = 32 , N( h4 ) = 2 to produce an O(h3 ) approximation to


​ ​ ​

M
5
3

8
3

5
2

None of the above


Q1.10
2 Points

Suppose that N(h) is an approximation to M for every h > 0 and that M =


N(h) + K2 h2 + K3 h3 + ... for some constant K2 , K3 , .... Consider the linear
​ ​ ​ ​

interpolating polynomial P (h) through (h2 , N(h)), (h2 /4, N(h/2))), Which of
the following best describes the order of the error M − P (0)?

O(h)
O(h2 )
O(h3 )
O(h4 )
Q2
10 Points

Derive an O(h4 ) five-point formula to approximate f ′ (x0 ) that uses f(x0


​ ​ −
h), f(x0 ), f(x0 + h), f(x0 + 2h), f(x0 + 3h).
​ ​ ​

Hint
Method 1: construct a polynomial interpolation, take the derivative, and then
evaluate the derivative at x = x0 .​

Method 2: consider the expression Af(x0 ​ − h) + Bf(x0 ) + Cf(x0 + h) +


​ ​

Df(x0 + 2h) + Ef(x0 + 3h). Expand in the fourth Taylor polynomials and
​ ​

choose A, B, C, and D such that the coefficient of h2 , h3 , h4 are zero, and the
constant term is zero, the coefficient of h is 1.
 Q2 (3).pdf  Download

Your browser does not support PDF previews. You can download the file instead.
Q3
10 Points

f (x0 + h)−f (x0 )


1 Analyze the round-off error for the formula f ′ (x0 ) = − h2 f ′′ (ξ0 )
​ ​

h
​ ​ ​ ​

.
2 Find the optimal h > 0 for the function f(x) = xex .

 Q3 (2).pdf  Download

Your browser does not support PDF previews. You can download the file instead.

Remark
1 Check "illustration" in section 4.1 in the textbook, pages 178-180.
2 The optimal h may depend on x0 .
3 We only need to consider the second order derivatives on [x0 , x0 ​ + h]. Why?
Q4
11 Points

In this question, we will implement a numerical differentiation method and


verify its convergence rate.

First, we have a function DiffMatrix that produces a differentiation matrix in the


script DiffMatrix.m .

function D=DiffMatrix(N,h)
% Vector to hold row indices of non-zero elements
row_indices =[1:N,1:N-1,N];
% Vector to hold column indices of non-zero elements
col_indices =[1:N,2:N,N-1];
% Vector to hold values of non-zero elements
values = [-ones(1,N-1),1, ones(1,N-1),-1];
%construct a sparse matrix
D = sparse(row_indices, col_indices, values, N, N)/h;%
end

The following code is an example of using the matrix

% Set the number of points


N = 5;

% Generate N equally spaced points in the interval [0, 1]


x = linspace(0, 1, N)';

% Calculate the step size


h = x(2) - x(1);

% Generate the differentiation matrix using the DiffMatrix function


D = DiffMatrix(N, h);

% Display the full differentiation matrix


full(D);

% Define the function fun and its first derivative funx


fun = @(x) sin(x); % Original function
funx = @(x) cos(x); % First derivative

% Evaluate the function at the generated points


y = fun(x);

% Evaluate the exact derivative at the generated points


dyexact = funx(x);

% Compute the numerical derivative using the differentiation matrix D


dy = D * y;

% Calculate the maximum error between the numerical and exact derivatives
error = max(abs(dy - dyexact));
Run the code will display

ans =

-4 4 0 0 0
0 -4 4 0 0
0 0 -4 4 0
0 0 0 -4 4
0 0 0 -4 4

error =

0.0990

Here, we use uniform grid points to compute derivatives. For the first N-1
points, we use the function value at x = xi , x = xi+1 to compute the derivative
​ ​

at x = xi . For the last point xN , we use the function value at x = xN −1 , x =


​ ​ ​

xN to compute the derative.

From the output of full(D) , we observe that the matrix contains many zeros. This
type of matrix is called a sparse matrix. By focusing on storing only the non-zero
elements, significant memory savings and computational efficiency can be
achieved. That’s why we use the command
D = sparse(row_indices, col_indices, values, N, N)/h; to construct the matrix in the function

DiffMatrix .

Next, let’s consider the script TestDiffMatrix.m . This script computes the derivative
of a given function using different step sizes and compares it with the exact
value of the derivative. The error is typically proportional to the power of the
step size h,, denoted as ha . In a log-log plot, this relationship appears as a
straight line with a slope of a. Therefore, we often use log-log plots to display
the error.

% Define the function fun and its first and second derivatives
fun = @(x) sin(x); % Original function
funx = @(x) cos(x); % First derivative
funxx = @(x) -sin(x); % Second derivative

% Create a list of N values (number of points)


Nlist = 2.^[3:10] + 1; % Powers of 2 from 8 to 1024, incremented by 1

% Initialize arrays for step sizes and errors


hlist = zeros(size(Nlist)); % Step sizes
errorlist = zeros(size(Nlist)); % Errors

% Loop over each N value


for indi = 1:length(Nlist)
N = Nlist(indi); % Current number of points
x = linspace(0, 1, N)'; % Equally spaced points in [0, 1]
hlist(indi) = max(x(2:end) - x(1:end-1)); % Calculate step size
D = DiffMatrix(N, hlist(indi)); % Calculate differentiation matrix
y = fun(x); % Evaluate function at points
dyexact = funx(x); % Exact derivative values
dy = D * y; % Numerical derivative using matrix D
errorlist(indi) = max(abs(dy - dyexact)); % Calculate error
end

% Plot numerical error vs step size


loglog(hlist, errorlist, '-r'); % Numerical error in red
hold on;

% Plot theoretical prediction of the error


loglog(hlist, max(abs(funxx(x))) / 2 * hlist, '--b'); % Theoretical error in blue
hold off;

% Add legend with more descriptive names


legend('Computed Derivative Error', 'Theoretical Error Bound', 'location', 'southeast');

% Save the figure as a JPG file


saveas(gcf, 'error.jpg');

The script produces the following figure

The blue curve perfectly overlaps with the red curve, indicating the numerical
error agrees with the theoretical error bound.
Instruction
Please write a function D=DiffMatrixOrder2(x) in the script DiffMatrixOrder2.m . The
function takes a vector x that contains N grid points as the input and returns a
matrix D. We expect matrix D can produce the derivative in the following use
case.

y = fun(x);
dy = D * y;

We assume the grid points are nonuniform. To compute the derivative at x =


x0 , we use the function value at x = x0 , x1 , x2 . If 1 ≤ i ≤ N − 1, we use the
​ ​

function value at x = xi−1 , xi , xi+1 to compute the derivative at x = xi . To


​ ​ ​

compute the derivative at x = xN , we use the function value at x =


xN −2 , xN −1 , xN . We expect that this is a second-order method, meaning the


​ ​

error is O(h2 ).

Please save the following script as TestDiffMatrixOrder2.m and use it to test the
function DiffMatrixOrder2 .

% Define the function fun and its first and second derivatives
fun = @(x) sin(x); % Original function
funx = @(x) cos(x); % First derivative
funxx = @(x) -sin(x); % Second derivative
funxxx = @(x) cos(x); % Second derivative

% Create a list of N values (number of points)


Nlist = 2.^[3:10] + 1; % Powers of 2 from 8 to 1024, incremented by 1

% Initialize arrays for step sizes and errors


hlist = zeros(size(Nlist)); % Step sizes
errorlist = zeros(size(Nlist)); % Errors

% Loop over each N value


for indi = 1:length(Nlist)
N = Nlist(indi); % Current number of points
x = cos(linspace(pi, 0, N))'; % nonuniformly distributed points in [-1, 1]
hlist(indi) = max(x(2:end) - x(1:end-1)); % Calculate maximum step size
D = DiffMatrixOrder2(x); % Calculate differentiation matrix
y = fun(x); % Evaluate function at points
dyexact = funx(x); % Exact derivative values
dy = D * y; % Numerical derivative using matrix D
errorlist(indi) = max(abs(dy - dyexact)); % Calculate error
end

% Plot numerical error vs step size


loglog(hlist, errorlist, '-r'); % Numerical error in red
hold on;

% Plot theoretical prediction of the error


loglog(hlist, max(abs(funxx(x))) / 2 * hlist, '--b'); % Theoretical error in blue
hold off;

% Add legend with more descriptive names


legend('Computed Derivative Error', 'Theoretical Error Bound', 'location', 'southeast');

% Save the figure as a JPG file


saveas(gcf, 'error.jpg');

Please replace loglog(hlist, max(abs(funxx(x))) / 2 * hlist, '--b'); with the correct theoretical
error bound.

Please upload DiffMatrixOrder2.m and TestDiffMatrixOrder2.m the screenshot of the


output file error.jpg from TestDiffMatrixOrder2.m
 TestDiffMatrixOrder2.m  Download

1 % Define the function fun and its first and second derivatives
2 fun = @(x) sin(x); % Original function
3 funx = @(x) cos(x); % First derivative
4 funxx = @(x) -sin(x); % Second derivative
5 funxxx = @(x) cos(x); % Third derivative
6
7 % Create a list of N values (number of points)
8 Nlist = 2.^[3:10] + 1; % Powers of 2 from 8 to 1024, incremented by 1
9
10 % Initialize arrays for step sizes and errors
11 hlist = zeros(size(Nlist)); % Step sizes
12 errorlist = zeros(size(Nlist)); % Errors
13
14 % Loop over each N value
15 for indi = 1:length(Nlist)
16 N = Nlist(indi); % Current number of points
17 x = cos(linspace(pi, 0, N))'; % Nonuniformly distributed points in [-1, 1]
18 hlist(indi) = max(x(2:end) - x(1:end-1)); % Calculate maximum step size
19 D = DiffMatrixOrder2(x); % Calculate differentiation matrix
20 y = fun(x); % Evaluate function at points
21 dyexact = funx(x); % Exact derivative values
22 dy = D * y; % Numerical derivative using matrix D
23 errorlist(indi) = max(abs(dy - dyexact)); % Calculate error
24 end
25
26 % Plot numerical error vs step size
27 loglog(hlist, errorlist, '-r'); % Numerical error in red
28 hold on;
29
30 % Plot theoretical prediction of the error
31 loglog(hlist, max(abs(funxxx(x))) / 2 * hlist.^2, '--b'); % Correct theoretical error in
blue
32 hold off;
33
34 % Add legend with more descriptive names
35 legend('Computed Derivative Error', 'Theoretical Error Bound', 'location',
'southeast');
36
37 % Save the figure as a JPG file
38 saveas(gcf, 'error.jpg');
 PHOTO-2024-07-20-03-59-20.jpg  Download

 Horner.m  Download

1 function y = Horner(coeffs, x)
2 % Horner's method for evaluating a polynomial
3 % coeffs: vector of polynomial coefficients in descending order
4 % x: point at which to evaluate the polynomial
5 n = length(coeffs);
6 y = coeffs(1);
7 for i = 2:n
8 y = y * x + coeffs(i);
9 end
10 end
11

You might also like