函数是斜上抛运动 ,以v0,y0,和角度作为参数赋值,导入math库,用函数计算y值
怎么赋值啊
【有帮助请采纳】
from math import *
def func(x,v0,y0,theta,g=9.8):
y =x*tan(theta*(pi/180))-(g*x**2)/(2*v0**2*(cos(theta*(pi/180)))**2)+y0
print('y值计算结果为:%.5f米'%(y))
注意一下,这里的g默认为9.8,x也是参数,该函数直接打印y值,没有返回值,这里输入theta的单位是角度,不是弧度
【有帮助请采纳】
感觉题主的题目里面少了两个条件:一是计算y值的函数以时间t为自变量,二是需要忽略空气阻力。加上这两个条件,这个函数就很容易写出来。
>>> import math
>>> def func(t, theta, v0, x0=0, y0=0, g=9.8):
vx = v0 * math.cos(math.radians(theta)) # 计算初始速度的水平分量
vy = v0 * math.sin(math.radians(theta)) # 计算初始速度的垂直分量
x = x0 + vx * t # 计算水平坐标
y = y0 + vy * t - 0.5 * g * t**2 # 计算垂直坐标
return x, y
>>> func(0.5, 80, 3) # 在(0,0)点以3m/s速度向80°角方向抛出,0.5秒钟后的位置
(0.2604722665003956, 0.2522116295183119)
如果使用numpy和matplotlib,可以绘出抛物线形状。假设站在(0,5)处(y0=5),以3m/s速度向80°角方向抛出,计算出0~1秒钟内连续的若干点,绘制抛物线如下图。
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> t = np.arange(0, 1, 0.01) # 0~1秒之内,每隔0.01秒取样
>>> x, y = func(t, 80, 3, 0, 5) # 在(0,5)点以3m/s速度向80°角方向抛出
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x0000027A0DDE5348>]
>>> plt.show()