這個問題是從 Unix & Linux Stack Exchange遷移過來的,因為它可以在 Stack Overflow 上回答。 3 天前遷移 。
我是這個平臺以及整個軟體開發和編碼的完全新手。我需要一些幫助來讓我的 python 腳本作業并輸出一個圖形。我讓它在 MATLAB 中作業,而不是在 Python 中作業,當我在 VSCode 中運行除錯時,沒有任何錯誤,但它沒有按我想要的那樣作業。
由于某種原因,在繪圖部分它沒有決議 max_range(黃色波浪線),但它在代碼的其他地方決議。不知道這個是怎么破解的?
我的程式在 GitHub 的一個開放存盤庫中,所以如果可以,請查看并提供幫助!
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)
uj5u.com熱心網友回復:
Max_range 未在您提供的代碼中定義。你忘記了一些事情。此外,您不需要 math 模塊,因為您有 numpy. 所以,我用“numpy”替換了涉及“math”的陳述句。這是您的代碼的作業版本: 注意:我注釋掉了不必要的匯入。
#import argparse # For the moment, you don't use it. But you can uncomment it later.
#import logging
#import math # you don't need it since you have numpy
#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 * (np.pi / 180)) # Converts launch angle into radians
max_range = int(((v0**2) / g) * (np.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 * np.tan(launch_angle)) - g / (2 * (v0**2) *
(np.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)])
#Commented because max_range is nowhere defined
plt.xlabel('Range [m]')
plt.ylabel('Height [m]')
plt.show()
if __name__ == '__main__':
x_values, y_values= trajectory(0, 0, 940, 45, 9.81)
# because trajectory returns 2 vectors...
# and you forgot to call plot
plot(x_values,y_values)
最好的問候,斯蒂芬
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/362267.html
