Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 37 ยท Projects and Practice

Numerical Methods Practice Problems with Solutions

Eight worked practice problems on interpolation, fitting, differentiation, integration, ODEs, optimization and eigenvalues, each with a checked Python solution.

Reading about numerical methods is not the same as using them. This lesson gives you eight problems covering interpolation and fitting, differentiation and integration, ODEs, optimization and eigenvalues. For each one: read the problem, try it yourself first (even on paper, to decide which method and what to check), then open the solution and compare. Every solution also validates itself against an independent answer, a habit worth copying.

Problem 1: interpolate a table

A table gives f(x) = ln(x) at x = 1, 2, 4, 5. Estimate ln(3) with the Newton interpolating polynomial, then with a cubic spline, and compare the errors with the true value 1.098612.

Python Solution 1

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The polynomial beats the natural spline here: with only four points and a smooth function, the artificial "zero curvature at the ends" condition costs more than it gains.

Problem 2: how many panels?

How many panels does the composite trapezoid rule need to integrate exp(-x^2) over [0, 1] to an error below 1e-6? Find n by doubling and compare with the reference from quad. Then check how many Simpson panels are needed.

Python Solution 2

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Simpson needs far fewer panels, as its error decreases like h^4 against h^2.

Problem 3: roots from derivatives

A bead slides on a curve whose height is h(x) = x^4 - 3x^2 + x. Find all points where the slope is zero (using a numerical derivative of h, not the formula), and classify each as a minimum or maximum with a second difference.

Python Solution 3

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 4: a line or a parabola?

Data: x = 0..9, y = [1.1, 1.9, 3.2, 5.1, 7.8, 11.2, 15.9, 21.1, 28.2, 35.8]. Fit a line and a parabola. Compare the residuals and decide which is justified using leave-one-out error.

Python Solution 4

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The line is clearly inadequate: the parabola cuts the residual about a hundredfold. The cubic still lowers the leave-one-out error here, because these data grow faster than any parabola (they are roughly exponential). That is a hint to try a different model family, such as a * exp(b * x) with curve_fit, instead of piling on polynomial degrees.

Problem 5: a falling parachutist

Speed obeys v' = g - (c/m) v with g = 9.81, c = 12.5 kg/s, m = 68.1 kg, v(0) = 0. Solve with RK4 for h = 2 seconds up to t = 12 and compare with the exact solution v(t) = (g m / c) (1 - exp(-(c/m) t)).

Python Solution 5

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 6: a stiff trap

Solve y' = -300 (y - sin t) + cos t, y(0) = 1, on [0, 2] with explicit Euler at h = 0.01 and h = 0.005, and with backward Euler at h = 0.01. The exact solution is y = sin t + exp(-300 t). Which behave?

Python Solution 6

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The explicit stability limit here is 2 / 300 ~ 0.0067: h = 0.01 explodes, h = 0.005 is stable, and backward Euler is fine even with the large step. See Euler's method.

Problem 7: minimize a bowl

Minimize f(x, y) = (x - 3)^2 + 10 (y + 1)^2 + x y with gradient descent using a fixed step, then with BFGS, and compare the iteration counts. What step size is safe? (The Hessian is [[2, 1], [1, 20]], so use its largest eigenvalue.)

Python Solution 7

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Problem 8: vibrating masses

Three equal masses joined by springs, ends fixed, have the stiffness matrix K = [[2,-1,0],[-1,2,-1],[0,-1,2]] (units k/m = 1). The vibration frequencies are sqrt(lambda) for each eigenvalue. Find the highest frequency by the power method, the lowest by shifted inverse iteration, and verify with the exact values sqrt(2 - 2 cos(j pi / 4)).

Python Solution 8

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For further reading on the underlying methods see power iteration and the QR algorithm.

How to check your own solutions

  • Compare with an independent method: a closed form, scipy, or a finer discretization.
  • Check convergence orders: halve h and look for the ratio 2^p.
  • Look at residuals, not just answers: A v - lambda v, f'(x*), the energy drift.
  • Change the tolerance. If the answer moves, it was not converged.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Problem 6, why does forward Euler with h = 0.01 explode?

    The step factor 1 - hk = -2 has magnitude above 1, so errors grow every step.
  2. 2. Which rule needs fewer panels for the same accuracy on a smooth integrand?

    Simpson's error is O(h^4) against O(h^2), so it needs far fewer panels.
  3. 3. What is a safe fixed step for gradient descent on a quadratic with Hessian H?

    Stability requires alpha < 2 / lambda_max; 1 / lambda_max is a common safe choice.
  4. 4. Why is leave-one-out error useful when choosing a polynomial degree?

    Predicting held-out points exposes overfitting that the training residual hides.

Summary

  • Choose the method from the structure of the problem: smoothness, noise, stiffness, conditioning.
  • Always validate against an independent answer and check convergence rates.
  • Residuals, conserved quantities and tolerance changes are cheap, powerful sanity checks.

Next: back to the Numerical Methods course overview