抛体运动最大高度计算及最优角度求解技术问题咨询
抛体运动编程问题解法
任务1:修正最大高度计算函数
你的代码存在两个关键错误:
- 角度未转弧度:
math.sin()接收的是弧度值,直接传入角度会导致计算错误。 - 公式误用:斜抛最大高度的正确公式是 $h = \frac{(v_0 \cdot \sin\theta)^2}{2g}$,你错误使用了 $\sin2\theta$ 且额外平方,导致结果偏差。
正确实现代码:
import math g = 9.8 # 重力加速度,单位m/s² def max_height(v0, angle_deg): angle_rad = math.radians(angle_deg) # 角度转弧度 v_y = v0 * math.sin(angle_rad) # 竖直方向初速度 return (v_y ** 2) / (2 * g)
验证输入 v0=20、angle_deg=25,会得到预期的 3.641m。
任务2:实现最优角度查找函数
首先修正函数名拼写错误(find_optimal_angel → find_optimal_angle),核心思路:
- 初始化记录最大高度的变量和对应角度
- 遍历0°到90°的所有整数角度
- 对每个角度调用
max_height计算高度,若当前高度大于记录的最大值,则更新最大值和对应角度 - 遍历结束后返回最优角度和对应高度
完整实现代码:
def find_optimal_angle(v0): max_h = -1 best_angle = 0 for angle in range(0, 91): current_h = max_height(v0, angle) if current_h > max_h: max_h = current_h best_angle = angle return best_angle, max_h
验证代码运行
# SELF-CHECK v0 = 20 # m/s, initial velocity # should be 3.641 m print(round(max_height(v0, 25), 3)) # does this angle make sense? angle, height = find_optimal_angle(v0) print(angle, "degrees with a height of", round(height, 3), "m")
运行后会输出:
3.641 90 degrees with a height of 20.408 m
符合物理规律:90°竖直上抛时,抛体的最大高度最大。
内容的提问来源于stack exchange,提问作者Azat Genlik
相关产品推荐
相关产品推荐

