Skip to content

Commit b0c243c

Browse files
authored
Update README.md
1 parent 642ab9a commit b0c243c

File tree

1 file changed

+147
-1
lines changed

1 file changed

+147
-1
lines changed

README.md

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,148 @@
1-
# Integration-Using-Romberg-Rule-Numerical-Method-Implementation-in-Python
1+
# Integration Using Romberg Rule Numerical Method Implementation in Python
2+
23
This repository contains a Python implementation of the Romberg Rule for numerical integration. The code allows users to choose among different numerical methods, specifically Trapezoidal, Simpson's 1/3, and Simpson's 3/8, for integration of the function \( f(x) = x^3 + x^2 + x + 1 \).
4+
5+
### Table of Contents
6+
- [Romberg Rule Theory](#romberg-rule-theory)
7+
- [Dependencies](#dependencies)
8+
- [Installation](#installation)
9+
- [Usage](#usage)
10+
- [Code Explanation](#code-explanation)
11+
- [Example](#example)
12+
- [Files in the Repository](#files-in-the-repository)
13+
- [Input Parameters](#input-parameters)
14+
- [Troubleshooting](#troubleshooting)
15+
- [Author](#author)
16+
17+
### Romberg Rule Theory
18+
The Romberg Rule is a sophisticated numerical integration technique that uses polynomial approximations of the integral and combines them in a way that increases accuracy. It is particularly useful for integrating functions over a specific interval by combining methods of varying orders of accuracy.
19+
20+
The integration results from different methods are used to create a table, allowing for an extrapolation step that refines the estimate.
21+
22+
**Methods:**
23+
1. **Trapezoidal Rule**: Approximates the area under the curve using trapezoids.
24+
2. **Simpson's 1/3 Rule**: Uses parabolic arcs to approximate sections of the curve. Requires an even number of intervals.
25+
3. **Simpson's 3/8 Rule**: A slight variation of Simpson's 1/3 that uses cubic polynomials for approximation.
26+
27+
### Dependencies
28+
This implementation does not require any external libraries; it uses standard Python functions.
29+
30+
### Installation
31+
No additional installation is required. Ensure you have Python installed on your system.
32+
33+
### Usage
34+
1. Clone the repository.
35+
2. Run the script using Python:
36+
```sh
37+
python romberg_integration.py
38+
```
39+
3. Input the required parameters when prompted:
40+
- Enter the lower limit of integration.
41+
- Enter the upper limit of integration.
42+
- Enter the number of intervals.
43+
4. Choose a method for integration when prompted.
44+
45+
### Code Explanation
46+
The code defines the function for the integration and implements various numerical methods. The main function `romberg_integration` applies the Romberg method to combine the results from the selected numerical method.
47+
48+
Below is a snippet from the code illustrating the main logic:
49+
50+
```python
51+
def function(x):
52+
return x**3 + x**2 + x + 1
53+
54+
def trapezoidal(function, a, b, n):
55+
h = (b - a) / n
56+
integration = function(a) + function(b)
57+
for i in range(1, n):
58+
k = a + i * h
59+
integration += 2 * function(k)
60+
integration *= h / 2
61+
return integration
62+
63+
def simp_one_third(function, a, b, n):
64+
h = (b - a) / n
65+
integration = function(a) + function(b)
66+
for interval in range(1, n):
67+
x = a + interval * h
68+
if interval % 2 == 0:
69+
integration += 2 * function(x)
70+
else:
71+
integration += 4 * function(x)
72+
integration *= h / 3
73+
return integration
74+
75+
def simp_third_eight(function, a, b, n):
76+
h = (b - a) / n
77+
integration = function(a) + 3 * function(a + h) + 3 * function(a + 2 * h) + function(b)
78+
for i in range(3, n, 2):
79+
integration += 4 * function(a + i * h)
80+
for i in range(4, n, 2):
81+
integration += 2 * function(a + i * h)
82+
integration *= 3 * h / 8
83+
return integration
84+
85+
def romberg_integration(method, function, a, b, n):
86+
table = []
87+
for i in range(n + 1):
88+
row = [0] * (n + 1)
89+
table.append(row)
90+
for i in range(0, n + 1):
91+
if method == "trapezoidal":
92+
table[i][0] = trapezoidal(function, a, b, 2**i)
93+
elif method == "simpson_one_third":
94+
table[i][0] = simp_one_third(function, a, b, 2**i)
95+
elif method == "simpson_three_eight":
96+
table[i][0] = simp_third_eight(function, a, b, 3 * 2**i)
97+
for j in range(1, n + 1):
98+
for i in range(j, n + 1):
99+
table[i][j] = (4 * j * table[i][j - 1] - table[i - 1][j - 1]) / (4 * j - 1)
100+
return table[n][n]
101+
```
102+
103+
### Example
104+
Below is an example of how to use the script:
105+
106+
1. **Run the script**:
107+
```sh
108+
python romberg_integration.py
109+
```
110+
111+
2. **Enter the input values**:
112+
```
113+
Enter lower limit: 0
114+
Enter upper limit: 1
115+
Enter number of intervals: 4
116+
Choose method to solve Romberg integration:
117+
1. Trapezoidal
118+
2. Simpson's 1/3
119+
3. Simpson's 3/8
120+
Enter method choice (1, 2, or 3): 2
121+
```
122+
123+
3. **Output**:
124+
- The script will calculate the integration using the chosen method and print the result:
125+
```
126+
Integration result using Romberg with simpson_one_third method: 1.3333
127+
```
128+
129+
### Files in the Repository
130+
- `romberg_integration.py`: The main script for performing the integration using the Romberg method.
131+
132+
### Input Parameters
133+
The script prompts for the following input values:
134+
- Lower limit of integration (`a`).
135+
- Upper limit of integration (`b`).
136+
- Number of intervals (`n`).
137+
138+
### Troubleshooting
139+
1. **Method Selection**: Ensure an appropriate method is chosen (1, 2, or 3) for integration.
140+
2. **Function Definition**: The function \( f(x) = x^3 + x^2 + x + 1 \) is hardcoded. Modify this function as necessary for different integrands.
141+
3. **Python Version**: This script is compatible with Python 3. Ensure you have Python 3 installed.
142+
143+
## Author
144+
Script created by [Your Name].
145+
146+
---
147+
148+
This documentation should guide you through understanding, installing, and using the Romberg numerical integration method script. 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!

0 commit comments

Comments
 (0)