I have post the similar question before,however,I think I may have misinterpreted my question,so may I just post my origin code here,and looking for someone can help me,I am really stuck now..thanks alot.
我之前也有过类似的问题,但是我想我可能误解了我的问题,所以我可以在这里发布我的原始代码,寻找可以帮助我的人,我现在真的被困住了。谢谢你很多。
from numpy import *
import math as M
#initial condition All in SI unit
G=6.673*10**-11 #Gravitational constant
ms=1.9889*10**30 #mass of the sun
me=5.9742*10**24 #mass of the earth
dt=10 #time step
#Creat arrays
vs=array([[0,0,0]]) #1st element stand for x component of V of earth
ve=array([[29770,0,0]])
rs=array([[0,0,0]])
re=array([[0,1.4960*10**11,0]])
#First update velocity in order to start leapfrog approximation
fs=-G*ms*me*((rs-re)/(M.sqrt((rs-re)[0][0]**2+(rs-re)[0][1]**2+(rs-re)[0][2]**2))**3)
fe=-fs
vs=vs+fs*dt/ms
ve=ve+fe*dt/me
n=input('please enter the number of timestep you want it evolve:')
#update force
def force(n,ms,me,rs,re,G):
rs,re=update_r(rs,re,n,dt)
fs=-G*ms*me*((rs-re)/(M.sqrt((rs-re)[0][0]**2+(rs-re)[0][1]**2+(rs-re)[0][2]**2))**3)
fe=-fs
return fs,fe
#update velocities
def update_v(n,vs,ve,ms,me,dt,fs,fe):
fs,fe=force(n,ms,me,rs,re,G)
i=arange(n)
vs=vs+fs[:]*i[:,newaxis]*dt/ms
ve=ve+fe[:]*i[:,newaxis]*dt/me
return vs,ve
#update position
def update_r(rs,re,n,dt):
vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
i=arange(n)
rs=rs+vs[:]*i[:,newaxis]*dt
re=re+ve[:]*i[:,newaxis]*dt
return rs,re
#there is start position,v,r,f all have initial arrays(when n=0).
#then it should calculate f(n=1) then use this to update v(n=0)
#to v(n=1),then use v(n=1) update r(n=0) to r(n=1),then use r(n=1)
#update f(n=1) to f(n=2)....and so on until finish n.but this code seems doesnt do this,,how can I make it? –
when i call force python gives:
当我调用force python时:
please enter the number of timestep you want it evolve:4Traceback (most recent call last):
File "<pyshell#391>", line 1, in <module>
force(n,ms,me,rs,re,G)
File "/Users/Code.py", line 24, in force
rs,re=update_r(rs,re,n,dt)
File "/Users/Code.py", line 39, in update_r
vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
UnboundLocalError: local variable 'vs' referenced before assignment
can anyone give me some tips?thanks......
有人能给我一些建议吗?
4 个解决方案
#1
5
In the first line of update_r
, you have vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
. Look at the function that you are calling. You are calling update_v
with a bunch of parameters. One of these parameters is vs
. However, that is the first time in that function that vs
appears. The variable vs
does not have a value associated with it yet. Try initializing it first, and your error should disappear
在update_r的第一行,有vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)。看看你所调用的函数。您使用一组参数调用update_v。其中一个参数是vs,但这是第一次出现vs出现的函数。变量vs没有与之关联的值。首先尝试初始化它,然后您的错误就会消失。
#2
9
where do you call force in this code?
在这段代码中你调用了什么力量?
In any event, the problem is in update_r. You reference vs in the first line of update_r even though vs is not defined in this function. Python is not looking at the vs defined above. Try adding
在任何情况下,问题都在update_r中。虽然在这个函数中没有定义vs,但在update_r的第一行中,您引用了vs。Python并没有考虑上面定义的vs。尝试添加
global vs
as the first line of update_r or adding vs to the parameter list for update_r
作为update_r的第一行,或向update_r的参数列表添加vs。
#3
1
Put an additional global
statement containing all your globals after each def
statement. Otherwise, all globals are transformed into locals within your def without it.
在每个def语句之后,添加一个包含全局变量的全局语句。否则,在没有它的情况下,所有的全局变量都转换为本地的局部变量。
def update_v(n,vs,ve,ms,me,dt,fs,fe):
global vs, ve, ...
#4
0
On line 39 you do
在第39行。
vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
while you are inside a function. Since you defined a global variable called vs, you would expect this to work. It would have worked if you had:
当你在一个函数里面。既然您定义了一个名为vs的全局变量,那么您将期望它能够工作。如果你有:
vs_new,ve_new = update_v(n,vs,ve,ms,me,dt,fs,fe)
because then the interpreter knows vs in the function arguments is the global one. But since you had vs in the left hand side, you created an uninitialized local variable.
因为在函数参数中解释器知道vs是全局变量。但是由于左侧有vs,所以创建了一个未初始化的局部变量。
But dude, you have a much bigger problem in your code: update_r calls update_v, update_v calls force, and force calls update_r - you will get a stack overflow :)
但是,您的代码中有一个更大的问题:update_r调用update_v, update_v调用force,并且强制调用update_r——您将得到一个堆栈溢出:)
#1
5
In the first line of update_r
, you have vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
. Look at the function that you are calling. You are calling update_v
with a bunch of parameters. One of these parameters is vs
. However, that is the first time in that function that vs
appears. The variable vs
does not have a value associated with it yet. Try initializing it first, and your error should disappear
在update_r的第一行,有vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)。看看你所调用的函数。您使用一组参数调用update_v。其中一个参数是vs,但这是第一次出现vs出现的函数。变量vs没有与之关联的值。首先尝试初始化它,然后您的错误就会消失。
#2
9
where do you call force in this code?
在这段代码中你调用了什么力量?
In any event, the problem is in update_r. You reference vs in the first line of update_r even though vs is not defined in this function. Python is not looking at the vs defined above. Try adding
在任何情况下,问题都在update_r中。虽然在这个函数中没有定义vs,但在update_r的第一行中,您引用了vs。Python并没有考虑上面定义的vs。尝试添加
global vs
as the first line of update_r or adding vs to the parameter list for update_r
作为update_r的第一行,或向update_r的参数列表添加vs。
#3
1
Put an additional global
statement containing all your globals after each def
statement. Otherwise, all globals are transformed into locals within your def without it.
在每个def语句之后,添加一个包含全局变量的全局语句。否则,在没有它的情况下,所有的全局变量都转换为本地的局部变量。
def update_v(n,vs,ve,ms,me,dt,fs,fe):
global vs, ve, ...
#4
0
On line 39 you do
在第39行。
vs,ve=update_v(n,vs,ve,ms,me,dt,fs,fe)
while you are inside a function. Since you defined a global variable called vs, you would expect this to work. It would have worked if you had:
当你在一个函数里面。既然您定义了一个名为vs的全局变量,那么您将期望它能够工作。如果你有:
vs_new,ve_new = update_v(n,vs,ve,ms,me,dt,fs,fe)
because then the interpreter knows vs in the function arguments is the global one. But since you had vs in the left hand side, you created an uninitialized local variable.
因为在函数参数中解释器知道vs是全局变量。但是由于左侧有vs,所以创建了一个未初始化的局部变量。
But dude, you have a much bigger problem in your code: update_r calls update_v, update_v calls force, and force calls update_r - you will get a stack overflow :)
但是,您的代码中有一个更大的问题:update_r调用update_v, update_v调用force,并且强制调用update_r——您将得到一个堆栈溢出:)