Topic: Python SciPy – From Easy to Top: Part 1 of 6: Introduction and Basics
---
1. What is SciPy?
• SciPy is an open-source Python library used for scientific and technical computing.
• Built on top of NumPy, it provides many user-friendly and efficient numerical routines such as routines for numerical integration, optimization, interpolation, eigenvalue problems, algebraic equations, and others.
---
2. Installing SciPy
If you don’t have SciPy installed yet, use:
---
3. Importing SciPy Modules
SciPy is organized into sub-packages for different tasks. Example:
---
4. Key SciPy Sub-packages
•
•
•
•
•
•
---
5. Basic Example: Numerical Integration
Calculate the integral of sin(x) from 0 to pi:
---
6. Basic Example: Root Finding
Find the root of the function f(x) = x^2 - 4:
---
7. SciPy vs NumPy
• NumPy focuses on basic array operations and linear algebra.
• SciPy extends functionality with advanced scientific algorithms.
---
8. Summary
• SciPy is essential for scientific computing in Python.
• It contains many specialized sub-packages.
• Understanding SciPy’s structure helps solve complex numerical problems easily.
---
Exercise
• Calculate the integral of e^(-x^2) from -infinity to +infinity using
• Find the root of cos(x) - x = 0 using
---
#Python #SciPy #ScientificComputing #NumericalIntegration #Optimization
https://t.iss.one/DataScienceM
---
1. What is SciPy?
• SciPy is an open-source Python library used for scientific and technical computing.
• Built on top of NumPy, it provides many user-friendly and efficient numerical routines such as routines for numerical integration, optimization, interpolation, eigenvalue problems, algebraic equations, and others.
---
2. Installing SciPy
If you don’t have SciPy installed yet, use:
pip install scipy
---
3. Importing SciPy Modules
SciPy is organized into sub-packages for different tasks. Example:
import scipy.integrate
import scipy.optimize
import scipy.linalg
---
4. Key SciPy Sub-packages
•
scipy.integrate
— Numerical integration and ODE solvers.•
scipy.optimize
— Optimization and root finding.•
scipy.linalg
— Linear algebra routines (more advanced than NumPy’s).•
scipy.signal
— Signal processing.•
scipy.fft
— Fast Fourier Transforms.•
scipy.stats
— Statistical functions.---
5. Basic Example: Numerical Integration
Calculate the integral of sin(x) from 0 to pi:
import numpy as np
from scipy import integrate
result, error = integrate.quad(np.sin, 0, np.pi)
print("Integral of sin(x) from 0 to pi:", result)
---
6. Basic Example: Root Finding
Find the root of the function f(x) = x^2 - 4:
from scipy import optimize
def f(x):
return x**2 - 4
root = optimize.root_scalar(f, bracket=[0, 3])
print("Root:", root.root)
---
7. SciPy vs NumPy
• NumPy focuses on basic array operations and linear algebra.
• SciPy extends functionality with advanced scientific algorithms.
---
8. Summary
• SciPy is essential for scientific computing in Python.
• It contains many specialized sub-packages.
• Understanding SciPy’s structure helps solve complex numerical problems easily.
---
Exercise
• Calculate the integral of e^(-x^2) from -infinity to +infinity using
scipy.integrate.quad
.• Find the root of cos(x) - x = 0 using
scipy.optimize.root_scalar
.---
#Python #SciPy #ScientificComputing #NumericalIntegration #Optimization
https://t.iss.one/DataScienceM
❤3🔥1
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.iss.one/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.iss.one/DataScienceM
❤5