How to solve a pair of nonlinear equations using Python?
Last Updated :
28 Apr, 2025
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.
What is a nonlinear equation?
A nonlinear equation is an equation in which the minimum degree of at least one variable term is 2 or more than two and the relationship between a nonlinear equation's variables cannot be represented by a straight line when plotted on a graph.
Prerequisite
Install these Python libraries using the following commands in your terminal:.
pip install numpy
pip install scipy
pip install sympy
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]
Similar Reads
Solve Linear Equations using eval() in Python Linear equations using one variable of the form a + bx = c + dx can be solved in Python using eval() function. The input type will be a linear equation in the form of a string. Syntax: eval(expression, globals=None, locals=None) Here, we will transform the equation into an expression of real and ima
3 min read
How to Solve a System of Equations using Inverse of Matrices? In mathematics, a matrix is an array of numbers arranged in a rectangular pattern and separated into rows and columns. They're commonly depicted by enclosing all of the integers within square brackets.In this article, we will discuss how to solve a system of equations using the inverse of matrices i
12 min read
Python - Solve the Linear Equation of Multiple Variable Prerequisite: Sympy.solve() In this article, we will discuss how to solve a linear equation having more than one variable. For example, suppose we have two variables in the equations. Equations are as follows: x+y =1 x-y =1 When we solve this equation we get x=1, y=0 as one of the solutions. In Pyth
2 min read
How to Solve Equations with Math.js? Linear and non-linear equations can be solved using Math.js, a powerful mathematics library for JavaScript. For linear equations, Math.js provides functions to handle both single equations and systems of equations using matrix operations. For non-linear equations, the library supports numerical meth
3 min read
Solve Linear Equation and return 3D Graph in Python In this article, we will make the 3D graph by solving the linear equations using Python. Solve Linear Equation in Python Here we are going to create a different variable for assigning the value into a linear equation and then calculate the value by using linalg.solve() methods. Python3 # Python prog
2 min read
Solve Systems of Equations Using Matrices A system of linear equations is a collection of two or more linear equations involving the same set of variables. It is a set of equations where each equation represents a straight line (or hyperplane in higher dimensions) when graphed. The goal is to find the values for the variables that satisfy a
5 min read