【发布时间】:2021-12-28 22:33:20
【问题描述】:
我是这个平台以及整个软件开发和编码的新手。我需要一些帮助才能让我的 python 脚本工作并输出图表。我让它在 MATLAB 中工作,而不是在 Python 中,当我在 VSCode 中运行调试时没有任何错误,但它没有按我想要的那样工作。
由于某些原因,在绘图部分它没有解析 max_range(黄色波浪线),但它在代码的其他地方被解析。我不确定这是怎么坏的?
我的程序在 GitHub 上的一个开放 repo 中,所以如果可以的话,请看看并提供帮助!
https://github.com/ashfletch/projectile-motion-project/blob/main/projectile_motion/projectile.py
我最终想让程序与 GUI 一起工作,但我还没有遇到这个障碍。
谢谢
灰
python 脚本:projectile_motion.py
import argparse
import logging
import math
import os
import sys
import tkinter as tk
from tkinter import filedialog, PhotoImage
import matplotlib.pylab as plt
import numpy as np
def trajectory(x0: int, y0: int, v0: int, theta: int, g: float) -> tuple:
launch_angle = (theta * (math.pi / 180)) # Converts launch angle into radians
max_range = int(((v0**2) / g) * (math.sin(2 * launch_angle))) # Calculates range in x-direction (using vector multiplication)
x_step = int(max_range / 100) # Calculates step, using 100 values for plotting up to the range
x_values = []
for value in range(x0, max_range, x_step):
x_values.append(value)
y_values = []
for x in x_values:
y_values.append(((x * math.tan(launch_angle)) - g / (2 * (v0**2) *
(math.cos(launch_angle))**2) * x**2) + y0)
return (x_values, y_values)
def projectilemotion(x0: int, y0: int, v0: int, theta: int, g: float):
pass
"""projectlemotion requires 5 user input arguments which are as follows;
Args:
x0: Initial displacement in 'x' domain in [m].
y0: Initial displacement in 'y' domain in [m].
v0: Initial velocity of projectile in [m/s].
theta: launch angle of projectile in [degrees]
g: The acceleration due to gravity for either Earth or Moon in [m/s**2].
Returns:
A plotted trajectory of the projectile under the conditions defined by
the args above, calculating the following outputs:
xPeak: the value of x [m] at which the yPeak is achieved.
yPeak: the maximum displacement in y-direction [m].
maxRange: the maximum displacement in x-direction [m].
Raises:
Error:
"""
def plot(x_values, y_values):
plt.plot(x_values, y_values)
plt.axis([0, max_range, 0, max(y_values)])
plt.xlabel('Range [m]')
plt.ylabel('Height [m]')
plt.show()
if __name__ == '__main__':
trajectory(0, 0, 940, 45, 9.81)
【问题讨论】:
标签: python github visual-studio-code