问题描述
从scipy教程中,我并没有真正了解optimize.minimize的工作方式.我想在以下方程组中最小化c3:
From scipy tutorial I don't really get how optimize.minimize works.I want to minimize c3 in following set of equations:
0 = cos(b1)+ cos(b2)- 0.0166
0 = sin(b1)+ sin(b2)+ 0.3077*c3 - 0.6278
0 = cos(b1)- cos(b2)+ 5.4155*c3 - 4.3547
间隔:
c3[0,1]
b1,b2[0,2*pi]
这是我的代码:
def fun(x):
return 4.9992-5.7233*x[0]-2*np.cos(x[2])-np.sin(x[2])-np.sin(x[1])
bnds = ((0,1),(0,2*np.pi),(0,2*np.pi))
i = optimize.minimize(fun, (0.05,np.pi*0.5,np.pi), method='SLSQP', bounds=bnds)
输出为
status: 0
success: True
njev: 6
nfev: 30
fun: -3.9601679766628886
x: array([ 1. , 1.57079633, 0.46367497])
message: 'Optimization terminated successfully.'
jac: array([ -5.72330004e+00, 0.00000000e+00, 6.11841679e-05,
0.00000000e+00])
nit: 6
L-BFGS-B
中的结果相同.我的理解是,此处c3已变为1,这仍然可以,但我希望将其降低.如果我在函数上应用fsolve
,它将找到c3 = 0.46的根.btw为什么我必须在代码中写x [0],x [1]和x [2]而不是c3,b1,b2?是否有使用约束等更巧妙的方法?
The result is the same in L-BFGS-B
My understanding is that here c3 has become 1 which is still ok, but I wanted it to be lower. If I apply fsolve
on the function it finds a root for c3=0.46.btw why do I have to write x[0],x[1] and x[2] instead of c3,b1,b2 in the code?Is there a more clever way using constrains e.g.?
推荐答案
对于三个变量 b1 , b2 和 c3 ,您具有三个超越方程em>.您需要做的是为您的变量求解这3个方程.由于方程式是超验的,因此可能没有解决方案,一个解决方案或许多解决方案.用 Mathematica 解决它们会得出:
You have three transcendent equations for the 3 variables b1, b2 and c3. What you need to do is to solve this 3 equations for your variables. As the equations are transcendent, it's possible that there is no solution, one solution or many solutions. Solving them with Mathematica gives:
In[30]:= eq1 = 0 == Cos[b1] + Cos[b2] - 0.0166;
eq2 = 0 == Sin[b1] + Sin[b2] + 0.3077*c3 - 0.6278;
eq3 = 0 == Cos[b1] - Cos[b2] + 5.4155*c3 - 4.3547;
In[42]:= Reduce[{eq1, eq2, eq3, b1 >= 0, b1 <= 2*Pi, b2 >= 0,
b2 <= 2*Pi, c3 >= 0, c3 <= 1}, {b1, b2, c3}]
During evaluation of In[42]:= Reduce::ratnz: Reduce was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result. >>
Out[42]= b1 == 0.214076 && b2 == 2.85985 && c3 == 0.446303
因此,实际上只有一种解决方案.现在,您还可以使用root
从数字上找到方程组的该解决方案:
So there is in fact only one solution. Now you could also use root
to find this solution of your system of equations numerically:
import scipy as sp
import scipy.optimize
def f(x):
b1, b2, c3 = x
e1 = sp.cos(b1) + sp.cos(b2) - 0.0166
e2 = sp.sin(b1) + sp.sin(b2) + 0.3077*c3 - 0.6278
e3 = sp.cos(b1) - sp.cos(b2) + 5.4155*c3 - 4.3547
return e1, e2, e3
res = sp.optimize.root(f, [0.5, 1.0, 1.0])
print('b1 = {}, b2 = {}, c3 = {}'.format(*res.x))
给予:
b1 = 0.214076256767, b2 = 2.85985240432, c3 = 0.446302998585
您对minimize
所做的事情是最小化三个方程的总和,这不等同于最小化这组方程中的c3".
What you did with minimize
is to minimize the sum of the three equations which is not equivalent to "minimize c3 in (the) set of equations".
minimize
并非为您想要的. 最小化"的作用类似于最小化,找到最小化f(x) = x**2
的x.答案显然是"x = 0".
minimize
is not made for what you want. 'Minimize' does something like minimize find the x that minimizes f(x) = x**2
. The answer would obviously be 'x=0'.
由于接口"minimize"的原因,您必须写"x [0]".该函数只是希望您以矢量形式给出要最小化的参数.
You have to write 'x[0]' because of the interface of 'minimize'. the function simply expects that you give the parameters you want to minimize in vector form.
这篇关于哪个变量通过scipy.optimize.minimize最小化/如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!