BIT203 ยท 2079 ยท 10 marks
What do you mean by numerical integration? Write any one application of numerical integration. Write an algorithm and c program to implement multi-segment trapezoidal rule.
What do you mean by numerical integration? Write any one application of numerical integration. Write an algorithm and c program to implement multi-segment trapezoidal rule.[10]
Quick answer
Numerical Integration (also called numerical quadrature) is the process of computing the approximate value of a definite integral using numerical methods when: The integrand f(x) is too complex to integrate analytically, or; The function is given only as a set of discrete data points (tabular data)...
Numerical Integration: Multi-Segment Trapezoidal Rule
1. What is Numerical Integration? (2 marks)
Numerical Integration (also called numerical quadrature) is the process of computing the approximate value of a definite integral using numerical methods when:
- The integrand f(x) is too complex to integrate analytically, or
- The function is given only as a set of discrete data points (tabular data), or
- The antiderivative does not exist in closed form.
The general goal is to approximate:
$$I = \int_a^b f(x), dx$$
by replacing the integrand with a simpler approximating function (such as polynomials) and summing the areas of geometric shapes under the curve.
2. Application of Numerical Integration (1 mark)
Engineering Application: Computing the area under a velocity-time curve to find displacement, or calculating the work done by a variable force:
$$W = \int_{x_1}^{x_2} F(x), dx$$
where F(x) may be given only as experimental data points, making analytical integration impossible.
3. Multi-Segment Trapezoidal Rule (7 marks)
Concept
The interval [a, b] is divided into n equal sub-intervals (segments), each of width:
$$h = \frac{b - a}{n}$$
The nodes are: $x_0 = a,; x_1 = a+h,; x_2 = a+2h,; \ldots,; x_n = b$
Each sub-interval is approximated by a trapezoid. The formula is:
$$\boxed{I \approx \frac{h}{2}\left[f(x_0) + 2f(x_1) + 2f(x_2) + \cdots + 2f(x_{n-1}) + f(x_n)\right]}$$
Or equivalently:
$$I \approx \frac{h}{2}\left[f(x_0) + f(x_n) + 2\sum_{i=1}^{n-1} f(x_i)\right]$$
Algorithm
Algorithm: Multi-Segment Trapezoidal Rule
Input : a (lower limit), b (upper limit), n (number of segments)
Output: Approximate value of integral I
Step 1: START
Step 2: Read a, b, n
Step 3: Compute h = (b - a) / n
Step 4: Set sum = f(a) + f(b) [first and last terms]
Step 5: Set i = 1
Step 6: WHILE i <= n-1 DO
x = a + i * h
sum = sum + 2 * f(x) [middle terms multiplied by 2]
i = i + 1
END WHILE
Step 7: I = (h / 2) * sum
Step 8: Print I
Step 9: STOP
C Program
#include <stdio.h>
#include <math.h>
/* Define the function to be integrated */
/* Example: f(x) = x^2, change as needed */
double f(double x) {
return x * x; /* Replace with desired function */
}
int main() {
double a, b, h, sum, x, result;
int n, i;
/* Input */
printf("Enter lower limit (a): ");
scanf("%lf", &a);
printf("Enter upper limit (b): ");
scanf("%lf", &b);
printf("Enter number of segments (n): ");
scanf("%d", &n);
/* Step size */
h = (b - a) / n;
/* Apply Multi-Segment Trapezoidal Rule */
/* First and last terms */
sum = f(a) + f(b);
/* Middle terms */
for (i = 1; i <= n - 1; i++) {
x = a + i * h;
sum = sum + 2 * f(x);
}
/* Final result */
result = (h / 2) * sum;
printf("\nApproximate value of integral = %.6lf\n", result);
return 0;
}
Sample Output
For $\int_0^1 x^2, dx$ with n = 4:
| i | x_i | f(x_i) = x_i^2 |
|---|---|---|
| 0 | 0.00 | 0.0000 |
| 1 | 0.25 | 0.0625 |
| 2 | 0.50 | 0.2500 |
| 3 | 0.75 | 0.5625 |
| 4 | 1.00 | 1.0000 |
$$h = \frac{1-0}{4} = 0.25$$
$$I \approx \frac{0.25}{2}\left[0 + 1 + 2(0.0625 + 0.25 + 0.5625)\right]$$
$$I \approx 0.125 \times [1 + 2(0.875)] = 0.125 \times 2.75 = \mathbf{0.34375}$$
Exact value = 1/3 = 0.33333... (small error due to finite segments; error decreases as n increases)
Key Points
| Feature | Detail |
|---|---|
| Method type | Newton-Cotes closed formula |
| Order of accuracy | O(h^2) per segment |
| Error decreases | As n increases (h decreases) |
| Easy to implement | Yes, simple loop structure |
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.