UPSC 2019 Maths Optional Paper 2 Q5e — Step-by-Step Solution
10 marks · Section B
Question
Draw a flow chart and write a basic algorithm (in FORTRAN/C/C++) for evaluating using Trapezoidal rule.
Technique
Composite trapezoidal rule; end ordinates weight 1, interior ordinates weight 2, multiply by .
Solution
Setup. Composite trapezoidal rule on with equal sub-intervals, , :
Step 1 — Flowchart (described)
┌─────────────┐
│ START │
└──────┬──────┘
│
┌───────────▼────────────┐
│ READ a, b, n │ (a=0, b=6, n=number of strips)
└───────────┬────────────┘
│
┌───────────▼────────────┐
│ h = (b - a)/n │
│ s = f(a) + f(b) │ f(x)=1/(1+x*x)
│ i = 1 │
└───────────┬────────────┘
│
┌──────▼───────┐ no
│ i <= n-1 ? ├────────────┐
└──────┬───────┘ │
│ yes │
┌───────────▼────────────┐ │
│ x = a + i*h │ │
│ s = s + 2*f(x) │ │
│ i = i + 1 │ │
└───────────┬────────────┘ │
│ │
└────────(loop)──────┘
│ (when i > n-1)
┌───────────▼────────────┐
│ y = (h/2) * s │
└───────────┬────────────┘
│
┌───────────▼────────────┐
│ PRINT y │
└───────────┬────────────┘
┌───────▼──────┐
│ STOP │
└──────────────┘
Step 2 — Algorithm (pseudocode)
1. START
2. READ a, b, n
3. h ← (b − a)/n
4. s ← f(a) + f(b) where f(x) = 1/(1 + x*x)
5. FOR i = 1 TO n−1
6. x ← a + i*h
7. s ← s + 2*f(x)
8. END FOR
9. y ← (h/2)*s
10. PRINT y
11. STOP
Step 3 — C program
#include <stdio.h>
double f(double x) { return 1.0 / (1.0 + x*x); }
int main(void) {
double a = 0.0, b = 6.0, h, x, s;
int n, i;
printf("Enter number of sub-intervals n: ");
scanf("%d", &n);
h = (b - a) / n;
s = f(a) + f(b); /* end ordinates */
for (i = 1; i <= n - 1; i++) {
x = a + i * h;
s += 2.0 * f(x); /* interior ordinates, weight 2 */
}
s = (h / 2.0) * s;
printf("Integral approx = %.6f\n", s); /* true value = arctan(6) */
return 0;
}
(FORTRAN equivalent: a DO I=1,N-1 loop accumulating S = S + 2.0*F(X) then Y = H/2.0*S.)
Verification
Numerically (Python), exact value :
n=6 h=1.0 trapezoid = 1.410799
n=12 h=0.5 trapezoid = 1.405476
n=60 h=0.1 trapezoid = 1.405640
n=600 h=0.01 trapezoid = 1.405648 (matches arctan 6) ✓
Error : halving from cuts the error roughly four-fold ( — even better here as the curve is smooth and gently varying). ✓