16.5 C
London
Monday, July 7, 2025
HomePythonDescriptive Statistics in PythonHow to Solve a System of Equations in Python (3 Examples)

How to Solve a System of Equations in Python (3 Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

To solve a system of equations in Python, we can use functions from the NumPy library.

The following examples show how to use NumPy to solve several different systems of equations in Python.

Example 1: Solve System of Equations with Two Variables

Suppose we have the following system of equations and we’d like to solve for the values of x and y:

5x + 4y = 35

2x + 6y = 36

The following code shows how to use NumPy to solve for the values of x and y:

import numpy as np

#define left-hand side of equation
left_side = np.array([[5, 4], [2, 6]])

#define right-hand side of equation
right_side = np.array([35, 36])

#solve for x and y
np.linalg.inv(left_side).dot(right_side)

array([3., 5.])

This tells us that the value for x is 3 and the value for y is 5.

Example 2: Solve System of Equations with Three Variables

Suppose we have the following system of equations and we’d like to solve for the values of x, y, and z:

4x + 2y + 1z = 34

3x + 5y – 2z = 41

2x + 2y + 4z = 30

The following code shows how to use NumPy to solve for the values of x, y, and z:

import numpy as np

#define left-hand side of equation
left_side = np.array([[4, 2, 1], [3, 5, -2], [2, 2, 4]])

#define right-hand side of equation
right_side = np.array([34, 41, 30])

#solve for x, y, and z
np.linalg.inv(left_side).dot(right_side)

array([5., 6., 2.])

This tells us that the value for x is 5, the value for y is 6, and the value for z is 2.

Example 3: Solve System of Equations with Four Variables

Suppose we have the following system of equations and we’d like to solve for the values of w, x, y, and z:

6w + 2x + 2y + 1z = 37

2w + 1x + 1y + 0z = 14

3w + 2x + 2y + 4z = 28

2w + 0x + 5y + 5z = 28

The following code shows how to use NumPy to solve for the values of w, x, y, and z:

import numpy as np

#define left-hand side of equation
left_side = np.array([[6, 2, 2, 1], [2, 1, 1, 0], [3, 2, 2, 4], [2, 0, 5, 5]])

#define right-hand side of equation
right_side = np.array([37, 14, 28, 28])

#solve for w, x, y, and z
np.linalg.inv(left_side).dot(right_side)
 
array([4., 3., 3., 1.])

This tells us that the value for w is 4, x is 3, y is 3, and z is 1.

Additional Resources

The following tutorials explain how to solve a system of equations using other statistical software:

How to Solve a System of Equations in R
How to Solve a System of Equations in Excel

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories