Python——铅球飞行计算问题
一、
1、IPO描述为:
输入:铅球发射角度、 初始速度(m/s)、 初始高度(m)
处理:模拟铅球飞行,时刻更新铅球在飞行中的位置
输出:铅球飞行距离(m)
可以拆分小的时间段。任意时刻的位置,都是 由前面的位置叠加新的时间段位移引起的,最终的控制条件为,当落地时结束,即y轴坐标为0时结束。微分的思想
(1)
#from math import pi,sin,cos,radians
import math def main():
angle = eval(input("Enter the launch angle (in degrees):"))
vel = eval(input("Enter the initial velocity (in meters/sec):"))
h0 = eval(input("Enter the initial height (in meters):"))
time = eval(input("Enter the time interval: ")) xpos = 0
ypos = h0 theta = math.radians(angle)
xvel = vel * math.cos(theta)
yvel = vel * math.sin(theta) while ypos >= 0:
xpos = xpos + time * xvel
yvell = yvel - time * 9.8
ypos = ypos + time * (yvel + yvell)/2.0
yvel = yvell print("\nDistance traveled:{0:0.1f}meters.".format(xpos)) if __name__ == "__main__":
main()

(2)画出轨迹
from math import pi,sin,cos,radians
import turtle def drawLine(x, y):#画线
turtle.pendown()
turtle.goto (x, y) def drawText(x, y,n):
turtle.penup()
turtle.goto (x, y)
turtle.pendown()
turtle.write((float("%2.f"%x)/n,float("%2.f"%y)/n))#注意表达方式,只保留两位小数 def main():
angle = eval(input("Enter the launch angle (in degrees):"))
vel = eval(input("Enter the initial velocity (in meters/sec):"))
h0 = eval(input("Enter the initial height (in meters):"))
time = eval(input("Enter the time interval: "))
n=20#显示系数 xpos = 0
ypos = h0 drawLine(500,0) #初始坐标轴
drawLine(0,0)
drawLine(0,300)
drawLine(0,18) theta = radians(angle)
xvel = vel * cos(theta)
yvel = vel * sin(theta) while ypos >= 0:
xpos = xpos + time * xvel
yvell = yvel - time * 9.8
ypos = ypos + time * (yvel + yvell)/2.0
yvel = yvell
drawLine(xpos*n,ypos*n)
drawText(xpos*n,ypos*n,n) turtle.hideturtle()#隐藏箭头
print("\nDistance traveled:{0:0.1f}meters.".format(xpos)) if __name__=="__main__":
main()


