模拟一个炮弹人的轨迹

 

 

 

 

 

# 0.  Place your necessary imports here.  You may find it useful to be able to plot when debugging and visualizing your result.

import numpy as np

# 1.  Create a 1D vector named `t` and 2D arrays named `vx`, `vy`, `x`, and `y` to hold the state variables, of size 90 x 1001.

t = np.linspace(0,10,1001)

print(len(t))

vx = np.empty((90,1001))

vy = np.empty((90,1001))

x = np.empty((90,1001))

y = np.empty((90,1001))


 

# 2.  Store the angles from 1 to 90 degrees as radians in a variable called `radians`. Use this to initialize the state variables for `vx` and `vy`.

angles = np.arange(1,91)

radians = angles * 2*np.pi/360

# 3.  Define properties like gravity, Callista's surface area, and Callista's mass, and any other parameters you may need as they come up.

A = 0.8  # m^2

g = -9.8  # m/s^2

m = 65 # kg

Cd = 1.4 # drag coeffiency

D = 1.225 # kg/m^3

# etc.  Note that I expect an `initial_height` and `initial_velocity` below.

# 4.  At this point, you should have defined `t`, `x`, `y`, `vx`, `vy`, `radians`, and the properties you need.  Now, initialize the starting condition in each array:

initial_height = 5 # m

initial_velocity = 70 # m/s

for i in range(len(radians)):

    x[ i,0 ] = 0

    y[ i,0 ]  = initial_height

    vx[ i,0 ] = initial_velocity * np.cos( radians[ i ] )

    vy[ i,0 ] = initial_velocity * np.sin( radians[ i ] )  # (see "Angles" above)

# 5.  Now you are ready to begin the simulation proper.  You will need two loops, one over every angle, and one over every time step for that angle's launch.

for i in range(len(radians)):  # loop over each angle

    #i=k+1

    for j in range(len(t)):  # loop over each time step

        #j=l+1

        # check that the location isn't below the ground; if so, adjust as specified above

        if j>0:

            if y[i,j-1]==0:

                x[i,j]=x[i,j-1]

                y[i,j]=0

                vx[i,j]=0

                vy[i,j]=0

            else:

                # calculate the acceleration including drag (for both x and y, x shown)

                v = np.sqrt( vx[ i,j-1]**2 + vy[ i,j-1]**2 )

                ax = -( 0.5*D*Cd*A/m ) * v**2 * ( vx[ i,j-1 ] / v )

                ay = -( 0.5*D*Cd*A/m ) * v**2 * ( vy[ i,j-1 ] / v )+g

            # calculate the change in position at time `ts` using the current velocities (`vx[ i,j ]`) and the previous positions (`x[ i,j-1 ]`).  This is slightly different from the previous example you solved in an earlier homework.

                ts=t[j]-t[j-1]

                vx[i,j]=vx[i,j-1]+ax*ts

               

                vy[i,j]=vy[i,j-1]+ay*ts

                y[i,j] = y[i,j-1]+vy[i,j]*ts+0.5*ay*ts**2

                x[i,j] = x[i,j-1]+vx[i,j]*ts+0.5*ax*ts**2

            # check that the location isn't below the ground (again!); if so, adjust

                if y[i,j]<=0:

                    x[i,j]=x[i,j-1]

                    y[i,j]=0

                    vx[i,j]=0

                    vy[i,j]=0

# 6.  The purpose of these calculations was to show which angle yielded the farthest distance.  Find this out and store the result in a variable named `best_angle`.

max= np.max(x)

maxi=np.where(max==x[:,1000])

print(maxi)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值