
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - Finding Roots of Scalar Functions
In numerical analysis, finding the roots of scalar functions is a fundamental task. A root of a scalar function f(x) is a value x such that f(x)=0. This is commonly used in solving equations where the goal is to find the value of x that makes the function zero. SciPy a powerful scientific computing library in Python provides several tools to solve for the roots of scalar functions using various numerical methods.
Key features of Finding root
Here are the key features of finding root of scalar functions in SciPy −
- Multiple Algorithms: SciPy offers a variety of algorithms to solve root-finding problems as mentioned below −
- Bisection Method: This method is ideal for continuous functions with opposite signs at the interval endpoints.
- Newton's Method: It is used when the function is differentiable and its derivative is available.
- Hybrid Methods: These methods which combine several techniques for robust root-finding.
- Handling Nonlinear Equations: Suitable for finding roots of nonlinear scalar functions whether smooth or with multiple roots.
- Initial Guess: Suitable for finding roots of nonlinear scalar functions whether smooth or with multiple roots.
- Initial Guess: Provides flexibility with initial guesses for root location, helping guide the algorithm toward convergence.
- Customizable Tolerance: Control the accuracy of the solution with customizable tolerance values e.g., tol.
- Flexible Constraints: Some methods support constraints or boundaries on variables during root search, especially in optimization contexts.
- Convergence Monitoring: Returns detailed information about the convergence status and messages by allowing users to check if the solution has been found.
- Support for Derivative-Free Methods: Methods such as Nelder-Mead are useful when the derivative of the function is not available or the function is non-smooth.
- Multi-Root Capabilities: Some algorithms can be adapted to find multiple roots or solutions for a set of equations.
Functions to find roots of Scalar functions
Here are the key functions which are used to find the roots of Scalar Functions −
S.No. | Function & Description |
---|---|
1 |
scipy.optimize.root() Finds the roots of a scalar or system of nonlinear equations. |
2 | scipy.optimize.newton() Finds the root of a function using the Newton-Raphson method. |
3 | scipy.optimize.bisect() Finds a root of a function within a specified interval using the bisection method. |
4 | scipy.optimize.brentq() Finds a root of a function within a specified interval using Brent's method. |
5 | scipy.optimize.fsolve() Finds the roots of a function using a numerical method for systems of nonlinear equations. |
6 | scipy.optimize.minimize_scalar() Minimizes a scalar function using various optimization algorithms. |
7 | scipy.optimize.ridder() Finds the root of a function using Ridder's method, a root-finding algorithm based on bracketing. |
Using the Bisection Method
The Bisection method is a simple and robust root-finding method that requires an interval in which the function changes sign. It's particularly useful when we have a continuous function and know that the root exists within the given interval. Here is the using the Bisection method with the help of scipy.optimize.bisect() method for finding the roots of Scalar functions −
from scipy.optimize import bisect # Define the objective function def objective_function(x): return x**2 - 4 # root at x = 2 or x = -2 # Perform the root finding in the interval [1, 3] root = bisect(objective_function, 1, 3) # Display the result print("Root found at:", root)
Following is the output of using the scipy.optimize.bisect() function −
Root found at: 2.0
Using the Newton-Raphson Method
The Newton-Raphson method is an efficient root-finding technique that requires the first derivative of the function. It is often faster than bisection but requires an initial guess near the root. In Scipy we have the function scipy.optimize.newton() to perform the Newton-Raphson method. Following is the example which shows how to use the scipy.optimize.newton() function to find the roots of scalar functions −
from scipy.optimize import newton # Define the objective function and its derivative def objective_function(x): return x**2 - 4 def derivative_function(x): return 2*x # Perform the root finding with an initial guess of 1.5 root = newton(objective_function, x0=1.5, fprime=derivative_function) # Display the result print("Root found at:", root)
Following is the output of using the scipy.optimize.newton() function −
Root found at: 2.0
Using the scipy.optimize.root for general equation
The root function in SciPy can be used for more complex root-finding tasks including systems of equations. In SciPy we have the function scipy.optimize.root() function to find the roots for a general non linear equation. Below is the example which shows how to use the scipy.optimize.root() function −
from scipy.optimize import root # Define the objective function def objective_function(x): return [x[0]**2 - 4, x[1] - x[0]] # Initial guess initial_guess = [1, 1] # Find the root of the system solution = root(objective_function, initial_guess) # Display the result print("Root found at:", solution.x)
Following is the output of using the scipy.optimize.root() function −
Root found at: [2. 2.]