assignment-2
assignment-2
Assignment 2
Submission Details
This assignment is due to be handed in before
Student ID
Student ID of partner
Please fill in your ID and that of your working partner above. You need to submit your work
even when working with a partner.
Please read carefully all instructions on this page to make sure you understand all rules and restric-
tions. Every submission consists of two parts: A theoretical part, which can be handwritten or computer-
written, and a numerical part, which is to be submitted as jupyter notebook .ipynb-file. Further rules
and restrictions:
All handwritten parts must be legible to receive full marks.
Make sure to provide explanations and show your work. The correct answer alone and without
explanation will not receive full marks.
Numerical work should come in an .ipynb-file with comments, explanations, code, and figures.
The submission file needs to be able to run through from top to bottom to reproduce all the results
of your submission.
Python is the only computer language to be used in this course.
You are allowed to use pre-defined mathematical functions (such as exp, linspace, max, ...), but
you are NOT allowed to use high-level numerical functions (such as interpolate, polyfit, ...).
All work must be explained, commented on, and all work must be shown. Implementation should
contain comments and explanations in markdown surrounding them. Figures should have appro-
prate axis scaling, labels, legends, etc.
The assignments can be worked on and submitted in pairs. If you choose to do so, indicate your
own and your partner’s student ID in the fields above. Each partner must upload all submission
files to moodle. In this case,both partners receive the same mark.
Late panelties apply automatically to late submissions. Even if you submit together with a part-
ner, and your partner submits in time, you will be penalised for a late submission if your own
submission is not in time.
To reiterate the above: You will only earn marks if you submit work, and do so before the deadline.
The use of generative AI in this assignment is strictly forbidden.
Assignment 2 MA2K4 2023/24– Numerical Methods and Computing
Theoretical Part
This part is to be solved by hand and manual calculation, without the help of a computer. Show
all your work to receive full marks.
2.1 Interpolate the function f (x) = log(x) by a quadratic polynomial p2 (x) with nodes at
x0 = 10, x1 = 11, and x2 = 12. Isolate the coefficients in the polynomial (which might
look ugly), and then compute them to at least 3 digits accuracy, for example with a
calculator.
2.3 Consider n + 2 distinct nodes xi ∈ R, i ∈ {0, 1, . . . n + 1}, and the corresponding data val-
ues yi ∈ R, i ∈ {0, . . . n+1}. Let q(x) be the Lagrange interpolation polynomial of degree
n for the nodes and values {(xi , yi ), i = 0, 1, . . . , n} and let r(x) be the Lagrange inter-
polation polynomial of degree n for the nodes and values {(xi , yi ), i = 1, 2, . . . , n + 1}.
Define now a new polynomial
Show that this p(x) is the Lagrange interpolation polynomial of degree n + 1 for the
nodes and values {(xi , yi ), i = 0, 1, . . . , n + 1}.
2.4 Recursive Formula for Polynomial Interpolation. Given a function f ∶ [a, b] → R, we make
the following recursive definition:
f [a] ∶= f (a)
f [b] − f [a]
f [a, b] ∶=
b−a
f [b, c] − f [a, b]
f [a, b, c] ∶=
c−a
f [b, c, d] − f [a, b, c]
f [a, b, c, d] ∶=
d−a
⋮
f [a1 , . . . , an ] − f [a0 , . . . , an−1 ]
f [a0 , . . . , an ] ∶= (1)
an − a0
Consider now (m + 1) distinct nodes xj ∈ R for j ∈ {0, . . . , m}. Define the polynomial
2
Assignment 2 MA2K4 2023/24– Numerical Methods and Computing
with q0 (x) = f (x0 ). Show that the Lagrange interpolation polynomial of degree m,
pm (x), is in fact given by
pm (x) = qm (x) .
2.5 For the Newton-Cotes quadrature formula, using equally spaced quadrature points
xj = a + nj (b − a), show that
αj = αn−j
where αj is the quadrature weight of the j-th node.
3
Assignment 2 MA2K4 2023/24– Numerical Methods and Computing
Numerical Part
This part is to be solved in python in a single jupyter notebook .ipynb-file. Make sure that all
your explanations, code, figures, results, comments, and discussions are included in this single
file to receive all marks. Re-use as much code as possible and avoid any code duplication.
2.6 Condition number for different grids. Let f (x) be a sufficiently smooth function, and
f˜(x) a small perturbation of f . Similarly, let pm (x) and p̃m (x) be their respective Lan-
grange interpolating polynomials. Then we have
a) Write a python function Lambda(m) that takes a number m ∈ N with m ≥ 2 and com-
putes the Lebesgue constant for m equally spaced nodes on the interval x ∈ [−1, 1]
including the boundaries. Approximate the ∞-norm by taking the maximum over
100 values of x. Use your implementation to compute Λ10 .
b) Use this function to plot Λm for equally spaced grids against m for values of m up to
30, and with logarithmic scaling on the y-axis. Show in your plot that this Λm behaves
like
2m
Λm ∼
m log m
up to an unimportant multiplicative constant. Comment on what this result has to
do with numerical well-posedness, stability, and convergence.
c) Now, write a new function LambdaC(m) that computes the Lebesgue constant for m ∈
N with m ≥ 2 like above, but on nodes located at
i
xi = cos ( π) where i ∈ {0, . . . , m − 1} .
m−1
Plot this new constant against m for 20 values of m up to 200. Compare this against
2/π log(m), up to an unimportant additive constant. What do you conclude, in com-
parison to the results above?
2.7 Recursive Formula for Polynomial Interpolation. The formula given in problem 2.4 al-
lows for a recursive definition of the Lagrange interpolating polynomial. In this prob-
lem we want to implement this recursive formula.
a) Implement a function recursive f(f, nodes) that takes a function f and an array
of nodes x = (x0 , . . . , xk ) and returns f [x0 , . . . , xk ] as defined in equation (1). Use this
to compute f [0, 1, 2, 3] for f (x) = sin(x).
Hint: You can make this function call itself to implement the recursive definition.
b) Now write a function qk(f, nodes, k, x) that computes the polynomial qk (x) as
defined in equation (2). You can make use of the function you implemented for the
previous problem.
4
Assignment 2 MA2K4 2023/24– Numerical Methods and Computing
Use this to plot into a single figure, for f (x) = tan−1 (x) and the nodes x = (0, 1, 2, 3, 4),
the polynomials qk (x) for k ∈ {0, 1, 2, 3, 4} on the interval x ∈ [0, 5]. Make sure to plot
f (x) for comparison, and plot the location of all nodes into the same plot as well.