Scientific Computing with Python — Exam Study Guide 1
Scientific Computing with Python
Exam Study Guide — Complete Step-by-Step Solutions
0 1 Contents
1 Curve Fitting 2
1.1 Least-Squares Fitting — ŷ = α1 + α2 x . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Interpolation 4
2.1 Linear Interpolation at x = 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Natural Cubic Spline at x = 1.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Lagrange Polynomial Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Newton’s Divided Differences — x = [0, 1, 2], y = [1, 3, 2] . . . . . . . . . . . . . . 6
2.5 Newton’s Divided Differences — x = [−5, −1, 0, 2], y = [−2, 6, 1, 3] . . . . . . . . 7
3 Numerical Integration Rπ 8
3.1 Left/Right RiemannR π& Midpoint Rule — 0 sin(x) dx . . . . . . . . . . . . . . . 8
3.2 Trapezoid Rule — 0 sin(x) dx, 5 grid points . . . . . . . . . . . . . . . . . . . . 9
3.3 Derivation of Simpson’s Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Boundary Value Problems — Finite Difference Method 11
4.1 Problem: u′′ = cos(6πx), u(0) = u(1) = 0 (Dirichlet–Dirichlet) . . . . . . . . . . . 11
4.2 Problem: u′′ = cos(6πx), u′ (0) = 0, u(1) = 0 (Neumann–Dirichlet) . . . . . . . . 12
4.3 Problem: u′′ = cos(6πx), u′ (0) = u′ (1) = 0 (Neumann–Neumann) . . . . . . . . . 13
Scientific Computing with Python — Exam Study Guide 2
1 1 Curve
Fitting
Problem 1.1. Least-Squares Fitting — ŷ = α1 + α2 x
Data: {(1, 1), (2, 2), (3, 2)}. Find α1 , α2 .
Method / Key Idea
We want to fit a line through data that cannot be made to pass through
P all points exactly.
2
The least-squares approach minimises the total squared error SSE = i (yi − ŷi ) .
(a) Linear algebra route (normal equations): Arrange the problem as an overdeter-
mined linear system Aα = b where each row of A is [1, xi ] and bi = yi . Because there are
more equations than unknowns, no exact solution exists. We multiply both sides by A⊤ to
obtain the normal equations:
A⊤ A α = A⊤ b
This always has a unique solution when the columns of A are independent.
(b) Calculus route: Write out SSE explicitly and set both partial derivatives equal to
zero:
∂ SSE ∂ SSE
= 0, = 0.
∂α1 ∂α2
This yields exactly the same system as the normal equations.
Step-by-Step Solution
(a) Part (a) — Linear Algebra Method
Step 1: Build the design matrix A and data vector b:
1 1 1
A= 1 2 ,
b = 2 .
1 3 2
Step 2: Compute A⊤ A and A⊤ b:
⊤ 3 6 ⊤ 1+2+2 5
A A= , A b= = .
6 14 1+4+6 11
Step 3: Write the normal equations:
3α1 + 6α2 = 5, 6α1 + 14α2 = 11.
5−6α2
Step 4: Solve. From the first equation: α1 = 3 . Substitute into the second:
5 − 6α2
6· + 14α2 = 11 =⇒ 10 − 12α2 + 14α2 = 11 =⇒ 2α2 = 1 =⇒ α2 = 12 .
3
5−3
Then α1 = 3 = 23 .
(b) Part (b) — Calculus Method
Step 1: Write SSE:
SSE = (1 − α1 − α2 )2 + (2 − α1 − 2α2 )2 + (2 − α1 − 3α2 )2 .
Scientific Computing with Python — Exam Study Guide 3
Step 2: ∂ SSE/∂α1 = 0: sum of −2(yi − α1 − α2 xi ) = 0, giving 3α1 + 6α2 = 5.
Step 3: ∂ SSE/∂α2 = 0: sum of −2xi (yi − α1 − α2 xi ) = 0, giving 6α1 + 14α2 = 11.
Same system as (a).
Final Answer
2 1 2 1
α1 = , α2 = =⇒ ŷ = + x
3 2 3 2
Scientific Computing with Python — Exam Study Guide 4
2 1 Interpolation
Common data set for Problems 2–5: x = [0, 1, 2], y = [1, 3, 2].
Problem 2.1. Linear Interpolation at x = 1.5
Method / Key Idea
Linear interpolation draws a straight line between two neighbouring data points and reads
off the y-value at the desired x.
y2 − y1
y = y1 + (x − x1 )
x2 − x1
First identify which subinterval contains the query point.
Step-by-Step Solution
Step 1: x = 1.5 lies in the interval [1, 2], so use (x1 , y1 ) = (1, 3) and (x2 , y2 ) = (2, 2).
Step 2: Apply the formula:
2−3
y(1.5) = 3 + (1.5 − 1) = 3 + (−1)(0.5) = 3 − 0.5.
2−1
Final Answer
y(1.5) = 2.5
Problem 2.2. Natural Cubic Spline at x = 1.5
Method / Key Idea
A cubic spline fits a different cubic polynomial on each sub-interval while ensuring continuity
of the function, first derivative, and second derivative at every interior node. For a natural
spline the second derivatives at the two endpoints are set to zero: M0 = Mn = 0.
For n + 1 points the interior moments Mi satisfy the tridiagonal system
yi+1 − yi yi − yi−1
hi Mi−1 + 2(hi + hi+1 )Mi + hi+1 Mi+1 = 6 − .
hi+1 hi
Once Mi are known, on the interval [xi , xi+1 ]:
Mi 3 Mi+1 3 yi Mi h yi+1 Mi+1 h
S(x) = (xi+1 −x) + (x−xi ) + − (xi+1 −x)+ − (x−xi ).
6h 6h h 6 h 6
Step-by-Step Solution
Step 1: We have 3 points so h0 = h1 = 1. Natural conditions: M0 = M2 = 0.
Step 2: The single interior equation (i = 1):
y2 − y1 y1 − y0
1 · M0 + 2(1 + 1)M1 + 1 · M2 = 6 − = 6[(2 − 3) − (3 − 1)] = 6(−3) = −18.
1 1
With M0 = M2 = 0: 4M1 = −18 =⇒ M1 = −4.5.
Scientific Computing with Python — Exam Study Guide 5
Step 3: Evaluate on [1, 2] (i.e. xi = 1, xi+1 = 2, yi = 3, yi+1 = 2, Mi = M1 =
−4.5, Mi+1 = M2 = 0, h = 1):
−4.5 3 0 3 −4.5
S(x) = (2 − x) + (x − 1) + 3 − (2 − x) + (2 − 0) (x − 1)
6 6 6
= −0.75(2 − x)3 + 3.75(2 − x) + 2(x − 1).
Step 4: Substitute x = 1.5:
S(1.5) = −0.75(0.5)3 + 3.75(0.5) + 2(0.5) = −0.75(0.125) + 1.875 + 1 = −0.09375 + 2.875.
Final Answer
S(1.5) = 2.78125
Problem 2.3. Lagrange Polynomial Interpolation
Method / Key Idea
For n + 1 data points (x0 , y0 ), . . . , (xn , yn ), the Lagrange basis polynomial for node i is
n
Y x − xk
Pi (x) = .
xi − xk
k=0
k̸=i
= 1 and Pi (xk ) = 0 for k ̸= i (cardinal property). The interpolating
Key property: Pi (xi ) P
polynomial is L(x) = ni=0 yi Pi (x).
Step-by-Step Solution
(a) Compute the three basis polynomials (x0 = 0, x1 = 1, x2 = 2):
(x − 1)(x − 2) (x − 1)(x − 2)
P0 (x) = = ,
(0 − 1)(0 − 2) 2
(x − 0)(x − 2) x(x − 2)
P1 (x) = = = −x(x − 2),
(1 − 0)(1 − 2) −1
(x − 0)(x − 1) x(x − 1)
P2 (x) = = .
(2 − 0)(2 − 1) 2
(b) Verify the cardinal property for P0 :
(−1)(−2) (0)(−1) (1)(0)
P0 (0) = = 1 ✓, P0 (1) = = 0 ✓, P0 (2) = = 0 ✓.
2 2 2
(The same holds for P1 and P2 by construction.)
(c) Build and simplify L(x):
L(x) = 1 · P0 (x) + 3 · P1 (x) + 2 · P2 (x)
(x − 1)(x − 2)
= − 3x(x − 2) + x(x − 1)
2
x2 − 3x + 2
= − 3x2 + 6x + x2 − x
2
3 7
= − x2 + x + 1.
2 2
Scientific Computing with Python — Exam Study Guide 6
Verification: L(0) = 1 ✓, L(1) = − 23 + 7
2 + 1 = 3 ✓, L(2) = −6 + 7 + 1 = 2 ✓.
Final Answer
L(x) = − 32 x2 + 72 x + 1
Problem 2.4. Newton’s Divided Differences — x = [0, 1, 2], y = [1, 3, 2]
Method / Key Idea
The divided-difference table is built recursively:
f [xi+1 ] − f [xi ] f [xi+1 , . . . , xi+k ] − f [xi , . . . , xi+k−1 ]
f [xi , xi+1 ] = , f [xi , . . . , xi+k ] = .
xi+1 − xi xi+k − xi
Newton’s interpolating polynomial uses the top diagonal of the table:
N (x) = f [x0 ] + f [x0 , x1 ](x − x0 ) + f [x0 , x1 , x2 ](x − x0 )(x − x1 ) + · · ·
It produces the same unique polynomial as Lagrange, just in a different form.
Step-by-Step Solution
Step 1: Build the divided-difference table:
xi f [xi ] 1st order 2nd order
0 1
3−1
=2
1−0
−1 − 2 3
1 3 =−
2−0 2
2−3
= −1
2−1
2 2
Step 2: Write Newton’s polynomial using the first row of each column:
N (x) = f [x0 ] + f [x0 , x1 ](x − x0 ) + f [x0 , x1 , x2 ](x − x0 )(x − x1 )
= 1 + 2(x − 0) + − 23 x(x − 1)
= 1 + 2x − 32 x2 + 23 x
= − 32 x2 + 27 x + 1.
Step 3: Verify: N (0) = 1 ✓, N (1) = 3 ✓, N (2) = 2 ✓.
Final Answer
N (x) = − 32 x2 + 27 x + 1 (identical to Lagrange result)
Scientific Computing with Python — Exam Study Guide 7
Problem 2.5. Newton’s Divided Differences — x = [−5, −1, 0, 2], y = [−2, 6, 1, 3]
Method / Key Idea
Same procedure as Problem 5 but with 4 points, giving a degree-3 polynomial and a larger
divided-difference table with three levels of differences.
Step-by-Step Solution
Step 1: Build the divided-difference table:
xi f [·] 1st 2nd 3rd
−5 −2
6 − (−2)
=2
−1 − (−5)
−5 − 2
−1 6 = − 75
0 − (−5)
1−6 2 − (−7/5) 17
= −5 = 35
0 − (−1) 2 − (−5)
1 − (−5)
0 1 =2
2 − (−1)
3−1
=1
2−0
2 3
Step 2: Coefficients from the top diagonal: −2, 2, − 75 , 17
35 .
Step 3: Newton’s polynomial:
N (x) = −2 + 2(x + 5) − 57 (x + 5)(x + 1) + 17
35 (x + 5)(x + 1) x.
Step 4: Verify at all four nodes (shown for x = 0):
N (0) = −2 + 2(5) − 75 (5)(1) + 0 = −2 + 10 − 7 = 1 ✓.
All four nodes check out similarly.
Final Answer
N (x) = −2 + 2(x + 5) − 75 (x + 5)(x + 1) + 17
35 (x + 5)(x + 1)x
Scientific Computing with Python — Exam Study Guide 8
3 1 Numerical
Integration
Rπ
Problem 3.1. Left/Right Riemann & Midpoint Rule — 0 sin(x) dx
5 evenly spaced grid points ⇒ 4 subintervals.
Method / Key Idea
b−a
h= , xi = a + i h.
n
Pn−1
Left Riemann: L = h i=0 f (xi ) (use left endpoint of each sub-interval)
Pn
Right Riemann: R = h i=1 f (xi ) (use right endpoint)
Pn−1 xi +xi+1
Midpoint: M = h i=0 f 2 (use centre of each sub-interval)
Step-by-Step Solution
Step 1: Set up: n = 4 subintervals, h = π/4. Grid points: x0 = 0, x1 = π/4, x2 =
π/2, x3 = 3π/4, x4 = π.
Step 2: Function values:
√ √
2 2
sin(0) = 0, sin(π/4) = 2 , sin(π/2) = 1, sin(3π/4) = 2 , sin(π) = 0.
Step 3: Left Riemann (skip last point):
" √ √ #
π 2 2 π √
L= 0+ +1+ = (1 + 2) ≈ 1.896.
4 2 2 4
Step 4: Right Riemann (skip first point):
"√ √ #
π 2 2 π √
R= +1+ + 0 = (1 + 2) ≈ 1.896.
4 2 2 4
(Same as left because sin(0) = sin(π) = 0.)
Step 5: Midpoint rule. Midpoints: π/8, 3π/8, 5π/8, 7π/8. By symmetry sin(5π/8) =
sin(3π/8) and sin(7π/8) = sin(π/8):
π π
M= · 2[sin(π/8) + sin(3π/8)] = · 2(0.3827 + 0.9239) ≈ 2.052.
4 4
Final Answer
Left ≈ 1.896, Right ≈ 1.896, Midpoint ≈ 2.052.
Exact value = 2. Midpoint is the most accurate.
Scientific Computing with Python — Exam Study Guide 9
Rπ
Problem 3.2. Trapezoid Rule — 0 sin(x) dx, 5 grid points
Method / Key Idea
The trapezoid rule approximates the area under the curve using trapezoids:
h
T = [f (x0 ) + 2f (x1 ) + 2f (x2 ) + · · · + 2f (xn−1 ) + f (xn )] .
2
Endpoints have weight 1; interior points have weight 2.
Step-by-Step Solution
Step 1: Same grid as Problem 7: h = π/4, 5 points.
Step 2: Apply formula:
π/4
T = [sin(0) + 2 sin(π/4) + 2 sin(π/2) + 2 sin(3π/4) + sin(π)]
2h
π √ √ i
= 0+ 2+2+ 2+0
8
π √ π √
= (2 + 2 2) = (1 + 2).
8 4
Final Answer
π √
T = (1 + 2) ≈ 1.896. Error ≈ 5.2% below exact value of 2.
4
Problem 3.3. Derivation of Simpson’s Rule
Z xi+1
h
Show that Li (x) dx = f (xi−1 ) + 4f (xi ) + f (xi+1 ) .
xi−1 3
Method / Key Idea
The quadratic Lagrange polynomial Li (x) through three equally-spaced nodes xi−1 = xi −h,
xi , xi+1 = xi + h is:
(x − xi )(x − xi+1 ) (x − xi−1 )(x − xi+1 ) (x − xi−1 )(x − xi )
Li (x) = f (xi−1 ) +f (xi ) +f (xi+1 ) .
(−h)(−2h) (h)(−h) (2h)(h)
Substitute t = x − xi so the limits become −h to h, then integrate each term analytically
using the fact that odd-power integrals over [−h, h] vanish.
Step-by-Step Solution
Step 1: Substitution t = x − xi , dt = dx. In terms of t:
x − xi−1 = t + h, x − xi = t, x − xi+1 = t − h.
Step 2: Each of the three Lagrange denominators:
t(t − h) (t + h)(t − h) t(t + h)
Li (x) = f (xi−1 ) 2
+ f (xi ) 2
+ f (xi+1 ) .
2h −h 2h2
Scientific Computing with Python — Exam Study Guide 10
Step 3: Compute the three key definite integrals over [−h, h]:
h h
t3 ht2 2h3
Z
t(t − h) dt = − = ,
−h 3 2 −h 3
Z h 3 h
2 2 t 2 4h3
(t − h ) dt = −h t =− ,
−h 3 −h 3
Z h
2h3
t(t + h) dt = (same as first by symmetry).
−h 3
Step 4: Combine:
Z xi+1
2h3 /3 −4h3 /3 2h3 /3
Li (x) dx = f (xi−1 ) · + f (x i ) · + f (x i+1 ) ·
xi−1 2h2 −h2 2h2
h 4h h
= f (xi−1 ) + f (xi ) + f (xi+1 ).
3 3 3
Final Answer
Z xi+1
h
Li (x) dx = f (xi−1 ) + 4f (xi ) + f (xi+1 ) ✓
xi−1 3
This is the Simpson’s Rule formula.
Scientific Computing with Python — Exam Study Guide 11
4 1 Boundary
Value Problems — Finite Difference Method
Note
Key recall: The second-derivative finite-difference approximation is
u(x + h) − 2u(x) + u(x − h)
u′′ (x) ≈ .
h2
1
With n grid points on [0, 1], the step size is h = .
n−1
Problem 4.1. Problem: u′′ = cos(6πx), u(0) = u(1) = 0 (Dirichlet–Dirichlet)
Method / Key Idea
Exact solution: Integrate u′′ = f (x) twice and apply boundary conditions to determine
both integration constants.
Finite difference: With Dirichlet BCs, the boundary values u0 and un are directly known.
Write the FD equation only at interior nodes. This gives a tridiagonal linear system with
the boundary values on the right-hand side.
Step-by-Step Solution
(a) Exact Solution
Step 1: Integrate once:
sin(6πx)
u′ (x) = + C1 .
6π
Step 2: Integrate again:
cos(6πx)
u(x) = − + C1 x + C2 .
36π 2
Step 3: Apply u(0) = 0:
1 1
− + C2 = 0 =⇒ C2 = .
36π 2 36π 2
Step 4: Apply u(1) = 0:
cos(6π) 1 1 1
− + C1 + = 0 =⇒ − + C1 + = 0 =⇒ C1 = 0.
36π 2 36π 2 36π 2 36π 2
(b) Finite Difference, 5 grid points
1
Step 1: Step size: h = 4 = 0.25. Grid: x0 = 0, x1 = 0.25, x2 = 0.5, x3 = 0.75, x4 = 1.
Step 2: Boundary conditions give u0 = 0 and u4 = 0 directly. Interior unknowns:
u1 , u2 , u3 . FD equation at each interior node (ui−1 − 2ui + ui+1 = h2 cos(6πxi )):
Node i = 1 (x = 0.25, cos(6π · 0.25) = cos(3π/2) = 0):
1
u0 − 2u1 + u2 = 16 (0) = 0 ⇒ −2u1 + u2 = 0.
Scientific Computing with Python — Exam Study Guide 12
Node i = 2 (x = 0.5, cos(6π · 0.5) = cos(3π) = −1):
1 1
u1 − 2u2 + u3 = 16 (−1) = − 16 .
Node i = 3 (x = 0.75, cos(6π · 0.75) = cos(9π/2) = 0):
u2 − 2u3 + u4 = 0 ⇒ u2 − 2u3 = 0.
Step 3: Matrix form:
−2 1 0 u1 0
1 −2 1 u2 = −1/16 .
0 1 −2 u3 0
Step 4: Solve. By symmetry u1 = u3 . From eq. 1: u2 = 2u1 . Substitute into eq. 2:
1 1 1
u1 − 2(2u1 ) + u1 = − 16 =⇒ −2u1 = − 16 =⇒ u1 = 32 .
1 1
So u2 = 16 , u3 = 32 .
Final Answer
1 − cos(6πx)
Exact: u(x) =
36π 2
1 1
FD approximation: u1 = u3 = 32 ≈ 0.031, u2 = 16 ≈ 0.063.
Exact values: u(0.25) ≈ 0.00282, u(0.5) ≈ 0.00564 — FD overestimates significantly with
only 5 points.
Problem 4.2. Problem: u′′ = cos(6πx), u′ (0) = 0, u(1) = 0 (Neumann–Dirichlet)
Method / Key Idea
A Neumann BC specifies the derivative at a boundary. In FDM we handle it using a ghost
point: introduce a fictitious node x−1 outside the domain and use the central-difference
approximation
u1 − u−1
u′ (0) ≈ = 0 =⇒ u−1 = u1 .
2h
Substitute this back into the FD equation at node i = 0, making x0 an unknown interior
node.
Step-by-Step Solution
(a) Exact solution:
u′ (0) = 0 ⇒ C1 = 0. Then u(1) = 0 gives C2 = 1
36π 2
, same as Problem 10:
1 − cos(6πx)
u(x) = .
36π 2
(b) FDM with ghost point, 5 grid points:
Step 1: h = 1/4. Now u4 = 0 (Dirichlet) but x0 is unknown. Unknown vector:
(u0 , u1 , u2 , u3 ).
Step 2: Ghost point condition: u−1 = u1 .
Scientific Computing with Python — Exam Study Guide 13
Step 3: FD equation at i = 0 (using ghost point):
u−1 − 2u0 + u1 2u1 − 2u0 1
= cos(0) = 1 ⇒ = 1 ⇒ 2u1 − 2u0 = 16 .
h2 h2
Step 4: Equations at i = 1, 2, 3 are unchanged from Problem 10. Matrix system (u4 = 0):
−2 2 0 0 u0 1/16
1 −2 1 0 u1 = 0 .
0 1 −2 1 u2 −1/16
0 0 1 −2 u3 0
1 1
Step 5: Solve from bottom up. Eq. 4: u2 = 2u3 . Eq. 3: u1 − 3u3 = − 16 , so u1 = 3u3 − 16 .
2 1 1 1 1
Eq. 2: u0 = 2u1 − 2u3 = 6u3 − 16 − 2u3 = 4u3 − 8 . Eq. 1: −2(4u3 − 8 ) + 2(3u3 − 16 ) = 16
⇒ −2u3 + 14 − 81 = 16
1
⇒ −2u3 = 161
− 18 = − 16
1
⇒ u3 = 321
.
Final Answer
1 − cos(6πx)
Exact: u(x) = (same as Q10).
36π 2
1 1 1
FD solution: u0 = 0, u1 = 32 , u2 = 16 , u3 = 32 .
Problem 4.3. Problem: u′′ = cos(6πx), u′ (0) = u′ (1) = 0 (Neumann–Neumann)
Method / Key Idea
With Neumann conditions on both ends, use ghost points at both boundaries:
u′ (0) = 0 ⇒ u−1 = u1 , u′ (1) = 0 ⇒ u5 = u3 .
All five nodes are now unknowns. This gives a singular matrix — the pure-Neumann
problem has a solution only up to an additive constant (you must pin one value to get a
unique solution).
Step-by-Step Solution
(a) Exact solution:
u′ (0) = 0 ⇒ C1 = 0. Check u′ (1) = sin(6π)
6π = 0 ✓ — automatically satisfied! C2 is free;
1
infinitely many solutions exist. Choosing C2 = 36π 2 gives
1 − cos(6πx)
u(x) = .
36π 2
Compatibility Condition
R1
For pure-Neumann problems a solution exists only if 0 f (x) dx = u′ (1) − u′ (0). Here
R1
0 cos(6πx) dx = 0 = 0 − 0 ✓.
(b) FDM — all 5 nodes unknown:
Scientific Computing with Python — Exam Study Guide 14
Ghost points: u−1 = u1 and u5 = u3 . Writing the FD equation at all nodes:
−2 2 0 0 0 u0 1
1 −2 1 0 0 u1
0
1
0 1 −2 1 0 u2 =
−1 .
16
0 0 1 −2 1 u3 0
0 0 0 2 −2 u4 1
This 5 × 5 matrix is singular (rows sum to zero ⇒ rank 4). Pin u0 = 0 (arbitrary constant
choice) to resolve uniqueness and solve the 4 × 4 reduced system.
Final Answer
cos(6πx)
Exact: infinitely many solutions u(x) = − + C.
36π 2
FD: Singular system — pin one value (e.g. u0 = 0) to obtain
1 1 1
u0 = 0, u1 = 32 , u2 = 16 , u3 = 32 , u4 = 0.