zoukankan      html  css  js  c++  java
  • Python——铅球飞行计算问题

    一、

    1、IPO描述为:
    输入:铅球发射角度、 初始速度(m/s)、 初始高度(m)

    处理:模拟铅球飞行,时刻更新铅球在飞行中的位置

    输出:铅球飞行距离(m)

    可以拆分小的时间段。任意时刻的位置,都是 由前面的位置叠加新的时间段位移引起的,最终的控制条件为,当落地时结束,即y轴坐标为0时结束。微分的思想

    (1)

     1 #from math import pi,sin,cos,radians
     2 import math
     3  
     4 def main():   
     5     angle = eval(input("Enter the launch angle (in degrees):"))
     6     vel = eval(input("Enter the initial velocity (in meters/sec):"))
     7     h0 = eval(input("Enter the initial height (in meters):"))
     8     time = eval(input("Enter the time interval: "))
     9  
    10     xpos = 0
    11     ypos = h0
    12  
    13     theta = math.radians(angle)
    14     xvel = vel * math.cos(theta)
    15     yvel = vel * math.sin(theta)     
    16  
    17     while ypos >= 0:
    18         xpos = xpos + time * xvel
    19         yvell = yvel - time * 9.8
    20         ypos = ypos + time * (yvel + yvell)/2.0
    21         yvel = yvell
    22         
    23     print("
    Distance traveled:{0:0.1f}meters.".format(xpos))
    24      
    25 if __name__ == "__main__":
    26     main()

    (2)画出轨迹

     1 from math import pi,sin,cos,radians
     2 import turtle
     3 
     4 def drawLine(x, y):#画线
     5     turtle.pendown()
     6     turtle.goto (x, y)
     7 
     8 def drawText(x, y,n):
     9     turtle.penup()
    10     turtle.goto (x, y)
    11     turtle.pendown()
    12     turtle.write((float("%2.f"%x)/n,float("%2.f"%y)/n))#注意表达方式,只保留两位小数
    13 
    14 def main():   
    15     angle = eval(input("Enter the launch angle (in degrees):"))
    16     vel = eval(input("Enter the initial velocity (in meters/sec):"))
    17     h0 = eval(input("Enter the initial height (in meters):"))
    18     time = eval(input("Enter the time interval: "))
    19     n=20#显示系数
    20  
    21     xpos = 0
    22     ypos = h0
    23 
    24     drawLine(500,0) #初始坐标轴
    25     drawLine(0,0)
    26     drawLine(0,300)
    27     drawLine(0,18)
    28  
    29     theta = radians(angle)
    30     xvel = vel * cos(theta)
    31     yvel = vel * sin(theta)     
    32  
    33     while ypos >= 0:
    34         xpos = xpos + time * xvel
    35         yvell = yvel - time * 9.8
    36         ypos = ypos + time * (yvel + yvell)/2.0
    37         yvel = yvell
    38         drawLine(xpos*n,ypos*n)
    39         drawText(xpos*n,ypos*n,n)
    40         
    41     turtle.hideturtle()#隐藏箭头
    42     print("
    Distance traveled:{0:0.1f}meters.".format(xpos))
    43 
    44 
    45 if __name__=="__main__":
    46     main()

    引入turtle库,画出轨迹图

     (3)程序模块化

     1 from math import pi,sin,cos,radians
     2 
     3 def getInputs():
     4     angle=eval(input("enter the launch angle (degree):"))
     5     vel = eval(input("Enter the initial velocity (in meters/sec):"))
     6     h0 = eval(input("Enter the initial height (in meters):"))
     7     time = eval(input("Enter the time interval: "))
     8     return angle,vel,h0,time
     9 
    10 def getXYComponents(vel,angle):
    11     theta=radians(angle)
    12     x=vel*cos(theta)
    13     y=vel*sin(theta)
    14     return x,y
    15 
    16 def updatePosition(time,xpos,ypos,xvel,yvel):
    17     xpos=xpos+time*xvel
    18     yvell=yvel-9.8*time
    19     ypos=ypos+(yvell+yvel)*time/2
    20     yvel=yvell
    21     return xpos,ypos,yvel
    22 
    23 def main():
    24     angle,vel,h0,time=getInputs()
    25     xvel,yvel=getXYComponents(vel,angle)
    26     xpos,ypos=0,h0
    27     while ypos>=0:
    28         xpos,ypos,yvel=updatePosition(time,xpos,ypos,xvel,yvel)
    29     print("distance traveled:{0:0.1f}meters.".format(xpos))
    30 
    31 if __name__=="__main__":
    32     main()
  • 相关阅读:
    241. Different Ways to Add Parentheses java solutions
    89. Gray Code java solutions
    367. Valid Perfect Square java solutions
    46. Permutations java solutions
    116. Populating Next Right Pointers in Each Node java solutions
    153. Find Minimum in Rotated Sorted Array java solutions
    判断两颗树是否相同
    求二叉树叶子节点的个数
    求二叉树第k层的结点个数
    将二叉排序树转换成排序的双向链表
  • 原文地址:https://www.cnblogs.com/ruo-li-suo-yi/p/7412714.html
Copyright © 2011-2022 走看看