引入turtle库,画出轨迹图
(3)程序模块化
from math import pi,sin,cos,radians def getInputs():
angle=eval(input("enter the launch angle (degree):"))
vel = eval(input("Enter the initial velocity (in meters/sec):"))
h0 = eval(input("Enter the initial height (in meters):"))
time = eval(input("Enter the time interval: "))
return angle,vel,h0,time def getXYComponents(vel,angle):
theta=radians(angle)
x=vel*cos(theta)
y=vel*sin(theta)
return x,y def updatePosition(time,xpos,ypos,xvel,yvel):
xpos=xpos+time*xvel
yvell=yvel-9.8*time
ypos=ypos+(yvell+yvel)*time/2
yvel=yvell
return xpos,ypos,yvel def main():
angle,vel,h0,time=getInputs()
xvel,yvel=getXYComponents(vel,angle)
xpos,ypos=0,h0
while ypos>=0:
xpos,ypos,yvel=updatePosition(time,xpos,ypos,xvel,yvel)
print("distance traveled:{0:0.1f}meters.".format(xpos)) if __name__=="__main__":
main()
Python——铅球飞行计算问题的更多相关文章
- 使用python做科学计算
这里总结一个guide,主要针对刚开始做数据挖掘和数据分析的同学 说道统计分析工具你一定想到像excel,spss,sas,matlab以及R语言.R语言是这里面比较火的,它的强项是强大的绘图功能以及 ...
- 使用Python做科学计算初探
今天在搞定Django框架的blog搭建后,尝试一下python的科学计算能力. python的科学计算有三剑客:numpy,scipy,matplotlib. numpy负责数值计算,矩阵操作等: ...
- 使用Python做科学计算初探(转)
今天在搞定Django框架的blog搭建后,尝试一下python的科学计算能力. python的科学计算有三剑客:numpy,scipy,matplotlib. numpy负责数值计算,矩阵操作等: ...
- windows下如何快速优雅的使用python的科学计算库?
Python是一种强大的编程语言,其提供了很多用于科学计算的模块,常见的包括numpy.scipy.pandas和matplotlib.要利用Python进行科学计算,就需要一一安装所需的模块,而这些 ...
- Python之字符串计算(计算器)
Python之字符串计算(计算器) import re expression = '-1-2*((60+2*(-3-40.0+42425/5)*(9-2*5/3+357/553/3*99/4*2998 ...
- Python实现的计算马氏距离算法示例
Python实现的计算马氏距离算法示例 本文实例讲述了Python实现的计算马氏距离算法.分享给大家供大家参考,具体如下: 我给写成函数调用了 python实现马氏距离源代码: # encod ...
- 使用python装饰器计算函数运行时间的实例
使用python装饰器计算函数运行时间的实例 装饰器在python里面有很重要的作用, 如果能够熟练使用,将会大大的提高工作效率 今天就来见识一下 python 装饰器,到底是怎么工作的. 本文主要是 ...
- 《Python之BMI计算》
<Python之BMI计算> 前段时间写了个 BMI 因为刚刚开始学 有几个错误 第一个: 厘米我当时也没注意因为觉得去掉0.00的话后面1866666666是正确的BMI值 刚刚去看看去 ...
- 用Python进行实时计算——PyFlink快速入门
Flink 1.9.0及更高版本支持Python,也就是PyFlink. 在最新版本的Flink 1.10中,PyFlink支持Python用户定义的函数,使您能够在Table API和SQL中注册和 ...
随机推荐
- npm发布包的那些事
npm发包的那些事 最近一直在研习关于node的知识,发布包虽然是最基础的一点,但由于一些地方的不注意很容易发生错误,我整理了我可能出现过的一些发布包的过程中的一些error,现在分享给大家: 正确的 ...
- AJAX 的 Ajax返回数据之前的loading等待效果(gif效果等)
首先,我们通过ajax请求,向后台传递参数,然后后台经过一系列的运算之后向前台返还数据,我希望在等待数据成功返还之前可以展示一个loading.gif图 不废话,在页面上执行点击事件(<a sc ...
- ASP.NET Core 中间件自定义全局异常处理
目录 背景 ASP.NET Core过滤器(Filter) ASP.NET Core 中间件(Middleware) 自定义全局异常处理 .Net Core中使用ExceptionFilter .Ne ...
- 写react项目要注意的事项
1,className一定是大写字母开头,例如:App-logo,App,App-header. 2,有关react元素的更新,唯一办法是创建新元素,然后重新将其传入ReactDOM.render() ...
- 误删除所有redo日志的一组成员的处理过程
系统中共有3个日志文件组,每个组中各有一个日志文件成员.往系统中添加一个日志文件组,组中日志文件成员数量是2.SQL> alter database add logfile group 4 (' ...
- 28 复杂的使用Specification查询
/** * Specification的多表查询 */ @Test public void testFind() { Specification<LinkMan> spec = new S ...
- deepin15.11安装N卡驱动,实测!!!(可解决N卡电脑关机卡屏)
前言:deepin(深度)是一款由武汉深之度公司研发的一款适合国人日常学习的linux系统,其UI精美,美过Mac.它对于中国用户的一个亮点就是QQ微信等国软件傻瓜式安装(类似安卓应用商店安装),如果 ...
- Spring Boot从入门到精通(八)日志管理实现和配置信息分析
Spring Boot对日志的处理,与平时我们处理日志的方式完全一致,它为Java Util Logging.Log4J2和Logback提供了默认配置.对于每种日志都预先配置使用控制台输出和可选的文 ...
- 通过CGAL将一个多边形剖分成Delaunay三角网
目录 1. 概述 2. 实现 3. 结果 4. 参考 1. 概述 对于平面上的点集,通过Delaunay三角剖分算法能够构建一个具有空圆特性和最大化最小角特性的三角网.空圆特性其实就是对于两个共边的三 ...
- Pending 打断点
pending 英['pendɪŋ],美['pɛndɪŋ] a. 未决定的, 待决的, 行将发生的, 向外伸出的prep. 在等待...之际, 直到...时为止, 在...期间, 在...过程中 pe ...