Assignment 1
Assignment 1
Assignment 1
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
restrictions. Every submission consists of two parts: A theoretical part, which can be handwrit-
ten 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 repro-
duce 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, sin, ...), but you
are NOT allowed to use high-level numerical functions (such as diff, polyfit, ...).
All work must be explained, commented on, and all work must be shown. Implementa-
tion should contain comments and explanations in markdown surrounding them. Figures
should have approprate 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 partner, 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.
Assignment 1 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.
1.1 For a vector x ∈ Rn and matrix A ∈ Rn×n , and any norm ∥ ⋅ ∥ on Rn , show that
∥Ax∥
∣∣∣A∣∣∣ = sup
x≠0 ∥x∥
is a norm on Rn×n .
1.2 Compute the condition number K(d) in the ∣ ⋅ ∣-norm on R for the following equations,
a) x − ad = 0, and
b) d − x + 1 = 0,
where d is the datum, a a parameter, and x is the “unknown”.
x2 + 2px − q = 0 ,
1.5 Write down the linear interpolant p1 (x) for the function f (x) = x3 − x, using the nodes
x0 = 0 and x1 = a.
2
Assignment 1 MA2K4 2023/24– Numerical Methods and Computing
Denote by
e(x) = f (x) − p1 (x)
the error that we produced in the linear interpolation. Show that this error fulfils
e(x) = 12 f ′′ (ξ)x(x − a) .
1.6 Repeat this calculation for the function f (x) = (x − a)4 for the same nodes, and show
that in this case there are two possible values for ξ. Give their values.
3
Assignment 1 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.
1.7 In this problem, we want to numerically measure the sensitivity of a numerical prob-
lem to small perturbations of the data, i.e. numerically measure the phenomenon quan-
tified by the condition number. For this purpose, we want to consider again finding
the solutions to quadratic equations of the form
x2 + 2px − q = 0 ,
Implement functions x plus and x minus computing the two solutions to the quadratic
equation, given p and q. These functions should look like
1 def x_plus (p , q ) :
2 # insert computation
3 return result
4
5 def x_minus (p , q ) :
6 # insert computation
7 return result
which allows you to compute the two results for arbitrary p and q.
a) Consider first, as in problem 1.4 a), how sensitive the two solutions are when chang-
ing p, to measure the relative change of the answer, ∣δx∣/∣x∣ devided by the relative
change of the datum p,
∣δx+ ∣/∣x+ ∣
R+ (p, δp) = ,
∣δp∣/∣p∣
and separately the same for x− . Take a perturbation δp = 10−3 and compute, for p = −1
as well as p = 1, for q = 1, the quantities R+ and R− . These quantities signifies the
sensitivity of the answer when changing the input, i.e. p in this case. Comment on
your observations and their implication.
b) Now plot R+ (p, δp) for δp = 10−3 and p ∈ [−1, 1], and the same for R− in a separate plot.
Into each of these figures, also plot the analytical condition numbers K(p) computed
in problem 1.4 a).
Comment on your observation and comparison. Remember to explain your code and
comment on the observed result.
c) Do the same for q: Compute the relative change ∣δx∣/∣x∣ divided by the relative change
of the datum, ∣δq∣/∣q∣, for p = 1, δq = 10−3 , for q = −1 as well as q = 1.
d) Now, for p = 1 and δq = 10−3 , plot the numerically measured sensitivity for q ∈ [−1, 1].
Again, plot this for both solutions separately, and compare each against the condition
number K(q). Again, comment on your observation and comparison.
4
Assignment 1 MA2K4 2023/24– Numerical Methods and Computing
1.8 Implement a function linear interpolate that takes a function f (x) and two nodes
x0 and x1 as well as a position x, and returns its linear interpolant p1 (x) evaluated at
x. This function should have the signature
1 def li ne ar _i nte rp ol at e (f , x_0 , x_1 , x ) :
2 # insert computation
3 return result
This function should allow you to evaluate the linear interpolant of an arbitrary func-
tion and with arbitrary nodes at any point x. With this function, do the following:
a) Evaluate the linear interpolant p1 (x) for the nodes x0 = 0, x1 = 1 for the function
f (x) = 2 sin(2x) at the location x = 0.75.
b) Plot the function f (x) = sin(x) on the interval x ∈ [0, 1] and its linear interpolant
p1 (x) for the nodes x0 = 0, x1 = 1 on the same interval, and interpret your result.
c) Define a new function interpolation error that computes the interpolation error
which takes a function f , two nodes x0 , x1 , and a location x at which to compute the
error. Make sure to reuse the above function linear interpolate to compute p1 (x)
within the function body.
Now, plot this error for the function f (x) = ln(x) and the nodes x0 = 10, x1 = 11 on
the interval x ∈ [x0 , x1 ]. What is the error at the specific values x = 10.2 and x = 10.8?
d) Estimate the maximum absolute error
max ∣e(x)∣
x∈[x0 ,x1 ]
of the linear interpolant. Do so for the function f (x) = 1/(1 + x2 ) with nodes x0 = 0
and x1 = h, by first setting h = 1. Approximate the maximum error numerically by
computing the error for at least 100 equally spaced gridpoints between x0 and x1 to
search for its largest absolute value at these points. Implement this procedure in a
function max error(f, x 0, x 1). How large is the maximum absolute error for the
above function and nodes?
Now, compute and plot this maximum absolute error for various h given by the for-
mula
h = 2−k for k ∈ {0, 1, . . . , 10} .
Plot the maximum absolute error for each of these h as a function of h on a log-log
scale. What do you observe and how do you interpret this result?
Plot for comparison, into the same plot, the function g(h) = hγ , and try to find a γ for
which the decay rate matches the rate of the absolute error (such that the lines appear
mostly parallel). Which γ do you obtain and what does this mean for the scaling of
the error?
5
Assignment 1 MA2K4 2023/24– Numerical Methods and Computing
1.9 In this problem, we want to write a functions that allow for the Lagrange interpolation
of a generic function.
a) First, write a function
1 def lagrange (k , x , nodes ) :
2 # code goes here
that computes the k-th Lagrange polynomial for the nodes in the array nodes and
evaluates it at point x, given by the formula
m x − xj
Lk (x) = ∏ .
j≠k xk − xj
Make sure that the function can take a whole array of values in x, for example to
facilitate plotting.
Using this function, plot all 5 Lagrange polynomials Lk (x), k ∈ {0, . . . , 4} for the nodes
nodes = [0,1,2,3,4] on the interval x ∈ [0, 4]. Do the same for all 11 Lagrange
polynomials for the nodes nodes = np.arange(11) on the interval x ∈ [0, 10].
b) Using the above function, write a function
1 def lagrange_interp (x , nodes , values ) :
2 # code goes here
for nodes nodes and values values and point x. Make sure this function can take an
array with many values as x.
Use this to make the following plots: For m equally spaced nodes on the interval
[−5, 5], including the boundaries, plot the m-th Lagrange interpolation of f (x) =
1/(1 + x2 ) against x ∈ [−5, 5], and plot f (x) itself for comparison. Do so in four plots
for m = 3, 5, 11, 21. What do you observe, and what kind of phenomenon is this?
What are the values of each of these interpolants at x = 1 and at x = 4.5 in compar-
ison to the actual value of the function? Comment on your results. How do your
observations relate to the Weierstrass theorem?