
- 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 Multivariate Functions
Finding Roots of Multivariate Functions in SciPy refers to the process of finding the values of multiple variables, typically represented as a vector that satisfy a system of nonlinear equations i.e., solving for the root or zero of a multivariate function. In other words we can say it involves finding the points where a vector-valued function F(x)=0 where F(x) is a set of equations and x a vector of variables.
How SciPy Helps in Finding Roots?
In SciPy we have the function scipy.optimize.root() which is used to find the roots of systems of multivariate nonlinear equations. It is a part of the scipy.optimize module which provides optimization and root-finding routines. The root function allows us to solve these systems by providing an initial guess and choosing from a variety of methods for solving the system.
Why to Use SciPy to find Multivariate Root?
Here are the reasons why to use SciPy for Multivariate Root Finding −
- Robust and Flexible Algorithms: SciPy provides various solvers such as 'hybr', 'lm', 'broyden1', etc which tailored for different nonlinear systems by making it versatile for diverse problems.
- Handles Complex Systems: It can solve coupled, nonlinear equations that lack analytical solutions.
- Ease of Use: A simple interface lets users define equations, provide initial guesses, and solve efficiently.
- Automatic or Custom Jacobians: SciPy can approximate derivatives or accept user-provided Jacobians for faster convergence.
- Scalable for Large Systems: Solvers such as 'broyden1' handle large systems without needing explicit Jacobians.
- Reliable and Optimized: Built on trusted libraries like MINPACK by ensuring accuracy and performance.
- Integration: Works seamlessly with NumPy, Matplotlib and SymPy for end-to-end workflows.
- Diagnostic Tools: Provides detailed outputs for convergence, residuals, and solution validation.
Syntax
Following is the Syntax of the function scipy.optimize.root() which is used to find the roots of multivariate functions −
scipy.optimize.root( fun, x0, args=(), method='hybr', jac=None, tol=None, options={} )
Parameters
- fun(callable): The function whose roots are sought. It should take an array x and return an array of the same shape.
- x0(array_like): Initial guess for the solution.
- args(tuple, optional): Extra arguments to pass to the function fun.
- method(str, optional): The algorithm to use. Options include 'hybr', 'lm', 'broyden1', 'anderson', etc.
- jac(callable, optional): A function to compute the Jacobian matrix of fun. If not provided then finite differences are used.
- tol(float, optional): Tolerance for termination.
- options(dict, optional): Solver-specific options like max iterations or verbosity level.
Return Value
scipy.optimize.root() returns an OptimizeResult object containing the below values −
- x: The solution array.
- success: A boolean indicating if the solver succeeded.
- message: Description of the cause of termination.
- fun: The residuals at the solution.
Finding the Root Multiple functions
Here in this example we will solve for the roots of multiple function involving multiple variables for the equations f1(x,y) = ex+y2-1 = 0 and f2(x,y) = x3+3x2+1 = 0 −
import numpy as np from scipy.optimize import root # Define the system of nonlinear equations def fun_system(vars): x, y = vars # f1(x, y) = e^x + y^2 - 1 # f2(x, y) = x^3 - 3x + 1 f1 = np.exp(x) + y**2 - 1 f2 = x**3 - 3*x + 1 return [f1, f2] # Return both residuals # Initial guess for [x, y] initial_guess = [0.0, 0.0] # Use root to solve the system of equations result = root(fun_system, initial_guess) # Display the result if result.success: print(f"Roots found: x = {result.x[0]}, y = {result.x[1]}") else: print(f"Root finding failed: {result.message}")
Here is the output of the function scipy.optimize.root() which is used to find the root of multiple functions −
Root finding failed: The iteration is not making good progress, as measured by the improvement from the last ten iterations.
Solving a System with More Equations & Variables
Here is an example which shows how to solve a system with more equations and variables with the help of the function scipy.optimize.root() −
import numpy as np from scipy.optimize import root # Define the system of equations def system_of_equations(x): return [ x[0]**2 + x[1]**2 + x[2]**2 - 9, # x + x + x = 9 x[0] + x[1] - x[2] - 1, # x + x - x = 1 x[0] - x[1] - x[2] - 2 # x - x - x = 2 ] # Initial guess for the solution x0 = [1, 1, 1] # Solve the system using the 'hybr' method (default) result = root(system_of_equations, x0, method='hybr') # Check the result and display the solution if result.success: print("Solution found:", result.x) else: print("Solver failed:", result.message)
Following is the output of the function scipy.optimize.root() which is used to solve a system with more equations and variables −
Solution found: [ 2.70256242 -0.5 1.20256242]