在每次迭代中更新函数参数python

时间:2016-03-15 02:16:31

标签: python-2.7

我试图在每次迭代后更新我的函数参数但是没有这样做。请检查我的代码,因为我是python语言的新手。我的任务是在每次迭代后计算xps(表示位置集合)和v2ps(表示速度集合),并希望将它们相互绘制。基本的这个程序代表垂直向下移动的物体的碰撞,其中一个物体也与它们正在移动的平面相撞。

acc_grav = 10
m1 =float(input(" Input mass of ball one, m1: "))
m2 =float(input(" Input mass of ball two, m2: "))
time_steps =10000
num_coll_bounce = 0
num_ball_coll = 0
eps=1.e-6

def ball_coll(x1_old,v1_old,x2_old,v2_old,time_ball_coll):
v1 = v1_old - acc_grav*time_ball_coll
v2 = v2_old - acc_grav*time_ball_coll
x1 = x1_old + time_ball_coll*v1_old - 0.5*acc_grav*(time_ball_coll)**2
x2 = x2_old + time_ball_coll*v2_old - 0.5*acc_grav*(time_ball_coll)**2 
v1_ball_coll = (v1*(m1-m2)+(2*m2*v2))/(m1+m2) 
v2_ball_coll = (v2*(m2-m1)+(2*m1*v1))/(m1+m2) 
cumlv2=v2
return [v1,v2,x1,x2,v1_ball_coll,v2_ball_coll]

def floor_coll(x1_old,v1_old,x2_old,v2_old,time_floor_coll):
v1 = v1_old - acc_grav*time_floor_coll
v2 = v2_old - acc_grav*time_floor_coll
x1 = 0 #at the time of bonuce
x2 = x2_old + time_floor_coll*v2_old - 0.5*acc_grav*time_floor_coll**2
#update velocities following rules for collision with walls 
v1_bounce = -v1 
v2_bounce = v2
return [v1,v2,x1,x2,v1_bounce,v2_bounce]

for i in range(0, 10):
    x1_0 = 1
    x2_0 = 3 - (i-1)*0.1
    v1_0 = 2
    v2_0 = 2*v1_0
    xps = []
    v2ps = []

   for n in range (time_steps-1):
      time_ball_coll = (x2_0-x1_0)/(v1_0 - v2_0)
      time_floor_coll = (v1_0 + (v1_0**2 + 2*acc_grav*x1_0)**1/2)/acc_grav

        if ((time_ball_coll - time_floor_coll)<eps and v1_0 - v2_0 > 0):
           num_coll_bounce = num_coll_bounce + 1
           num_ball_coll = num_ball_coll + 1
           ball_coll(x1_0,v1_0,x2_0,v2_0,time_ball_coll)

           #xps[n] = x2_0
           #v2ps(n,num_ballcoll) = v2ini
           xps.append(x2_0)
           v2ps.append(v2_0) 


       else:
          num_coll_bounce = num_coll_bounce + 1
          floor_coll(x1_0,v1_0,x2_0,v2_0,time_floor_coll)
          #x1_old,v1_old,x2_old,v2_old,time_floor_coll = dd2

          x_1.append(x1_0)
          x_2.append(x2_0)

0 个答案:

没有答案