This repository contains a Python implementation of Simpson’s 3/8 Rule for numerical integration. The code computes the integral of the function ( F(x) = x^2 + 5x - 9 ) over a specified interval using Simpson's 3/8 Rule.
- Simpson’s 3/8 Rule Theory
- Dependencies
- Installation
- Usage
- Code Explanation
- Example
- Files in the Repository
- Input Parameters
- Troubleshooting
- Author
Simpson's 3/8 Rule is a method for numerical integration that estimates the value of a definite integral by interpolating the integrand using polynomial segments. This rule divides the interval into smaller segments, applying a weighted average at various points to obtain a more accurate approximation of the integral.
Formula: For ( n ) intervals where ( n ) is divisible by 3: [ \int_a^b f(x) , dx \approx \frac{3h}{8} \left[ f(x_0) + 3f(x_1) + 3f(x_2) + 2f(x_3) + \ldots + f(x_n) \right] ] Where:
- ( h = \frac{b-a}{n} )
This implementation does not require any external libraries; it uses standard Python functions.
No additional installation is required. Ensure you have Python installed on your system.
-
Clone the repository.
-
Run the script using Python:
python simpsons_38_rule.py
-
Input the required parameters when prompted:
- Enter the lower limit of integration.
- Enter the upper limit of integration.
- Enter the step size (number of sub-intervals).
-
The script will calculate the integral and display the result.
The code defines a function F(x)
representing the integrand and another function Simpsons_38_interpolation(a, b, n)
to compute the integral using Simpson’s 3/8 Rule. The main program prompts the user for input values and computes the integration.
Below is a snippet from the code illustrating the main logic:
def F(x):
return x**2 + 5*x - 9
def Simpsons_38_interpolation(a, b, n):
h = (b - a) / n
integration = 0
for i in range(n):
x0 = a + i * h
x1 = x0 + h / 3
x2 = x1 + h / 3
x3 = a + (i + 1) * h
val = (h * (F(x0) + 3 * F(x1) + 3 * F(x2) + F(x3))) / 8
integration += val
return integration
print("____Simpson's 3/8 Rule____")
a = float(input("Enter the lower limit: "))
b = float(input("Enter the upper limit: "))
step_size = int(input("Enter the number of intervals (step size): "))
integration = Simpsons_38_interpolation(a, b, step_size + 1)
print("\nIntegrated value = %.6f" % (integration))
Below is an example of how to use the script:
-
Run the script:
python simpsons_38_rule.py
-
Enter the input values:
Enter the lower limit: 0 Enter the upper limit: 2 Enter the number of intervals (step size): 3
-
Output:
- The script will calculate the integration using Simpson's 3/8 Rule and print the result:
Integrated value = 10.666667
simpsons_38_rule.py
: The main script for performing the integration using Simpson's 3/8 Rule.
The script prompts for the following input values:
- Lower limit of integration (
a
). - Upper limit of integration (
b
). - Number of sub-intervals (
n
, which must be a multiple of 3).
- Interval Size: Ensure that the number of intervals entered is a multiple of 3 to apply Simpson’s 3/8 rule.
- Function Definition: The function ( F(x) = x^2 + 5x - 9 ) is hardcoded. Modify this function as necessary to integrate a different function.
- Python Version: This script is compatible with Python 3. Ensure you have Python 3 installed.
Script created by [Your Name].
This documentation should guide you through understanding, installing, and using the Simpson’s 3/8 Rule script for numerical integration. For further issues or feature requests, please open an issue in the repository. Feel free to contribute by creating issues and submitting pull requests. Happy coding!