Skip to content Skip to sidebar Skip to footer

Which Variable Is Minimized By Scipy.optimize.minimize/how Does It Work?

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*c

Solution 1:

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

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))

gives:

b1 = 0.214076256767, b2 = 2.85985240432, c3 = 0.446302998585

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 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'.

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.

Post a Comment for "Which Variable Is Minimized By Scipy.optimize.minimize/how Does It Work?"