+ 1

Net FOrce on an Object code Error.

#A significant part of introductory physics is calculating the net force on an object based #on several different magnitudes and directions. If you're unfamiliar with how this works, #Each force acting on the object is defined by its angle and magnitude. The process for #calculating net force is: # - For each force, break the force into its horizontal and vertical components. The #horizontal component can be calculated as magnitude * cos(angle), and the vertical #component can be calculated as magnitude * sin(angle). # - Sum all the horizontal components to find the total horizontal force, and sum the #vertical components to find the total vertical force. # - Use the Pythagorean theorem to calculate the total magnitude: sqrt(total_horizontal #^ 2 + total_vertical ^ 2) # - Use inverse tangent to calculate the angle: atan(total_vertical / total_horizontal) #Write a function called find_net_force. find_net_force shouldtake one parameter as #input: #a list of 2-tuples. Each 2-tuple in the list is a (magnitude, angle) pair. angle will be in #degrees between -180 and 180. #Return a 2-tuple containing the final magnitude and angle of all the forces. angle should #again be in degrees. You should round both magnitude and angle to one decimal place, ***************************************************************************************************** from math import sin, cos, tan, asin, acos, atan, radians, degrees, sqrt def find_net_force(force): hor_comp = 0 vert_comp = 0 for tuple in force: degre_radian = radians(tuple[1]) hor_comp = hor_comp + (tuple[0] * cos(degre_radian)) vert_comp = vert_comp + tuple[0] * sin(degre_radian) mag = round((sqrt((hor_comp ** 2) + (vert_comp ** 2))) ,1) angle = round(degrees(atan(vert_comp/hor_comp)),1) return (mag, angle) forces = [(10, 135), (10, -135), (20, 0), (20, 180)] print(find_net_force(forces)) #The expected result is (14.1, 180.0) but my code is printing (14.1, -0.0)

28th Sep 2020, 11:23 AM
NG_ - avatar
1 Answer
You should use atan2 instead of atan in your angle calculation: angle = round(degrees(atan2(vert_comp,hor_comp)),1) Of course you'll need to import atan2 from math before you can use it :)
22nd Oct 2020, 8:45 PM