Search code examples
pythonpython-3.xscipyleast-squares

Error to allocate result variables from opt.leastsq


I'm setting up an interior positioning system, and when I request the opt.leastsq function, I have the expected result in the string, but it gives me an output error.

Here's my code:

x, y, _ = opt.leastsq(F, x0=[xp, yp], Dfun=J)

The above code always gives me the error:

x, y, _ = opt.leastsq(F, x0=[xp, yp], Dfun=J)
ValueError: not enough values to unpack (expected 3, got 2)

But when I print this function:

x = opt.leastsq(F, x0=[xp, yp], Dfun=J)
print(x)

It gives me exactly the result I expect, with the exception of the number "2" outside the coordinate:

(array([4.6243258 , 4.23836195]), 2)

I appreciate who can help me organize my code to have the first value assigned to the variable x, and the second value assigned to the variable y. Avoiding that the said error appears to me as mentioned above.

Thank you very much in advance.


Solution

  • Thanks to hpaulj guidelines, being able to adjust the values of the result in two variables "x" and "y". I leave here the process that I did and it works correctly. I hope to be of help to others who have the same question. First I extract the values and place them in another variable which I called "posit"

    posit, _ = opt.leastsq(F, x0=[xp, yp], Dfun=J)
    

    Then using the "splint" function I set the coordinate values for each variable ("x" and "y")

    x, y = np.split(posit, 2, axis=0)
    print("x = ",x)
    print("y = ",y)
    

    So I get the expected result, now being able to handle the values separately exactly in the "x" and "y" coordinates.

    x =  [3.40314229]
    y =  [0.79129237]