Topic: Python SciPy – From Easy to Top: Part 2 of 6: Numerical Integration and Differentiation
---
1. Numerical Integration Overview
• Numerical integration approximates the area under curves when an exact solution is difficult or impossible.
• SciPy provides several methods like quad, dblquad, and trapz.
---
2. Using `scipy.integrate.quad`
This function computes the definite integral of a function of one variable.
Example: Integrate cos(x) from 0 to pi divided by 2
---
3. Double Integration with `dblquad`
Integrate a function of two variables over a rectangular region.
Example: Integrate f(x, y) = x times y over x from 0 to 1, y from 0 to 2
---
4. Using the Trapezoidal Rule: `trapz`
Useful for integrating discrete data points.
Example:
---
5. Numerical Differentiation with `derivative`
SciPy’s
Example: Derivative of sin(x) at x equals pi divided by 4
---
6. Limitations of `derivative`
•
• Suitable for simple derivative calculations but not for complex cases.
---
7. Summary
•
•
•
•
---
Exercise
• Compute the integral of e to the power of negative x squared from 0 to 1 using
• Calculate the derivative of cos(x) at 0.
• Use
---
#Python #SciPy #NumericalIntegration #Differentiation #ScientificComputing
https://t.me/DataScienceM
---
1. Numerical Integration Overview
• Numerical integration approximates the area under curves when an exact solution is difficult or impossible.
• SciPy provides several methods like quad, dblquad, and trapz.
---
2. Using `scipy.integrate.quad`
This function computes the definite integral of a function of one variable.
Example: Integrate cos(x) from 0 to pi divided by 2
import numpy as np
from scipy import integrate
result, error = integrate.quad(np.cos, 0, np.pi/2)
print("Integral of cos(x) from 0 to pi/2:", result)
---
3. Double Integration with `dblquad`
Integrate a function of two variables over a rectangular region.
Example: Integrate f(x, y) = x times y over x from 0 to 1, y from 0 to 2
def f(x, y):
return x * y
result, error = integrate.dblquad(f, 0, 1, lambda x: 0, lambda x: 2)
print("Double integral result:", result)
---
4. Using the Trapezoidal Rule: `trapz`
Useful for integrating discrete data points.
Example:
import numpy as np
from scipy import integrate
x = np.linspace(0, np.pi, 100)
y = np.sin(x)
area = integrate.trapz(y, x)
print("Approximate integral using trapz:", area)
---
5. Numerical Differentiation with `derivative`
SciPy’s
derivative function approximates the derivative of a function at a point.Example: Derivative of sin(x) at x equals pi divided by 4
from scipy.misc import derivative
import numpy as np
def f(x):
return np.sin(x)
dx = derivative(f, np.pi/4, dx=1e-6)
print("Derivative of sin(x) at pi/4:", dx)
---
6. Limitations of `derivative`
•
derivative uses finite difference methods, which can be noisy for non-smooth functions.• Suitable for simple derivative calculations but not for complex cases.
---
7. Summary
•
quad is powerful for one-dimensional definite integrals.•
dblquad handles two-variable integration.•
trapz approximates integration from sampled data.•
derivative provides numerical differentiation.---
Exercise
• Compute the integral of e to the power of negative x squared from 0 to 1 using
quad.• Calculate the derivative of cos(x) at 0.
• Use
trapz to approximate the integral of x squared over \[0, 5] using 50 points.---
#Python #SciPy #NumericalIntegration #Differentiation #ScientificComputing
https://t.me/DataScienceM
❤5