python - 有没有办法在代码中使用 np.array

标签 python arrays numpy

我想知道是否有办法将这段代码转换为 np.array 代码。然后将其添加到 link .我想添加一个球发射的角度。

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

最佳答案

您可以采用这种方法:

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

关于python - 有没有办法在代码中使用 np.array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30569883/

相关文章:

python - 来自另一个文件的 pytest fixture

python - 将字符串字典转换为 numpy 数组

python - pandas - 如何根据第一次约会重新采样?

python - python中向量数组的范数

python - 返回满足条件的类的所有实例

python - 在同一图中将 x 轴的 2 个刻度

python - 避免在文件末尾换行 - Python

ios - core Data 重启应用后删除数据

java - 在JAVA中对两个或多个数组进行排序

python - Numpy 数组 : row/column wise argmax with random ties