VCE Specialist Maths study materials
VCE Specialist Maths study resources

Pseudocode for a Numerical Method Activity

VCE Specialist Maths · Put your knowledge to work in context.

All case studies

Pseudocode for a Numerical Method Activity

A teacher is preparing a Unit 2 learning activity on numerical integration. Students will implement the trapezoidal rule in pseudocode and compare it with the exact value of an integral they can evaluate analytically. The teacher drafts the following pseudocode:

SET a = 0, b = 1, n = 4
SET h = (b - a) / n
SET total = 0
FOR i FROM 1 TO n
    SET x = a + i*h
    total = total + f(x)
END FOR
SET result = h * total
PRINT result

A student runs it for f(x) = x^2 on [0,1] and gets 0.46875, while the exact value is 1/3. The student concludes 'the trapezoidal rule is wrong'. The teacher wants to use the moment to teach both debugging and the meaning of approximation error.

1. Identify the error in the pseudocode and correct it. Then explain why the student's value differs from 1/3.

Read the worked answer

The loop omits the endpoint values f(a) and f(b) and does not apply the half-weight to the endpoints. The trapezoidal rule is

T = h/2 * [f(a) + f(b) + 2sum_{i=1}^{n-1} f(a + ih)].

The given code sums only the interior points i = 1..n (and even includes f(b) once with full weight, while omitting f(a)), then multiplies by h rather than h/2. Corrected pseudocode:

SET a = 0, b = 1, n = 4
SET h = (b - a) / n
SET total = f(a) + f(b)
FOR i FROM 1 TO n-1
    SET x = a + i*h
    total = total + 2*f(x)
END FOR
SET result = (h/2) * total
PRINT result

For f(x) = x^2, h = 0.25, so T = 0.125*[0 + 1 + 2*(0.0625 + 0.25 + 0.5625)] = 0.125*[1 + 1.75] = 0.34375, which is below 1/3 as expected for a convex function. The student's 0.46875 arose from a coding error, not from the rule being wrong. The rule is an approximation; its error here is about 0.0104, and it shrinks as n grows (error is O(h^2)).

2. Write a short debugging sequence (in words or pseudocode) that a student could use to locate the fault, and state what output at each stage would confirm the diagnosis.

Read the worked answer

A staged debugging sequence:

  1. Print h. Expected 0.25. If not, the interval or n is wrong.
  2. Print the list of x values sampled inside the loop. Expected 0.25, 0.5, 0.75, 1.0 in the faulty version; the correct version should sample 0.25, 0.5, 0.75 as interior points and handle 0 and 1 separately as endpoints.
  3. Print the running total after each iteration. Compare with hand-computed values: interior sum for x^2 should be 0.0625 + 0.25 + 0.5625 = 0.875.
  4. Print the final multiplier. If the code multiplies by h instead of h/2, the factor is visibly wrong.

Confirming the diagnosis: the sampled x-list shows 1.0 included with full weight and 0.0 missing, and the multiplier is h. Both observations together explain the inflated result. After correction, the printed result should be 0.34375, and doubling n to 8 should give a value closer to 1/3 (about 0.3359), confirming convergence.

3. The teacher wants students to appreciate that the error is O(h^2). Describe an activity using the corrected pseudocode that produces evidence for this claim, including what students should tabulate.

Read the worked answer

Students run the corrected code for n = 2, 4, 8, 16, 32 with the same integrand and record:

  • n
  • h = 1/n
  • computed T
  • exact value (1/3)
  • absolute error E = |T - 1/3|
  • ratio E(n)/E(2n)

If the error is O(h^2), then halving h should reduce the error by roughly a factor of 4, so the ratio column should approach 4. For x^2 on [0,1], the trapezoidal error has a known closed form; students should observe ratios close to 4 and note the small deviations due to rounding. Students then repeat with f(x) = x^3, where the trapezoidal rule is exact (error zero for all n), and discuss why the ratio test breaks down there. The activity teaches that empirical convergence rates are evidence, not proof, and that special cases (polynomials of low degree) can mislead.