Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand your question correctly, I <em>think</em> this is what you're after:</p> <pre><code>from scipy.optimize import minimize import numpy as np def f(coord,x,y,r): return np.sum( ((coord[0] - x)**2) + ((coord[1] - y)**2) - (r**2) ) x = np.array([0, 2, 0]) y = np.array([0, 0, 2]) r = np.array([.88, 1, .75]) # initial (bad) guess at (x,y) values initial_guess = np.array([100,100]) res = minimize(f,initial_guess,args = [x,y,r]) </code></pre> <p>Which yields:</p> <pre><code>&gt;&gt;&gt; print res.x [ 0.66666666 0.66666666] </code></pre> <p>You might also try the least squares method which expects an objective function that returns a vector. It wants to minimize the sum of the squares of this vector. Using least squares, your objective function would look like this:</p> <pre><code>def f2(coord,args): x,y,r = args # notice that we're returning a vector of dimension 3 return ((coord[0]-x)**2) + ((coord[1] - y)**2) - (r**2) </code></pre> <p>And you'd minimize it like so:</p> <pre><code>from scipy.optimize import leastsq res = leastsq(f2,initial_guess,args = [x,y,r]) </code></pre> <p>Which yields:</p> <pre><code>&gt;&gt;&gt; print res[0] &gt;&gt;&gt; [ 0.77961518 0.85811473] </code></pre> <p>This is basically the same as using <code>minimize</code> and re-writing the original objective function as:</p> <pre><code>def f(coord,x,y,r): vec = ((coord[0]-x)**2) + ((coord[1] - y)**2) - (r**2) # return the sum of the squares of the vector return np.sum(vec**2) </code></pre> <p>This yields:</p> <pre><code>&gt;&gt;&gt; print res.x &gt;&gt;&gt; [ 0.77958326 0.8580965 ] </code></pre> <p>Note that <code>args</code> are handled a bit differently with <code>leastsq</code>, and that the data structures returned by the two functions are also different. See the documentation for <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#scipy.optimize.minimize" rel="noreferrer"><code>scipy.optimize.minimize</code></a> and <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.leastsq.html#scipy.optimize.leastsq" rel="noreferrer"><code>scipy.optimize.leastsq</code></a> for more details.</p> <p>See the <a href="http://docs.scipy.org/doc/scipy/reference/optimize.html" rel="noreferrer"><code>scipy.optimize</code></a> documentation for more optimization options.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload