[英]Assigning the last iterated value to a variable in python
I have the last set of values of an iteration: y1[i],y2[i],y3[i]
(which was obtained by doing the integration of coupled ODE) Now I need to assign these values to another variables : Y1,Y2,Y3 and then use these variables in a function f(y,t) (ODEs that has to be integrated again). 我有一个迭代的最后一组值:
y1[i],y2[i],y3[i]
(通过对耦合ODE进行积分获得)现在我需要将这些值分配给另一个变量:Y1, Y2,Y3,然后在函数f(y,t)中使用这些变量(必须再次积分ODE)。 This is part of my code: 这是我的代码的一部分:
#constants used
H = 2.27e-18
l = 1.5
G = 6.637*(10**(-11))
k = (8*3.14*G)**0.5
om_de = 0.75
omega_matter = 1 - om_de
w0 = -0.85
rho_c = 3*(H**2)/(k**2)
c = ((om_de*(1 + w0)*rho_c)/(H**2))**0.5
v0 = (om_de*(1 - w0)*rho_c)/(2*np.exp(-l*k))
#for iteration
p = 0.0
q = 1.0
n = 10000.0
def H(Y1,Y2,Y3):
return -3*Y3*(H**2)*(omega_matter/(Y1**3) + (H**2)*(Y3**2)/(2.0*rho_c) + (v0*np.exp(-l*Y2*k))/(rho_c))**0.5 + (l*k*v0*np.exp(-l*Y2*k))/(H**2)
dH = (q-p)/n
Y1 = [y1[j]]
Y2 = [y2[j]]
Y3 = [y3[j]]
But I am not able to do any further operations on them.
TypeError: unsupported operand type(s) for ** or pow(): 'function' and 'int'
TypeError:**或pow()不支持的操作数类型:“函数”和“整数”
keeps showing up. 不断出现。 The link to the complete code is here: https://pastebin.com/mdQE29N9 (might help in understanding what I am trying to achieve and the code is too lengthy to be posted here)
完整代码的链接位于: https : //pastebin.com/mdQE29N9 (可能有助于理解我要实现的目标,并且代码太长,无法在此处发布)
I think the problem is with the way I have assigned the values,but am not so sure. 我认为问题出在我分配值的方式上,但不确定。 Any help here as to what is causing this and how I am supposed to solve this would be great.
对于造成此问题的原因以及我应该如何解决的任何帮助,将非常有用。
You have an error in line 57: 您在第57行中出现错误:
def F(Y1,Y2,Y3):
return Y1*(omega_matter/(Y1**3) + (H**2)*(Y3**2)/(2.0*rho_c) + (v0*np.exp(-l*Y2*k))/(rho_c))**(1.0/2.0)
in expression H**2
, where H
is defined as a function in line 62: 在表达式
H**2
,其中H
在第62行中定义为函数:
def H(Y1,Y2,Y3):
...
I'm guessing you really meant to say: H(Y1,Y2,Y3)**2
. 我猜您真的要说:
H(Y1,Y2,Y3)**2
。
But that's not all. 但这还不是全部。 Line 52:
第52行:
y1 == y1.append(y1[j] + (dh/6.0)*(k1 + 2.0*k2 + 2.0*k3 + k4))
No need to test for equivalence between y1
and None
(that's what append()
returns). 无需测试
y1
和None
之间的等效性(这是append()
返回的结果)。 I'm not sure what you tried to achieve here, maybe this? 我不确定您想在这里实现什么,也许是吗?
y1.append(y1[-1] + (dh/6.0)*(k1 + 2.0*k2 + 2.0*k3 + k4))
Also, to access the last element of y1
(line 66), you can use a negative index: 另外,要访问
y1
的最后一个元素(第66行),可以使用负索引:
Y1 = [y1[-1]] # instead of: Y1 = [y1[j]]
You have a variable named H and a function named H() 您有一个名为H的变量和一个名为H()的函数
H = 2.27e-18
and 和
def H(Y1,Y2,Y3):
return -3*Y3*(H**2)*(omega_matter/(Y1**3) + (H**2)*(Y3**2)/(2.0*rho_c) + (v0*np.exp(-l*Y2*k))/(rho_c))**0.5 + (l*k*v0*np.exp(-l*Y2*k))/(H**2)
Change the name of the variable or the function for something else. 将变量或函数的名称更改为其他名称。
H_1 = 2.27e-18 #for example
As @randomir says, you probably want to look at his answer too 正如@randomir所说,您可能也想看看他的回答
You can reproduce the error by simple doing: 您可以通过执行以下操作来重现该错误:
H = 5
def H(a,b):
pass
print(H**3)
TypeError: unsupported operand type(s) for ** or pow(): 'function' and 'int'
TypeError:**或pow()不支持的操作数类型:“函数”和“整数”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.