solving dynamic number of non-linear equations in python

Go To StackoverFlow.com

1

Fsolve in Scipy seems to be the right candidate for this, I just need help passing equations dynamically. I appreciate any thoughts in advance.

By dynamic I mean number of equations differ from one run to another for example one situation i have :

alpha*x + (1-alpha)*x*y - y = 0
beta*x  + (1- beta)*x*z - z = 0
A*x + B*y + C*z = D

and another situation i have:

alpha*x + (1-alpha)*x*y - y = 0
beta*x  + (1- beta)*x*z - z = 0
gama*x  + (1 -gama)*x*w - w =0
A*x + B*y + C*z + D*w = E

alpha, beta, A, B, C, D and E are all constants. x, y, z, w are variables.

2012-04-05 16:29
by pouria3
I played with Scipy but I wasn't successful with dynamic part. you basically need to define functions for your equations in advance, which is not what i wanted. I then explored BBsolve in R. seems to address what i seek - pouria3 2012-04-10 06:03


1

I haven't used Fsolve myself, but according to its documentation it takes a callable function. Something like this handles multiple functions with unknown number of variables. Bear in mind that the args must be ordered correctly here, but each function simply takes a list.

def f1(argList):
    x = argList[0]
    return x**2
def f2(argList):
    x = argList[0]
    y = argList[1]
    return (x+y)**2
def f3(argList):
    x = argList[0]
    return x/3

fs = [f1,f2,f3]
args = [3,5]
for f in fs:
    print f(args)

For Fsolve, you could try something like this (untested):

def func1(argList, constList):
    x = argList[0]
    y = argList[1]
    alpha = constList[0]
    return alpha*x + (1-alpha)*x*y - y
def func2(argList, constList):
    x = argList[0]
    y = argList[1]
    z = argList[2]
    beta = constList[1]
    return beta*x  + (1- beta)*x*z - z
def func3(argList, constList):
    x = argList[0]
    w = argList[1] ## or, if you want to pass the exact same list to each function, make w argList[4]
    gamma = constList[2]
    return gama*x  + (1 -gama)*x*w - w
def func4(argList, constList):

    return A*x + B*y + C*z + D*w -E ## note that I moved E to the left hand side


functions = []
functions.append((func1, argList1, constList1, args01))
# args here can be tailored to fit your  function structure
# Just make sure to align it with the way you call your function:
# args = [argList, constLit]
# args0 can be null.
functions.append((func1, argList2, constList2, args02))
functions.append((func1, argList3, constList3, args03))
functions.append((func1, argList4, constList4, args04))

for func,argList, constList, args0 in functions: ## argList is the (Vector) variable you're solving for.
    Fsolve(func = func, x0 = ..., args = constList, ...)
2012-04-05 16:57
by Nisan.H
Many thanks for your help and your insight. one of my problems is that I don't know how many equations i have in advance, so i can't really define the functions for them - pouria3 2012-04-05 18:22
What is the source where you get your list of equations from? You can dynamically define them and append to the list, but that would be specifically tailored to the format of the source for the equation - Nisan.H 2012-04-05 19:44
@pouria3 In python almost everything happens at runtime. At runtime you should know how many equition you have, because you have them. You don't have to know things in advanceSimon Bergot 2012-04-06 09:05
Hi and many thanks for your help and insights. I started exploring BBsolve in R. it seems more flexible to me comparing to python module - pouria3 2012-04-10 05:59
Ads