使用 `fsolve` 求解具有 n 个未知数的 m 个方程,其中 n<m
using `fsolve` to solve m equations with n unknowns where n<m
假设我有两个方程,其中一个未知,我想用 fsolve
来求解:
0 = 0.5*x[0]**2-2
0 = 2-x
显然答案是 x=2
。这个我试过了
import numpy as np; from scipy.optimize import fsolve
def f(x):
r = np.zeros(2)
r[0] = 0.5*x[0]**2-2
r[1] = 2-x[0]
return r
fsolve(f,[0.5])
错误信息是"The array returned by a function changed size between calls"
我看不出这里出了什么问题。我该如何解决这个问题?
一般来说,如何求解变量数少于方程数的方程式。
这是完整的消息
Traceback (most recent call last):
File "<ipython-input-37-e4f77791f3f6>", line 12, in <module>
fsolve(f,[0.5])
File "... anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 148, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File ".... /anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 227, in _root_hybr
ml, mu, epsfcn, factor, diag)
ValueError: The array returned by a function changed size between calls
在超定系统的情况下(方程的数量大于变量的数量)你需要使用,例如,最小二乘法。在这种情况下,通常没有传统意义上的解决方案。我们需要定义,我们应该将什么视为系统的解决方案。
让你有一个标量变量的两个方程组:
f(x) = 0
g(x) = 0
这个系统通常不一致,没有传统意义上的解决方案。
让我们将一些值 eps1 和 eps2 添加到系统的右侧:
f(x) = 0 + eps1
g(x) = 0 + eps2
eps1 和 eps2 是一些值;
现在,让我们找到 x
当 eps1^2 + eps2^2
上升时的最小值;这将是系统的解决方案
在最小二乘意义上。
要使用 scipy 获得此类解决方案,您可以使用 least_square 函数。
让我们看看下面的一段代码,它解决了你的方程组:
import numpy as np
from scipy.optimize import fsolve, least_squares
def f(x):
r = np.zeros(2)
r[0] = 0.5*x**2-2
r[1] = 2-x
return r
least_squares(f, [0.0])
结果:
active_mask: array([0.])
cost: 5.175333019854869e-20
fun: array([ 2.87759150e-10, -1.43879575e-10])
grad: array([7.19397879e-10])
jac: array([[ 2.00000001],
[-1. ]])
message: '`gtol` termination condition is satisfied.'
nfev: 6
njev: 6
optimality: 7.193978788924559e-10
status: 1
success: True
x: array([2.])
假设我有两个方程,其中一个未知,我想用 fsolve
来求解:
0 = 0.5*x[0]**2-2
0 = 2-x
显然答案是 x=2
。这个我试过了
import numpy as np; from scipy.optimize import fsolve
def f(x):
r = np.zeros(2)
r[0] = 0.5*x[0]**2-2
r[1] = 2-x[0]
return r
fsolve(f,[0.5])
错误信息是"The array returned by a function changed size between calls"
我看不出这里出了什么问题。我该如何解决这个问题?
一般来说,如何求解变量数少于方程数的方程式。
这是完整的消息
Traceback (most recent call last):
File "<ipython-input-37-e4f77791f3f6>", line 12, in <module>
fsolve(f,[0.5])
File "... anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 148, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File ".... /anaconda3/lib/python3.7/site-packages/scipy/optimize/minpack.py", line 227, in _root_hybr
ml, mu, epsfcn, factor, diag)
ValueError: The array returned by a function changed size between calls
在超定系统的情况下(方程的数量大于变量的数量)你需要使用,例如,最小二乘法。在这种情况下,通常没有传统意义上的解决方案。我们需要定义,我们应该将什么视为系统的解决方案。
让你有一个标量变量的两个方程组:
f(x) = 0 g(x) = 0
这个系统通常不一致,没有传统意义上的解决方案。
让我们将一些值 eps1 和 eps2 添加到系统的右侧:
f(x) = 0 + eps1 g(x) = 0 + eps2
eps1 和 eps2 是一些值;
现在,让我们找到 x
当 eps1^2 + eps2^2
上升时的最小值;这将是系统的解决方案
在最小二乘意义上。
要使用 scipy 获得此类解决方案,您可以使用 least_square 函数。
让我们看看下面的一段代码,它解决了你的方程组:
import numpy as np
from scipy.optimize import fsolve, least_squares
def f(x):
r = np.zeros(2)
r[0] = 0.5*x**2-2
r[1] = 2-x
return r
least_squares(f, [0.0])
结果:
active_mask: array([0.])
cost: 5.175333019854869e-20
fun: array([ 2.87759150e-10, -1.43879575e-10])
grad: array([7.19397879e-10])
jac: array([[ 2.00000001],
[-1. ]])
message: '`gtol` termination condition is satisfied.'
nfev: 6
njev: 6
optimality: 7.193978788924559e-10
status: 1
success: True
x: array([2.])