簡體   English   中英

為什么 sympy.solve 不輸出任何內容?

[英]Why does sympy.solve not output anything?

我正在嘗試求解方程組。 下面例子中的方程是拉格朗日函數(LDash 1 到 7)的一階導數,我是用紙筆得到的。 系統的解決方案應該給出最大值和最小值的候選者。

選擇變量是X1, X2, LC, LA, s, u ,而所有其他符號都是正實數的占位符。

該腳本運行但隨后在我的控制台中不顯示任何內容。 在整個SymPy 文檔的示例中, solve輸出解決方案。 你能幫我找出為什么它在下面的例子中沒有這樣做嗎?

我對 Python 完全陌生,所以請告訴我如何改進。 謝謝!

# import stuff
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import Function
from sympy.solvers import solve
import sympy as sp

X1, X2, LC, LA, s, u = sp.var('X_1, X_2, L_C, L_A, s, u', positive=True);

# import exogenous variables

a1, a2, a3, a4, S1, S2, S3, S4, p1, p2, p3, p4, fdash, vdash,f, v = sp.symbols('alpha_1, alpha_2, alpha_3, alpha_4, S_1, S_2, S_3, S_4, p_1, p_2, p_3, p_4, \hat{F}, \hat{V}, f, v', positive=True)

v0 = sp.symbols('v_0') # raw skill level
i = sp.symbols('\hat{\imath}') # children in the household - time endowment
I = sp.symbols('I') # household size and total time endowment
climate = sp.symbols('theta') # climate variable
capital = sp.symbols('K') # quasi fixed land and (non-human) capital
lam = sp.symbols('\lambda') # shadow prices (lagrange multiplier)

# pen-and-paper: derivatives of the Lagrange function w.r.t choice variables

LDash1 = a1/(X1-S1)-lam*p1 # L' w.r.t X1
LDash2 = a2/(X2-S2)-lam*p2 # L' w.r.t X2
LDash3 = -a3/(I-i-LA-S3) + 2*lam*p3 + lam*p1*fdash # L' w.r.t LA
LDash4 = -1/(i-LC) - a4/(i-LC-S4) + 2*lam*p4 + lam*p1*fdash*(-s*u + s*v +(1-s)*v0) # L' w.r.t LC
LDash5 = lam*p1*fdash *(-u*LC + v*LC - v0*LC) # L' w.r.t. s
LDash6 = lam*p1*fdash *(-s*LC + s*vdash*LC) # L' w.r.t u
LDash7 = p1*X1 + p2*X2 -2*p3*I + 2*p3*i + 2*p3*LA -2*p4*i + 2*p4*LC + p1*f # L' w.r.t lambda

solve((LDash1,LDash2, LDash3, LDash4, LDash5, LDash6, LDash7), (X1,X2,LA,LC,u,s))

您有兩個問題,首先,解決調用將花費很長時間,並且可能根本無法成功返回。 其次,即使它會返回,說你更換與呼叫solve(LDash1, X1)的返回值solve從不打印。 例如,嘗試用此替換最后一行,以便能夠調試您的方程

solve(LDash1, X1)

在文檔中打印答案的原因是因為它們使用交互模式,這通常由 Python 交互提示指示>>>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM