如何获得此Trinket代码以在标准Python中运行?

时间:2020-02-06 17:57:11

标签: python

我正在做这个简单的练习,以了解弹丸运动背后的物理原理。我想根据加速度,速度和位移的理论模型对其进行绘图。我实际上在Trinket中找到了该练习,但是我想将其应用于Spyder 4。我知道Trinket使用的命令与普通的python 3不同,而且我对python编程(或一般编程)还比较陌生,所以我无法弄清楚。

这是我来自Trinket的代码:

mem addr   | 0123456789ab # relative to the address of Derived object
Base1      | 1111         # 1 denotes address occupied by Derived::Base1
Base2      |     2222     # 2 denotes address occupied by Derived::Base2
i          |         iiii # i denotes address occupied by Derived::i
Derived    | DDDDDDDDDDDD # D denotes address occupied by Derived
-------------------------
all subobj | 11112222iiii

在Spyder上,错误代码是这样的:


thegraph=graph(xtitle="x position [m]", ytitle="y position [m]")
f1=gcurve(color=color.blue)

#starting position (x and y)
#you can change the starting position to whatever you like
x=0 #m
y=1.5 #m

#intial velocity
v0=7 #m/s
theta=30*pi/180 #this is converted to radians

#the startin x and y velocities
vx=v0*cos(theta)
vy=v0*sin(theta)

#gravitational constant
g=9.8 #N/kg
#mass - this doesn't matter, but I put it in anyway
m=.1 #kg

#starting time and time step
t=0 #sec
dt=0.01 #sec

#the loop run as long as the vertical position is >= to zero (the ground)
while y>=0:
  #calculate the force in the y-direction
  Fy=-m*g 
  #calculate the acceleration in the y-direction
  ay=Fy/m

  #update y velocity (use acceleration)
  vy=vy+ay*dt

  #update x and y position
  x=x+vx*dt
  y=y+vy*dt

  #update time 
  t=t+dt


  #plot x and y to get trajectory - you can change this
  f1.plot(x,y)

#print out stuff
print("final x position = ", x, " m")
print("time of flight = ", t, " s")

0 个答案:

没有答案