-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.py
More file actions
192 lines (170 loc) · 6.14 KB
/
integration.py
File metadata and controls
192 lines (170 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# Integral Calculus in Python with Examples
#
# Integral calculus is a fundamental branch of mathematics that deals with the accumulation of quantities
# and the areas under or between curves. In Python, the SymPy library provides powerful tools for symbolic
# mathematics, including integration. This script demonstrates the basics of performing integrals in Python
# using SymPy, accompanied by practical examples.
import sympy as sp
# Define symbolic variables
x, y = sp.symbols('x y')
# ## Indefinite Integrals
# An **indefinite integral** represents a family of functions whose derivative is the integrand.
# It does not include the limits of integration.
# ### Example 1: Basic Indefinite Integral
# Compute the indefinite integral of \( f(x) = x^2 \):
f1 = x**2
F1 = sp.integrate(f1, x)
print("Example 1: Indefinite Integral of x^2")
print(F1) # Output: x**3/3
print()
# ### Example 2: Integral of Trigonometric Function
# Compute the indefinite integral of \( f(x) = \sin(x) \):
f2 = sp.sin(x)
F2 = sp.integrate(f2, x)
print("Example 2: Indefinite Integral of sin(x)")
print(F2) # Output: -cos(x)
print()
# ## Definite Integrals
# A **definite integral** computes the accumulation of quantities between specific limits,
# effectively calculating the area under the curve.
# ### Example 3: Definite Integral of a Polynomial
# Compute the definite integral of \( f(x) = x^2 \) from \( x = 0 \) to \( x = 3 \):
f3 = x**2
F3 = sp.integrate(f3, (x, 0, 3))
print("Example 3: Definite Integral of x^2 from 0 to 3")
print(F3) # Output: 9
print()
# ### Example 4: Definite Integral of an Exponential Function
# Compute the definite integral of \( f(x) = e^x \) from \( x = 1 \) to \( x = 2 \):
f4 = sp.exp(x)
F4 = sp.integrate(f4, (x, 1, 2))
print("Example 4: Definite Integral of e^x from 1 to 2")
print(F4) # Output: exp(2) - exp(1)
# Numerical evaluation
numerical_value_F4 = F4.evalf()
print("Numerical Evaluation of Example 4:")
print(numerical_value_F4) # Approximately 4.670774
print()
# ## Integration Techniques
# SymPy can handle various integration techniques symbolically. Here are a few examples:
# ### Example 5: Integration by Substitution
# Compute the integral \( \int 2x e^{x^2} dx \):
f5 = 2 * x * sp.exp(x**2)
F5 = sp.integrate(f5, x)
print("Example 5: Integral of 2x * e^(x^2) dx")
print(F5) # Output: exp(x**2)
print()
# ### Example 6: Integration of Rational Functions
# Compute the integral \( \int \frac{1}{x} dx \):
f6 = 1 / x
F6 = sp.integrate(f6, x)
print("Example 6: Integral of 1/x dx")
print(F6) # Output: log(x)
print()
# ### Example 7: Integral Involving Trigonometric Identities
# Compute the integral \( \int \sin^2(x) dx \):
f7 = sp.sin(x)**2
F7 = sp.integrate(f7, x)
print("Example 7: Integral of sin^2(x) dx")
print(F7) # Output: x/2 - sin(2*x)/4
print()
# ## Using the `functions.py` Module
# Assuming you have a `functions.py` module with utility functions for differentiation,
# integration, limits, etc., you can utilize the `integrate` function as follows:
# ### Example 8: Using `functions.py` to Compute an Integral
#
# ```python
# from functions import integrate
# import sympy as sp
#
# x = sp.symbols('x')
# expr = sp.sin(x) * sp.exp(x)
#
# # Compute the indefinite integral
# integral = integrate(expr, x)
# print("Indefinite Integral:", integral)
#
# # Compute the definite integral from 0 to pi
# def_integral = integrate(expr, x, limits=(0, sp.pi))
# print("Definite Integral from 0 to pi:", def_integral.evalf())
# ```
# Further Enhancements
# If you'd like to extend your `functions.py` module to include additional integration features,
# consider the following edits:
# ### Update `functions.py` to Handle Definite Integrals
# ```python:functions.py
# # functions.py
# import sympy as sp
#
# # Define a function for differentiation
# def differentiate(expr, var):
# return sp.diff(expr, var)
#
# # Define a function for integration
# def integrate(expr, var, limits=None):
# """
# Integrate the expression with respect to the given variable.
#
# :param expr: The expression to integrate.
# :param var: The variable to integrate with respect to.
# :param limits: A tuple (lower, upper) for definite integrals.
# :return: The integral of the expression.
# """
# if limits:
# return sp.integrate(expr, (var, limits[0], limits[1]))
# else:
# return sp.integrate(expr, var)
#
# # Define a function for finding limits
# def limit(expr, var, point, direction='+-'):
# return sp.limit(expr, var, point, dir=direction)
#
# # Define a function for finding critical points
# def critical_points(expr, var):
# derivative = sp.diff(expr, var)
# return sp.solve(derivative, var)
#
# # Example usage
# if __name__ == "__main__":
# x = sp.symbols('x')
# expr = x**2 + 3*x + 2
#
# print("Function:", expr)
# print("Derivative:", differentiate(expr, x))
# print("Integral:", integrate(expr, x))
# print("Limit as x approaches 1:", limit(expr, x, 1))
# print("Critical Points:", critical_points(expr, x))
# ```
# ### Example Usage in `functions.py`
# ```python:main.py
# from functions import integrate
# import sympy as sp
#
# x = sp.symbols('x')
# expr = sp.sin(x) * sp.exp(x)
#
# # Indefinite Integral
# indefinite = integrate(expr, x)
# print("Indefinite Integral:", indefinite)
#
# # Definite Integral from 0 to pi
# definite = integrate(expr, x, limits=(0, sp.pi))
# print("Definite Integral from 0 to pi:", definite.evalf())
#
# # Numeric evaluation:
# # (exp(pi) + 1)/2 ≈ 12.07034631
# ```
# **Output:**
# ```
# Indefinite Integral: -exp(x)*cos(x)/2 + exp(x)*sin(x)/2
# Definite Integral from 0 to pi: (exp(pi) + 1)/2
# ```
# Numeric evaluation:
# ```
# (exp(pi) + 1)/2 ≈ (23.14069263 + 1)/2 ≈ 12.07034631
# ```
# Conclusion
# Integral calculus in Python, especially using SymPy, offers a powerful and flexible way to perform both symbolic and numerical integrations.
# Whether you're dealing with simple polynomials or more complex functions involving trigonometric identities and exponential growth,
# SymPy provides the tools needed to compute integrals efficiently. By integrating these capabilities into your Python projects,
# you can automate and simplify many mathematical computations.