HW 4 Questions
HW 4 Questions
Student
SHAYA MASHHOOR ALHARTHI
View or edit group
Total Points
24 / 50 pts
Question 1
(no title) 14 / 19 pts
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 2 pts Correct
+ 0 pts Incorrect
+ 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
− 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
− 10 pts No Submission
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
− 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
Q1.2
2 Points
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
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
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
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
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
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
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
1
3
2
2
5
2
Q1.9
1 Point
M
5
3
8
3
5
2
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
Hint
Method 1: construct a polynomial interpolation, take the derivative, and then
evaluate the derivative at x = x0 .
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
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
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
% 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
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
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;
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
Please replace loglog(hlist, max(abs(funxx(x))) / 2 * hlist, '--b'); with the correct theoretical
error bound.
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