Skip to content

Commit 372ccb4

Browse files
authored
Update README.md
1 parent 222e05e commit 372ccb4

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

README.md

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,112 @@
1-
# Integration-Using-Trapezoidal-Rule-Numerical-Method-Implementation-in-Python
1+
# Integration Using Trapezoidal Rule Numerical Method Implementation in Python
2+
23
This repository contains a Python implementation of the Trapezoidal Rule for numerical integration. The code integrates a given function within specified limits and computes the area under the curve.
4+
5+
## Table of Contents
6+
- [Trapezoidal Rule Theory](#trapezoidal-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+
## Trapezoidal Rule Theory
18+
The Trapezoidal Rule is a numerical technique to approximate the definite integral of a function. The idea is to divide the area under the curve into trapezoids rather than rectangles, which provides a more accurate estimate than the rectangular method.
19+
20+
### Formula:
21+
The formula for the Trapezoidal Rule is:
22+
\[
23+
\int_{a}^{b} f(x) \, dx \approx \frac{(b - a)}{2n} \left[f(x_0) + 2 \sum_{i=1}^{n-1} f(x_i) + f(x_n)\right]
24+
\]
25+
where \( n \) is the number of subintervals, \( a \) and \( b \) are the lower and upper limits of integration, respectively.
26+
27+
## Dependencies
28+
No external libraries are required for this script.
29+
30+
## Installation
31+
Simply clone the repository and run the script using any Python interpreter.
32+
33+
## Usage
34+
1. Clone the repository.
35+
2. Ensure the script is in the directory you want to work in.
36+
3. Run the script using Python:
37+
```sh
38+
python trapezoidal_rule.py
39+
```
40+
4. Provide the required inputs when prompted:
41+
- Enter the lower limit.
42+
- Enter the upper limit.
43+
- Enter the number of subintervals (\( N \)).
44+
45+
## Code Explanation
46+
The code begins by defining the function to be integrated. It then takes the lower and upper limits of integration, and the number of subintervals as inputs. The script divides the interval into smaller subintervals and calculates the area of trapezoids under the curve to approximate the integral.
47+
48+
Below is a snippet from the code illustrating the main logic:
49+
50+
```python
51+
def fnc(x):
52+
return x * x
53+
54+
a = float(input("Enter lower limit: "))
55+
b = float(input("Enter upper limit: "))
56+
n = int(input("Enter the number of subintervals (N): "))
57+
58+
area = 0
59+
a_i = a
60+
diff = (b - a) / n
61+
62+
for i in range(1, n + 1):
63+
b_i = a_i + (diff * i)
64+
h = b_i - a
65+
area += h * (fnc(a) + fnc(b_i)) / 2
66+
a = b_i
67+
68+
print("The approximate area under the curve is:", area)
69+
```
70+
71+
The code calculates and prints the approximate area under the curve using the Trapezoidal Rule formula.
72+
73+
## Example
74+
Below is an example of how to use the script:
75+
76+
**Run the script**:
77+
```sh
78+
python trapezoidal_rule.py
79+
```
80+
81+
**Enter the input values**:
82+
```
83+
Enter lower limit: 0
84+
Enter upper limit: 1
85+
Enter the number of subintervals (N): 10
86+
```
87+
88+
**Output**:
89+
```
90+
The approximate area under the curve is: 0.335... (This value will vary depending on the approximation)
91+
```
92+
93+
## Files in the Repository
94+
- `trapezoidal_rule.py`: The main script for performing numerical integration using the Trapezoidal Rule.
95+
96+
## Input Parameters
97+
The script prompts for the following input values:
98+
- **Lower Limit** (`a`): The lower bound of the definite integral.
99+
- **Upper Limit** (`b`): The upper bound of the definite integral.
100+
- **Number of Subintervals** (`n`): The number of subintervals to divide the area under the curve.
101+
102+
## Troubleshooting
103+
1. **Input Values**: Ensure that the input values are appropriate for the function being integrated and cover the range of interest.
104+
2. **Number of Subintervals**: A higher number of subintervals will provide a more accurate approximation but may increase computation time.
105+
3. **Python Version**: This script is compatible with Python 3. Ensure you have Python 3 installed.
106+
107+
## Author
108+
Script created by sudipto3331.
109+
110+
---
111+
112+
This documentation should guide you through understanding, installing, and using the Trapezoidal Rule script for numerical integration. For further issues or feature requests, please open an issue in the repository on GitHub. Feel free to contribute by creating issues and submitting pull requests. Happy coding!

0 commit comments

Comments
 (0)