Search code examples
pythonscipysystem

fsolve Python function, increment bug


I am trying to solve a four nonlinear equation system using the scipy.optimize function fsolve.

def equations(p):
   e1, e2, F, B = p

   Eq_F1 = (-F + Fa(4, e1, e2) + Fa(5,e1, e2) - A1*Acc1(e1, e2))
   Eq_T1 = (F*L + Fa(4,e1, e2)*A2 + Fa(5, e1, e2)*A3
   Eq_F2 = (Fb(1, e1, B)*A4*math.cos(B) + Fb(2,e1)*A5 + Fb(3,e1)*A6 + F*np.cos(alpha(e1, e2))- A7*Acc2)
   Eq_T2 = (Fb(1,e1, B)*math.cos(B)*A8- F*np.cos(alpha(e1, e2))*A9- Fb(2,e1)*A10- Fb(3,e1)*A11

   return (Eq_F1, Eq_T1, Eq_F2, Eq_T2)

Where Fa, Fb et alpha are functions of e1, e2 and of a number. Ai are constants I introduced to give you a global vision of the system. I solve the system as following:

e1, e2, F, B  = fsolve(equations,(0.3,5,100,0.1), xtol=1.49012e-14)

Where the first guessing is reasonable knowing my problem.

The results given being false, I introduced print(e1, e2, F, B) in the equations function. What a surprise ! If the first values are 0.3, 5, 100, 0.1, they immediately jump to extreme values on the second one, impeding the convergence... Thus the results turn far from relevant.

Has anyone got an idea ?


Solution

  • I can't reproduce your code because I don't have all the constants. Without knowing the exact details of your problem, I cannot be sure, but I will guess there is a high probability of a numerical issue here.

    fsolve is a function that implements an algorithm in numerical analysis, and it iterates towards an approximate solution. Numerical algorithms can be "touchy" in the sense that if the user does not use the right settings for a particular problem, or if the particular choice of algorithm is unsuited for the problem, errors can result.

    • Your starting point might be bad -- numerical algorithms can be very sensitive to the choice of starting point.
    • Your xtol might be way too small -- this can lead to tiny step sizes in the iteration of the algorithm which prevents convergence and also causes numerical error (e.g. rounding error) to accumulate.