为什么我的代码错误地计算了(幅度,角度)对列表中的总力?
Why is my code miscalculating the total force from a list of (magnitude, angle) pairs?
我正在努力调试 Python 代码以从 (magnitude, angle)
对列表中计算总力(angle
在 -180 和 180 之间)。该任务要求我们:
- 对于每个力,将力分解为水平和垂直分量。水平分量可以计算为magnitude * cos(angle),垂直分量可以计算为magnitude * sin(angle)。
- 将所有水平分量相加得到总水平力,将垂直分量相加得到总垂直力。
- 利用勾股定理计算总数
幅度:sqrt(total_horizontal ^ 2 + total_vertical ^ 2)
- 用反正切法计算角度:
阿坦(total_vertical / total_horizontal)
编写一个名为 find_net_force 的函数。 find_net_force 应该将一个参数作为输入:一个二元组列表。列表中的每个 2 元组都是 (magnitude, angle) 对。角度将在 -180 到 180 度之间。
Return 包含所有力的最终大小和角度的二元组。角度应该再次以度为单位。您应该将大小和角度四舍五入到小数点后一位,您可以使用 round(magnitude, 1) 和 round(angle, 1).
现阶段我的代码是:
from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt
def find_net_force(mylist):
finalmag = 0
horiz = 0
vert = 0
finalangle = 0
for i in range(0, len(mylist)):
horiz += (mylist[i][0] * cos(mylist[i][1]))
vert += (mylist[i][0] * sin(mylist[i][1]))
finalmag += sqrt((horiz ** 2) + (vert ** 2))
finalangle = atan2(vert, horiz)
return (round(finalmag, 1), round(degrees(finalangle), 1))
但是,如果我 运行
forces = [(10, 90), (10, -90), (100, 45), (20, 180)]
print(find_net_force(forces))
代码 returns (76.0, 65.4)
而正确答案是 (87.0, 54.4)
。我不清楚为什么我的代码错误地计算了总力。
问题:为什么我的代码错误地计算了(大小,角度)对列表中的总力?
from math import sqrt, sin, cos, atan2, degrees, radians
def find_net_force(forces):
h_force = sum(
[
force[0] *
cos(radians(force[1])) for force in forces
]
)
v_force = sum(
[
force[0] *
sin(radians(force[1])) for force in forces
]
)
r_force = round(sqrt((h_force ** 2) + (v_force ** 2)), 1)
r_angle = round(degrees(atan2(v_force, h_force)), 1)
return (r_force, r_angle)
forces = [(10, 90), (10, -90), (100, 45), (20, 180)]
print(find_net_force(forces))
试试这个。我使用了列表理解。
我正在努力调试 Python 代码以从 (magnitude, angle)
对列表中计算总力(angle
在 -180 和 180 之间)。该任务要求我们:
- 对于每个力,将力分解为水平和垂直分量。水平分量可以计算为magnitude * cos(angle),垂直分量可以计算为magnitude * sin(angle)。
- 将所有水平分量相加得到总水平力,将垂直分量相加得到总垂直力。
- 利用勾股定理计算总数 幅度:sqrt(total_horizontal ^ 2 + total_vertical ^ 2)
- 用反正切法计算角度: 阿坦(total_vertical / total_horizontal)
编写一个名为 find_net_force 的函数。 find_net_force 应该将一个参数作为输入:一个二元组列表。列表中的每个 2 元组都是 (magnitude, angle) 对。角度将在 -180 到 180 度之间。
Return 包含所有力的最终大小和角度的二元组。角度应该再次以度为单位。您应该将大小和角度四舍五入到小数点后一位,您可以使用 round(magnitude, 1) 和 round(angle, 1).
现阶段我的代码是:
from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt
def find_net_force(mylist):
finalmag = 0
horiz = 0
vert = 0
finalangle = 0
for i in range(0, len(mylist)):
horiz += (mylist[i][0] * cos(mylist[i][1]))
vert += (mylist[i][0] * sin(mylist[i][1]))
finalmag += sqrt((horiz ** 2) + (vert ** 2))
finalangle = atan2(vert, horiz)
return (round(finalmag, 1), round(degrees(finalangle), 1))
但是,如果我 运行
forces = [(10, 90), (10, -90), (100, 45), (20, 180)]
print(find_net_force(forces))
代码 returns (76.0, 65.4)
而正确答案是 (87.0, 54.4)
。我不清楚为什么我的代码错误地计算了总力。
问题:为什么我的代码错误地计算了(大小,角度)对列表中的总力?
from math import sqrt, sin, cos, atan2, degrees, radians
def find_net_force(forces):
h_force = sum(
[
force[0] *
cos(radians(force[1])) for force in forces
]
)
v_force = sum(
[
force[0] *
sin(radians(force[1])) for force in forces
]
)
r_force = round(sqrt((h_force ** 2) + (v_force ** 2)), 1)
r_angle = round(degrees(atan2(v_force, h_force)), 1)
return (r_force, r_angle)
forces = [(10, 90), (10, -90), (100, 45), (20, 180)]
print(find_net_force(forces))
试试这个。我使用了列表理解。