1

I have 3 large nonlinear equations with three unknowns, when I scipy.optimize.fsolve I obtain an answer around 10^85 which is too large. A Runtime warning is thrown as well.

import scipy.optimize as opt
def func(variables):
    (A, B, C) = variables
    x1=4
    x2=10
    x3=20
    x4=5
    y1=4
    y2=15
    y3=10
    y4=10

    eq1 = ((((x2)/(A*x2+B*y2+C))-((x1)/(A*x1+B*y1+C))) *  (((x3)/(A*x3+B*y3+C))-((x2)/(A*x2+B*y2+C))) + (((y2)/(A*x2+B*y2+C))-((y1)/(A*x1+B*y1+C))) * (((y3)/(A*x3+B*y3+C))-((y2)/(A*x2+B*y2+C))) +(((1 )/(A*x2+B*y2+C))-((1 )/(A*x1+B*y1+C))) * (((1 )/(A*x3+B*y3+C))-((1 )/(A*x2+B*y2+C))))
    eq2 = ((((x3)/(A*x3+B*y3+C))-((x2)/(A*x2+B*y2+C))) * (((x4)/(A*x4+B*y4+C))-((x3)/(A*x3+B*y3+C))) + (((y3)/(A*x3+B*y3+C))-((y2)/(A*x2+B*y2+C))) * (((y4)/(A*x4+B*y4+C))-((y3)/(A*x3+B*y3+C))) +(((1 )/(A*x3+B*y3+C))-((1 )/(A*x2+B*y2+C))) * (((1 )/(A*x4+B*y4+C))-((1 )/(A*x3+B*y3+C))))
    eq3 = ((((x4)/(A*x4+B*y4+C))-((x3)/(A*x3+B*y3+C))) * (((x1)/(A*x1+B*y1+C))-((x4)/(A*x4+B*y4+C))) + (((y4)/(A*x4+B*y4+C))-((y3)/(A*x3+B*y3+C))) * (((y1)/(A*x1+B*y1+C))-((y4)/(A*x4+B*y4+C))) +(((1 )/(A*x4+B*y4+C))-((1 )/(A*x3+B*y3+C))) * (((1 )/(A*x1+B*y1+C))-((1 )/(A*x4+B*y4+C))))

    return [eq1, eq2, eq3]


solution = opt.fsolve(func, np.array([1,1,1]))
print(solution)

and answer is:

RuntimeWarning:The number of calls to function has reached maxfev = 800.
      warnings.warn(msg, RuntimeWarning)
    [6.72215161e+84 5.84595081e+84 6.34963908e+85]

What is my mistake?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • Seems like the python version is diverging. Can you try using a lower value for `factor` (e.g. 10 or 1)? – BlackBear Apr 01 '20 at 13:32
  • 1
    Also, which algorithm does matlab use? Perhaps it is implemented in scipy too (https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.root.html#scipy.optimize.root) – BlackBear Apr 01 '20 at 13:37
  • @BlackBear . I tried 0.001,0.01,0.1 answer is still the same. – mahdi bagheri Apr 01 '20 at 13:40
  • @BlackBear I checked the link and used different algorithms, with setting tol in a way to prevent diverging finaly found three algorithms have the same resault .Thankyou for your good answer . – mahdi bagheri Apr 01 '20 at 14:10
  • Probably not what you want, but `z3` might be what you want here? – Gareth Ma Apr 01 '20 at 14:26
  • You appear to have doubled up on brackets on many occasions, eg. ```((1 )/(A*x1+B*y1+C))``` could be written as ```1/(A*x1+B*y1+C)``` without worry. So I would double check that the equations and brackets in particular are what you expect them to be. – Paddy Harrison Apr 01 '20 at 14:34
  • @Gareth Ma No, [A,B,C] are unknowns. actually they are parameters of a plane in 3D cartisian coordination. where have you find z3 ? :) – mahdi bagheri Apr 01 '20 at 14:37
  • 1
    `z3` is a SAT-solver lol. Google it you'll find – Gareth Ma Apr 01 '20 at 14:37
  • @Paddy Harrison actually I did this to make equations aligned . you see lines are well disciplined, if somthing missed it will be apear by shifting everything. although you'r right it could be writen as you said. – mahdi bagheri Apr 01 '20 at 14:45

1 Answers1

0

Also included in scipy optimize is fmin_tnc, which allows bounds to be placed on the roots:

opt.fmin_tnc(func,x0=np.array([1,1,1]),
             approx_grad=True, 
             bounds=[(None,1E5),(None,1E5),(None,1E5)])
EMiller
  • 817
  • 1
  • 7
  • 20