Okay, we start by understanding that both the Trapezoidal Rule and Simpson's Rule are used to approximate the value of a definite integral (∫f(x)dx). Here, 'n' refers to the number of evenly-spaced intervals to use in the approximation.
As I can't refer to the Python code, I'll assume we have a function f(x), and we want to find the approximate definite integral of this function between the limits of a and b.
1. **Trapezoidal Rule**
Here's how you can do it:
Step 1: You start by dividing the area under the curve of the function into 'n' evenly-spaced intervals/trapezoids, of which each pair of points (x[i], y[i]) and (x[i+1], y[i+1]) forms a trapezoid.
Step 2: Calculate the step size, h = (b - a) / n.
Step 3: The x values can be generated from a to b using the step-size h.
Step 4: The y values are just the function evaluated at each of the x values.
Step 5: Compute the sum of the areas of the trapezoids. According to the formula of the trapezoidal rule, it is (h / 2) * (y[0] + 2 * Σ(y[i]) + y[n]), where the summation Σ(y[i]) is over i from 1 to n-1.
2. **Simpson's Rule**
This rule is similar to the trapezoidal rule, but it fits parabolas to every two intervals instead of trapezoids. Here are the steps:
Step 1: It starts by dividing the area under the curve of the function into 'n' evenly-spaced intervals/parabolas.
Step 2: Like before, calculate the step size, h = (b - a) / n.
Step 3: Generate the x values from a to b using the step-size h.
Step 4: Similarly, the y values are just the function evaluated at each of the x values.
Step 5: Compute the sum of the areas of the parabolas. According to the Simpson's rule formula, it is (h / 3) * (y[0] + 4 * Σ(y[2i - 1]) + 2 * Σ(y[2i]) + y[n]), where first summation Σ(y[2i - 1]) is over i from 1 to n/2, and second summation Σ(y[2i]) is over i from 2 to n/2 - 1.
Again, note that the actual results will highly depend on the specific function, the limits of a and b, and the value of n chosen.