← 2019 Paper 2

UPSC 2019 Maths Optional Paper 2 Q5e — Step-by-Step Solution

10 marks · Section B

Algorithms and flowcharts for numerical analysis problems · Numerical Analysis · asked 2× in 13 yrs · Read the full method →

Question

Draw a flow chart and write a basic algorithm (in FORTRAN/C/C++) for evaluating y=06dx1+x2y=\displaystyle\int_0^6 \frac{dx}{1+x^2} using Trapezoidal rule.

Technique

Composite trapezoidal rule; end ordinates weight 1, interior ordinates weight 2, multiply by h/2h/2.

Solution

Setup. Composite trapezoidal rule on [a,b]=[0,6][a,b]=[0,6] with nn equal sub-intervals, h=(ba)/nh=(b-a)/n, xi=a+ihx_i=a+ih:

abf(x)dxh2[f(x0)+f(xn)+2i=1n1f(xi)],f(x)=11+x2.\int_a^b f(x)\,dx\approx \frac{h}{2}\Big[f(x_0)+f(x_n)+2\sum_{i=1}^{n-1}f(x_i)\Big],\qquad f(x)=\frac{1}{1+x^2}.

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 06dx1+x2=tan16=1.405648\int_0^6\frac{dx}{1+x^2}=\tan^{-1}6=1.405648:

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 O(h2)\sim O(h^2): halving hh from 1.00.51.0\to0.5 cuts the error roughly four-fold (5.2×1031.7×1045.2\times10^{-3}\to1.7\times10^{-4} — even better here as the curve is smooth and gently varying). ✓

We post more of this — worked solutions, CSAT trap breakdowns, guide chapters — a few times a week on Telegram. Free, no sign-in. Join

This solution is part of the Maths Coverage Map — 13 years, mapped. Get the take-away PDF free.