2015-06-01 88 views
2

我想知道是否有辦法將此代碼轉換爲np.array代碼。然後將其添加到link。我想添加一個球從哪裏發射的角度。有沒有辦法可以在代碼中使用np.array

import numpy as np 
import scipy as sp 
from scipy.integrate import ode 
import matplotlib.pylab as pl 
import matplotlib.pyplot as plt 
import math 
from matplotlib import* 
from matplotlib.pyplot import * 
from __future__ import division 
import math 

def projectile_xy(initPos,g,initVel): 
    data_xy = [] 
    initTime = 0.0 
    while True: 

現在計算高度y

 y = initPos + (initTime * initVel * math.sin(theta)) - (g * initTime * initTime)/2 

彈丸擊中地面水平

 if y < 0: 
      break 

地計算x

 x = initVel * math.cos(theta) * initTime 

追加(X,Y)的元組的距離到名單

 data_xy.append((x, y)) 

使用時間在0.1秒

 initTime += 0.1 
    return data_xy 

g = 9.8 
#h = float(raw_input("Enter the height ")) 
initPos = float(raw_input("Enter the height ")) 
der = float(raw_input("Enter the angle ")) 
#v = float(raw_input("Enter velocity ")) 
initVel = float(raw_input("Enter velocity ")) 


theta = math.radians(der) # radians 
data_der = projectile_xy(initPos,g,initVel) 

找到最大高度增量...

point_height_max = max(data_der, key = lambda q: q[1]) 
xm, ym = point_height_max 
x_max = max(data_der)[0] 

print(''' 
Projectile Motion ... 
Using a firing angle of {} degrees 
and a muzzle velocity of {} meters/second 
the maximum height is {:0.1f} meters 
at a distance of {:0.1f} meters'''.format(der, initVel, ym, xm)) 
print "maximum distance" ,(x_max) 

輸入高度1 輸入角45 輸入速度30

Projectile Motion ... 
Using a firing angle of 45.0 degrees 
and a muzzle velocity of 30.0 meters/second 
the maximum height is 24.0 meters 
at a distance of 46.7 meters 
maximum distance 91.2167747731 

回答

1

您可以按照這種方法:

import numpy as np 

linear_vel = 20 
ang = np.pi/3 
y=10 
x=12 
g=9.8 
y_vel = linear_vel*np.cos(ang) 
x_vel = linear_vel*np.sin(ang) 


t = (y_vel+np.sqrt(y_vel**2+2*g*y))/g #time when projectile hits    ground 
n= 20 #number of instances of time you want 

time_values = np.linspace(0,int(t),n) 
axes = np.zeros((2,n)) 

for i in range(0,n): 
    axes[0,i]=x_vel*time_values[i] 
    axes[1,i]=y_vel*time_values[i]-0.5*g*time_values[i]**2 



#print time_values 
#print axes 
+0

可以使用廣播:'軸[0] = x_vel * time_values'和'軸[1] = y_vel * time_values - 0.5 * G * time_values ** 2'和得到擺脫for循環。 – ryanpattison

相關問題