---
title: "Composite Simpson 1/3 algorithm"
subject: "Numerical Method"
chapter: 5
key: "semester_4/numerical_method:simpson-13-algorithm"
source: https://www.bctengineer.com/q/semester_4/numerical_method/simpson-13-algorithm
site: BCT Engineering
---

# Composite Simpson 1/3 algorithm

Numerical Method · Chapter 5: Numerical Differentiation and Integration

- Asked in: 2072 Asoj, 4 marks
- Video: [Simpson's One Third Rule C programming With Output](https://www.youtube.com/watch?v=hcGUu66xJcM) — Solution Hub
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/numerical_method/simpson-13-algorithm
- Whole chapter: https://www.bctengineer.com/md/semester_4/numerical_method/chapter-5

## Question

Write an algorithm to calculate the definite integral ∫ from a to b of f(x) dx using composite Simpson's 1/3 rule.

## Short answer

Divide [a,b] into n even subintervals of width h = (b-a)/n. Compute sum = f(a)+f(b). For i=1 to n-1, add 4f(x) if i is odd, else 2f(x). Multiply sum by h/3. Algorithm: read a, b, n (if odd, add 1), compute h, accumulate sum, output I = (h/3)*sum.

## Answer

Composite Simpson's 1/3 rule divides [a, b] into n (even) equal sub-intervals of width h = (b − a)/n and fits a parabola over each pair:

```text
I ≈ (h/3) [ f₀ + 4(f₁ + f₃ + … + fₙ₋₁) + 2(f₂ + f₄ + … + fₙ₋₂) + fₙ ],   fᵢ = f(a + i·h)
```

#### Algorithm

1. Start.
2. Define the function f(x).
3. Read the limits a and b and the number of sub-intervals n.
4. If n is odd, add 1 to n (or ask again), because n must be even.
5. Compute h = (b − a)/n.
6. Set sum = f(a) + f(b).
7. For i = 1 to n−1:
   - x = a + i·h
   - if i is odd, sum = sum + 4·f(x); otherwise sum = sum + 2·f(x).
8. Compute I = (h/3)·sum.
9. Print I.
10. Stop.

#### C program

```c
#include <stdio.h>
#include <math.h>

double f(double x) { return exp(x) + sin(2 * x); }   /* example function */

int main() {
    double a, b, h, x, sum, I;
    int n, i;
    printf("Enter a, b and n (even): ");
    scanf("%lf %lf %d", &a, &b, &n);
    if (n % 2 != 0) n++;                 /* n must be even */
    h = (b - a) / n;
    sum = f(a) + f(b);
    for (i = 1; i < n; i++) {
        x = a + i * h;
        if (i % 2 == 1) sum += 4 * f(x);  /* odd points */
        else            sum += 2 * f(x);  /* even points */
    }
    I = h / 3 * sum;
    printf("Integral = %f\n", I);
    return 0;
}
```

For a = 0, b = 2, n = 10, the program prints the integral of eˣ + sin 2x as about 7.2160 (exact 7.2159).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What condition must n satisfy for composite Simpson's 1/3 rule?
   A. n must be a multiple of 3
   B. n must be odd
   C. n must be even
   D. n must be a power of 2
   Answer: C. Each parabola covers two sub-intervals, so n must be even. (Key point: Condition on n)

2. In the loop, which weights are given to f(x_i) for interior points?
   A. 2 for odd i and 4 for even i
   B. 3 for all interior points
   C. 4 for all interior points
   D. 4 for odd i and 2 for even i
   Answer: D. Odd-indexed points get 4 and even-indexed interior points get 2. (Key point: Weights)

3. What does the sum start with before the loop?
   A. f(a) + f(b)
   B. 0
   C. f(a) only
   D. 4 f(a) + f(b)
   Answer: A. The end points have weight 1. (Key point: Initial sum)

4. How is the integral finally computed?
   A. I = (3h/8) × sum
   B. I = (h/3) × sum
   C. I = h × sum
   D. I = (h/2) × sum
   Answer: B. Simpson's 1/3 rule multiplies the weighted sum by h/3. (Key point: Final formula)

## Related in Numerical Method

- [Composite Simpson 3/8: e^x + sin 2x](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asimpson-38-composite) — Numerical Method, chapter 5
- [Derive Simpson's 1/3 rule](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Aderive-simpson-13) — Numerical Method, chapter 5
- [Simpson 1/3: 4e^x/(1+x³)](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asimpson-13-4e-x) — Numerical Method, chapter 5
- [Simpson 3/8 pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asimpson-38-pseudocode) — Numerical Method, chapter 5

## Related in other subjects

- [Contour integral ∫dx/(1+x²)²](https://www.bctengineer.com/question.md?key=semester_4%2Fapplied_mathematics%3Areal-integral-1-over-1-x2-sq) — Applied Mathematics, chapter 1
- [Offsets area: trapezoidal, Simpson, coordinate](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Aarea-offsets-curved-boundary-trapezoidal-simpson-coordinate) — Surveying I, chapter 9
- [DUIS: ∫ e^(−ax) sin x / x dx](https://www.bctengineer.com/question.md?key=semester_1%2Fmathematics_i%3Aduis-e-ax-sinx-over-x) — Mathematics I, chapter 2, asked 8×
- [Sequential covering for rule induction](https://www.bctengineer.com/question.md?key=semester_7%2Fdata_mining%3Asequential-covering) — Data Mining, chapter 3
