Solve a Pair of Nonlinear Equations Using Python
Below are some ways by which we can solve a pair of nonlinear equations using Python:
- Using fsolve from scipy.optimize
- Using root from scipy.optimize
- Using minimize from scipy.optimize (Optimization Method)
- Using nsolve from SymPy
- Using Newton’s method with NumPy
We will perform all methods on these equations:
Equation 1: x2 + y2 = 25
Equation 2: x2 - y = 0
Solve Non-Linear Equations Using fsolve from SciPy
This Python code uses the fsolve function from the scipy.optimize library to find the numerical solution to a system of nonlinear equations. The equations are defined in the equations function, where eq1 and eq2 represent the equations. The initial guess for the solution is set to [1, 1] for [x,y], and fsolve is used to iteratively use this guess until it reaches to a solution. The final solution is then printed.
Python3
from scipy.optimize import fsolve def equations( vars ): x, y = vars eq1 = x * * 2 + y * * 2 - 25 eq2 = x * * 2 - y return [eq1, eq2] initial_guess = [ 1 , 1 ] solution = fsolve(equations, initial_guess) print ( "Solution:" , solution) |
Output:
Solution: [2.12719012 4.52493781]
Solve a Pair of NonLinear Equations Using root from SciPy
This Python code uses a method called root from the scipy.optimize library to find the solution to a set of math equations. The code starts with a guess for the solution, [1, 1], and the root function uses this guess until it finds the correct answer. The solution is then printed.
Python3
from scipy.optimize import root def equations( vars ): x, y = vars eq1 = x * * 2 + y * * 2 - 25 eq2 = x * * 2 - y return [eq1, eq2] initial_guess = [ 1 , 1 ] solution = root(equations, initial_guess) print ( "Solution:" , solution.x) |
Output:
Solution: [2.12719012 4.52493781]
Solve Non-Linear Equations Using minimize from SciPy
This Python code uses the minimize function from the scipy.optimize library to find the optimal solution for equations. The equations are defined as equation1 and equation2. The objective function is representing the combined error of the equations and it is minimized to find the solution. The initial guess for the solution is set to [1, 1], and the optimized solution is printed using result.x.
Python3
from scipy.optimize import minimize # Define the equations def equation1(x, y): return x * * 2 + y * * 2 - 25 def equation2(x, y): return x * * 2 - y # Define the objective function for optimization def objective(xy): x, y = xy return equation1(x, y) * * 2 + equation2(x, y) * * 2 # Initial guess initial_guess = [ 1 , 1 ] # Perform optimization result = minimize(objective, initial_guess) solution_optimization = result.x print ( "Optimization Method Solution:" , solution_optimization) |
Output:
Optimization Method Solution: [2.12719023 4.52493776]
Solve Non-Linear Equations Using nsolve from SymPy
This Python code uses the sympy library to symbolically define equations and then uses the nsolve function to find the solution. The initial guess for the solution is set to [1, 1], and nsolve iteratively uses this guess until it reaches to a solution. The final solution is then printed.
Python3
from sympy import symbols, Eq, nsolve # Define the variables x, y = symbols( 'x y' ) # Define the equations eq1 = Eq(x * * 2 + y * * 2 , 25 ) eq2 = Eq(x - y, 0 ) # Initial guess for the solution initial_guess = [ 1 , 1 ] # Use nsolve to find the solution solution = nsolve([eq1, eq2], [x, y], initial_guess) print ( "Solution:" , solution) |
Output:
Solution: Matrix([[2.12719012092489], [4.52493781056044]])
Solve Equations in Python Using Newton’s method with NumPy
This Python code defines a Newton’s method implementation (newton_method) to solve a system of nonlinear equations. The method iteratively uses the initial guess [1, 1] by updating it based on the Jacobian matrix and the equations until convergence, and the final solution is then printed.
Python3
import numpy as np def equations( vars ): x, y = vars eq1 = x * * 2 + y * * 2 - 25 eq2 = x * * 2 - y return np.array([eq1, eq2]) def newton_method(initial_guess, tolerance = 1e - 6 , max_iter = 100 ): vars = np.array(initial_guess, dtype = float ) for _ in range (max_iter): J = np.array([[ 2 * vars [ 0 ], 2 * vars [ 1 ]], [ 2 * vars [ 0 ], - 1 ]]) F = equations( vars ) delta = np.linalg.solve(J, - F) vars + = delta if np.linalg.norm(delta) < tolerance: return vars initial_guess = [ 1 , 1 ] solution = newton_method(initial_guess) print ( "Solution:" , solution) |
Output:
Solution: [2.12719012 4.52493781]
How to solve a pair of nonlinear equations using Python?
Solving the nonlinear equation includes finding the values of variables that satisfy the equation. In Python, nonlinear equations can be solved using the SciPy, NumPy, and SymPy libraries. The methods and approaches we will discuss in this article will require the installation of these Python libraries.