Home >Backend Development >Python Tutorial >Three ways to solve equations in python

Three ways to solve equations in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-14 17:14:2832240browse

Three methods of solving equations in python:

Related recommendations: "python video"

Three ways to solve equations in python

Numpy Solving the system of equations

x + 2y = 3
4x + 5y = 6

Of course we can manually write the analytical solution and then write a function to solve it. This is actually just using Python to do "numerical calculations". But in fact, numpy.linalg.solve can directly solve linear equations.

Generally, we assume that the linear equations are in the shape of Ax=b, where A is the coefficient matrix and b is one-dimensional (n-dimensional also Yes, this will be mentioned below), x is an unknown variable. Taking the simplest system of linear equations of two variables above as an example, we can use numpy.linalg.solve to write like this:

In [1]: import numpy as np
  ...: A = np.mat('1,2; 4,5')    # 构造系数矩阵 A
  ...: b = np.mat('3,6').T       # 构造转置矩阵 b (这里必须为列向量)
  ...: r = np.linalg.solve(A,b)  # 调用 solve 函数求解
  ...: print r
  ...:
Out[1]: [[-1.]
      [ 2.]]

Then what was mentioned earlier What is the "n-dimensional" situation? In fact, it is to solve multiple sets of linear equations of two variables with the same form at the same time. For example, if we want to solve these two groups at the same time:

x + 2y = 3
4x + 5y = 6

and

x + 2y = 7
4x + 5y = 8

, we can write like this:

In [2]: import numpy as np
  ...: A = np.mat('1,2; 4,5')          # 构造系数矩阵 A
  ...: b = np.array([[3,6], [7,8]]).T  # 构造转置矩阵 b (这里必须为列向量),
  ...: 注意这里用的是 array
  ...: r = np.linalg.solve(A,b)        # 调用 solve 函数求解
  ...: print r
  ...:
Out[2]: [[-1.         -6.33333333]
      [ 2.          6.66666667]]

SciPy solves the system of nonlinear equations

Generally speaking, we only need to use func and x0. func is a function constructed by ourselves, which is the system of equations that needs to be solved The left end of (the right end is 0), and x0 is the given initial value.

Let’s look at a specific example to solve:

x + 2y + 3z - 6 = 0
5 * (x ** 2) + 6 * (y ** 2) + 7 * (z ** 2) - 18 = 0
9 * (x ** 3) + 10 * (y ** 3) + 11 * (z ** 3) - 30 = 0

You can write it like this:

In [3]: from scipy.optimize import fsolve
  ...:
  ...: def func(i):
  ...:     x, y, z = i[0], i[1], i[2]
  ...:     return [
  ...:             x + 2 * y + 3 * z - 6,
  ...:             5 * (x ** 2) + 6 * (y ** 2) + 7 * (z ** 2) - 18,
  ...:             9 * (x ** 3) + 10 * (y ** 3) + 11 * (z ** 3) - 30
  ...:            ]
  ...:
  ...: r = fsolve(func,[0, 0, 0])
  ...: print r
  ...:
Out[3]: [ 1.00000001  0.99999998  1.00000001]

Of course, SciPy can also be used to solve linear equations. This is because scipy.optimize.fsolve is essentially the least squares method to approximate the real results.

SymPy solves the system of equations

For example, to solve a problem:

x + 2 * (x ** 2) + 3 * (x ** 3) - 6 = 0

It is directly:

In [4]: from sympy import *
  ...: x = symbols('x')
  ...: solve(x + 2 * (x ** 2) + 3 * (x ** 3) - 6, x)
Out[4]: [1, -5/6 - sqrt(47)*I/6, -5/6 + sqrt(47)*I/6]

The above is the detailed content of Three ways to solve equations in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is python a crawler?Next article:Is python a crawler?