在Python中用用户输入调用/选择变量(浮点值) [英] Calling/selecting variables (float valued) with user input in Python

查看:254
本文介绍了在Python中用用户输入调用/选择变量(浮点值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一个计算物理学项目(绘制化学反应物相对于彼此的速率以显示振荡行为),取得了相当大的成功。然而,我的一个模拟涉及两个以上的主动振荡代理(实际上是五个),这显然不适合任何单一的可视化绘图...



我的方案是从而让用户分别选择他们想要在x轴和y轴上绘制哪两种反应物。我试图(愚蠢)将字符串输入值转换成各自的变量名称,但我想我需要一个完全不同的方法,如果有任何存在?

如果有助于澄清任何,这里是我的代码的一部分:
$ b $ pre $ def $耦合Bseselator(A,B,t_trial,display_x,display_y):
t = 0
t_step = .01
X = 0
Y = 0
E = 0
U = 0
V = 0
dX = (A) - (B + 1)*(X)+(X ** 2)*(Y)
dY =(B)*(X) - (X ** 2)*(Y)$ b (U) - (B)*(X)=(E)*(U) - (X)
dU =(U ** 2)*
dV =(E)*(U) - (U ** 2)*(V)
array_t = [0]
array_X = [0]
array_Y = [0 ]
array_U = [0]
array_V = [0]

while t <= t_trial:
X_1 = X +(dX)*(t_step / 2 )
Y_1 = Y +(dY)*(t_step / 2)
E_1 = E +(dE)*(t_step / 2)
U_1 = U +(dU)*(t_step / 2)
V_1 = V +(dV)*(t_step / 2)
(A) - (B + 1)*(X_1)+(X_1 ** 2)*(Y_1)
dY_1 =(B)*(X_1) - (X_1 ** 2)*(Y_1) (U_1) - (B_1)*(U_1) - (B_1)*(U_1) - (E_1)*(U_1) - (X_1)
dU_1 =(U_1 ** 2)*(V_1) (U_1)*(V_1)
X_2 = X +(dX_1)*(t_step / 2)
Y_2 = Y +(dY_1)*(t_step / 2)
E_2 = E +(dE_1)*(t_step / 2)
U_2 = U +(dU_1)*(t_step / 2)
V_2 = V +(dV_1)*(t_step / 2)
dX_2 =(A) - (B + 1)*(X_2)+(X_2 ** 2)*(Y_2)
dY_2 = *(X_2) - (X_2 ** 2)*(Y_2)
dE_2 = - (E_2)*(U_2) - (X_2)
dU_2 =(U_2 ** 2)*(V_2) - (E_2 + 1)*(U_2) - (B)*(X_2)
dV_2 =(E_2)*(U_2) - (U_2 ** 2)*(V_2)
X_3 = X + (t_step)
Y_3 = Y +(dY_2)*(t_step)
E_3 = E +(dE_2)*(t_step)
U_3 = U +(dU_2)*(t_step )
V_3 = V +(dV_2)*(t_step)
dX_3 =(A) - (B + 1)*(X_3)+(X_3 ** 2)*(Y_3)
dY_3 =(B)*(X_3) - (X_3 ** 2)*(Y_3)
dE_3 = - (E_3)*(U_3) - (X_3)
dU_3 =(U_3 ** 2)*(V_3) - (E_3 + 1)*(U_3) - (B)*(X_3)
dV_3 =(E_3)*(U_3) - (U_3 ** 2)*(V_3)
X = X +((dX + 2 * dX_1 + 2 * dX_2 + dX_3)/ 6)* t_step
Y = Y +((dX + 2 * dY_1 + 2 * dY_2 + dY_3)/ 6)* t_step
E = E +((dE + dE_1 + 2 * dE_2 + dE_3)/ 6)* t_step
U = U +((dU + 2 * dU_1 + 2 * dY_2 + dE_3)/ 6)* t_step
V = V + (A) - (B + 1)*(X)+(X ** 2)*(Y)$ b $ d $ + $ (b)*(X) - (X ** 2)*(Y)
t_step = .01 /(1 + dX ** 2 + dY ** 2)** .5
t = t + t_step
array_X.append(X)
array_Y.append(Y)
array_E.append(E)
array_U.append(U)
array_V .append(V)
array_t.append(t)

其中

  display_x = raw_input(在您想要分析的催化剂中选择催化剂(X,Y,E,U或V)
display_y = raw_input(从列表中选择另一个催化剂,你想包含在相位/场图中)

coupledBrusselator(A,B,t_trial,display_x,display_y)

谢谢!

解决方案

一旦你计算了不同的数组,你可以将它们添加到映射的 dict 名称到数组。这可以用来查找正确的数组: display_x display_y

  named_arrays = {
X:array_X,
Y:array_Y,
E:array_E,
...
}

return(named_arrays [display_x],named_arrays [display_y])


I've been working on a computational physics project (plotting related rates of chemical reactants with respect to eachother to show oscillatory behavior) with a fair amount of success. However, one of my simulations involves more than two active oscillating agents (five, in fact) which would obviously be unsuitable for any single visual plot...

My scheme was hence to have the user select which two reactants they wanted plotted on the x-axis and y-axis respectively. I tried (foolishly) to convert string input values into the respective variable names, but I guess I need a radically different approach if any exist?

If it helps clarify any, here is part of my code:

def coupledBrusselator(A, B, t_trial,display_x,display_y):
    t = 0
    t_step = .01
    X = 0       
    Y = 0
    E = 0
    U = 0
    V = 0
    dX = (A) - (B+1)*(X) + (X**2)*(Y)  
    dY = (B)*(X) - (X**2)*(Y)
    dE = -(E)*(U) - (X)  
    dU = (U**2)*(V) -(E+1)*(U) - (B)*(X)
    dV = (E)*(U) - (U**2)*(V)
    array_t = [0]
    array_X = [0]
    array_Y = [0]
    array_U = [0]
    array_V = [0]       

    while t <= t_trial:             
        X_1 = X + (dX)*(t_step/2)   
        Y_1 = Y + (dY)*(t_step/2)
        E_1 = E + (dE)*(t_step/2)
        U_1 = U + (dU)*(t_step/2)
        V_1 = V + (dV)*(t_step/2) 
        dX_1 = (A) - (B+1)*(X_1) + (X_1**2)*(Y_1)  
        dY_1 = (B)*(X_1) - (X_1**2)*(Y_1)
        dE_1 = -(E_1)*(U_1) - (X_1)  
        dU_1 = (U_1**2)*(V_1) -(E_1+1)*(U_1) - (B)*(X_1)
        dV_1 = (E_1)*(U_1) - (U_1**2)*(V_1)
        X_2 = X + (dX_1)*(t_step/2)
        Y_2 = Y + (dY_1)*(t_step/2)
        E_2 = E + (dE_1)*(t_step/2)
        U_2 = U + (dU_1)*(t_step/2)
        V_2 = V + (dV_1)*(t_step/2) 
        dX_2 = (A) - (B+1)*(X_2) + (X_2**2)*(Y_2)
        dY_2 = (B)*(X_2) - (X_2**2)*(Y_2)
        dE_2 = -(E_2)*(U_2) - (X_2)  
        dU_2 = (U_2**2)*(V_2) -(E_2+1)*(U_2) - (B)*(X_2)
        dV_2 = (E_2)*(U_2) - (U_2**2)*(V_2)   
        X_3 = X + (dX_2)*(t_step)
        Y_3 = Y + (dY_2)*(t_step)
        E_3 = E + (dE_2)*(t_step)
        U_3 = U + (dU_2)*(t_step)
        V_3 = V + (dV_2)*(t_step) 
        dX_3 = (A) - (B+1)*(X_3) + (X_3**2)*(Y_3)
        dY_3 = (B)*(X_3) - (X_3**2)*(Y_3)
        dE_3 = -(E_3)*(U_3) - (X_3)  
        dU_3 = (U_3**2)*(V_3) -(E_3+1)*(U_3) - (B)*(X_3)
        dV_3 = (E_3)*(U_3) - (U_3**2)*(V_3) 
        X = X + ((dX + 2*dX_1 + 2*dX_2 + dX_3)/6) * t_step 
        Y = Y + ((dX + 2*dY_1 + 2*dY_2 + dY_3)/6) * t_step
        E = E + ((dE + 2*dE_1 + 2*dE_2 + dE_3)/6) * t_step          
        U = U + ((dU + 2*dU_1 + 2*dY_2 + dE_3)/6) * t_step
        V = V + ((dV + 2*dV_1 + 2*dV_2 + dE_3)/6) * t_step
        dX = (A) - (B+1)*(X) + (X**2)*(Y)  
        dY = (B)*(X) - (X**2)*(Y)
        t_step = .01 / (1 + dX**2 + dY**2) ** .5
        t = t + t_step
        array_X.append(X) 
        array_Y.append(Y)
        array_E.append(E)
        array_U.append(U)
        array_V.append(V)
        array_t.append(t)   

where previously

display_x = raw_input("Choose catalyst you wish to analyze in the phase/field diagrams (X, Y, E, U, or V) ") 
display_y = raw_input("Choose one other catalyst from list you wish to include in phase/field diagrams ")

coupledBrusselator(A, B, t_trial, display_x, display_y) 

Thanks!

解决方案

Once you have calculated the different arrays, you could add them to a dict that maps names to arrays. This can then be used to look up the correct arrays for display_x and display_y:

named_arrays = {
  "X": array_X,
  "Y": array_Y,
  "E": array_E,
  ...
}

return (named_arrays[display_x], named_arrays[display_y])

这篇关于在Python中用用户输入调用/选择变量(浮点值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