1

Hello I have a problem using Scipy's fsolve function when I have an equation system.

My code only let's me have as much variables as there are equations in my system, but we all know that in practice, you can have more equations than variables, especially when the solution is not a concrete number but a range of numbers, an additional equation can help narrow down the "haystack" so to speak.

How to insert more equations than variables is my question?

Suppose I have the following non-linear system:

A/B=0.4583(3)
A/C=0.25
A/D=0.72(2)
B/C=0.54(54)

So I have the following code for this:

from scipy.optimize import *
from numpy  import *

def FUNC(arg):

    A,B,C,D=arg
    UNK=empty((4))  

    UNK[0]= A/B-0.458333333333333
    UNK[1]= A/C-0.25
    UNK[2]= A/D-0.722222222222222
    UNK[3]= B/C-0.545454545454546

    return UNK


SOLVED= fsolve(FUNC, [1.0]*4)

print (SOLVED)

The problem is that I also know the following information:

B/D=1.57(57)
C/D=2.8(8)

How can I insert these 2 additional equations into my equation system?

Also how can I display the range of solutions instead of just 1 solution, it seems like fsolve only displays the 1st solution it encounters, not the entire range of possible solutions.

johnyboy325
  • 115
  • 9

1 Answers1

1

Note that scipy's fsolve is just a Wrapper for the MINPACK's hybrd routine:

The purpose of HYBRD is to find a zero of a system of N non-linear functions in N variables by a modification of the Powell hybrid method. The user must provide a subroutine which calculates the functions. The Jacobian is then calculated by a forward-difference approximation.

If you want to solve a system of 6 equations, your function FUNC needs to be a function of 6 variables. We could do it like this:

import numpy as np
from scipy.optimize import fsolve

def FUNC(arg):
    A, B, C, D, E, F = arg
    UNK=np.empty((6))  

    UNK[0]= A/B-0.458333333333333
    UNK[1]= A/C-0.25
    UNK[2]= A/D-0.722222222222222
    UNK[3]= B/C-0.545454545454546
    UNK[4]= B/D-1.575757575757575
    UNK[5]= C/D-2.888888888888888
    return UNK

fsolve(FUNC, x0=[1.0]*6)

It isn't possible to get all solutions with fsolve. But you could try different initial points x0 with the hope to get different solutions.

joni
  • 6,840
  • 2
  • 13
  • 20