SciPy 3

SciPy : fsolve

We use the scipy.optimize.fsolve to solve the nonlinear equations. The basic format is fsolve(func, x0). In the format, func(x) is the function of error in equations. The parameter x is vector.

Here is the question.
$$
f_1(u_1, u_2, u_3) = 0 \\
f_2(u_1, u_2, u_3) = 0 \\
f_3(u_1, u_2, u_3) = 0
$$
Here is the definition of func :

1
2
3
def func(x):
u1, u2, u3 = x
return [f1(u1, u2, u3), f2(u1, u2, u3), f3(u1, u2, u3)]

Here is an example :
$$
5b + 3 = 0 \\
4a^2-2 sin (bc) = 0 \\
bc -1.5 = 0
$$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from scipy.optimize import fsolve
import numpy as np

def func(x):
#float将数组中的元素转为标准浮点数,提高运行速度
x0 = float(x[0])
x1 = float(x[1])
x2 = float(x[2])
return [
5*x1+3,
4*x0**2-2*np.sin(x1*x2),
x1*x2-1.5
]

result = fsolve(func, [1,1,1])

print(result)
print(func(result))

Result :

1
2
[-0.70622057 -0.6        -2.5       ]
[0.0, -9.1260332624187868e-14, 5.329070518200751e-15]

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!