2012-04-05 20 views
1

FsolveScipyこれは正しい候補のようですが、私はちょうど方程式を動的に渡すのを助ける必要があります。私は事前に考えていただければ幸いです。ダイナミックによってPythonで動的な数の非線形方程式を解く

私は方程式の数が、私が持っている別の例のための1つの状況に一度の実行異なる意味:、

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 

alphabetaA

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

と私が持っている別の状況B,C,DおよびEはすべて定数である。 x,y,z,wは変数である。

+0

。あなたは基本的に私が望むものではないあなたの方程式の関数を事前に定義する必要があります。私はR.のBBsolveを調べて、私が探しているものに対処しているようだ。 – pouria3

答えて

1

私はFsolveを自分で使用していませんが、ドキュメントによれば呼び出し可能な関数が使用されています。 このようなものは、未知数の変数を持つ複数の関数を処理します。 argsはここで正しく並べられなければなりませんが、各関数は単にリストを取ることに注意してください。

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) 

fsolveはでは、この(未テスト)のような何かを試みることができる:私はscipyのダウンロードと遊んだけど、私は動的部分と成功しませんでした

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, ...) 
+0

あなたの助けとあなたの洞察に感謝します。私の問題の1つは、私が事前に持っている方程式の数がわからないので、実際にそれらの関数を定義することはできません。 – pouria3

+0

方程式のリストを取得するソースは何ですか?それらを動的に定義してリストに追加することができますが、これは方程式のソースの形式に合わせて調整されます –

+0

@ pouria3 pythonではほとんどすべてが実行時に発生します。実行時にはあなたが持っているequitionの数が分かるはずです。 *事前に物事を知る必要はありません。 – Simon

関連する問題