Question: from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt #First, we define the function: def find_net_force(forces): #We know from

from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt

#First, we define the function:
def find_net_force(forces):
   
   #We know from the directions that our goal is to find the
   #total horizontal and total vertical forces. So, let's
   #create variables to hold those values so we can add to
   #them as we go along:
   total_horizontal = 0
   total_vertical = 0
   
   #Now, let's iterate through each force in the forces:
   for force in forces:
       
       #To make our code easier to read, let's pull the
       #magnitude and angle out from the tuple:
       magnitude, angle = force
       
       #The directions told us we need to convert our
       #angles to radians to use Python's trignometric
       #functions:
       angle = radians(angle)
       
       #Now, the horizontal component is the magnitude
       #times the cosine of the angle:
       horizontal = magnitude * cos(angle)
       
       #And the vertical component is the magnitude times
       #the sine of the angle:
       vertical = magnitude * sin(angle)
       
       #Now that we've calculated the horizontal and
       #vertical components, let's add those to our
       #running tallies:
       total_horizontal += horizontal
       total_vertical += vertical
   
   #The net magnitude is the hypotenuse of the triangle
   #with these two components as their legs. So, we can
   #calculate the net magnitude using the Pythagorean
   #theorem:
   net_magnitude = sqrt(total_horizontal**2 + total_vertical**2)
   
   #Then as instructed, we round it to one decimal point:
   net_magnitude = round(net_magnitude, 1)
   
   #Next, we need to calculate the angle. As directed, we
   #ccan use  the atan2 function from Python's math
   #library, which takes the vertical and horizontal
   #components as arguments:
   net_angle = atan2(total_vertical, total_horizontal)
   
   #Then, we convert this back to degrees:
   net_angle = degrees(net_angle)
   
   #And round it to one decimal point as instructed:
   net_angle = round(net_angle, 1)
   
   #Last, we return the tuple of the magnitude and angle:
   return (net_magnitude, net_angle)

forces = [(10, 90), (10, -90), (100, 45), (20, 180)]
print(find_net_force(forces))

 

 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 

#Revise the above to use a file instead of
#a list as its parameter. Name this new function
#find_net_force_from_file. The function should take one
#parameter, the name of a file. The function should return
#the net magnitude and direction, just as it did in the other
#problem.
#
#Each line of the file will have two numbers, both integers:
#the first number will be the magnitude, and the second
#number will be the angle (in degrees, from -180 to 180).
#There will be a space between them.
#
#HINT: You may have multiple functions in your code if you
#want!
#
#HINT 2: Try to write  to  reuse as much
#of your earlier code as possible. Remember, when loading
#from a file, any text you load is initially a string. You'll
#almost certainly need to use the split() method.

 

from math import sin, cos, tan, asin, acos, atan2, radians, degrees, sqrt

 

#My function here!

def find_net_force_from_file(forces):

   f=open(forces, "w")
   
   total_horizontal = 0
   total_vertical = 0

   for force in forces:
       magnitude, angle = force.strip()
       angle = radians(angle)
       horizontal = magnitude * cos(angle)
       vertical = magnitude * sin(angle)
       total_horizontal += horizontal
       total_vertical += vertical
   

   net_magnitude = sqrt(total_horizontal**2 + total_vertical**2)
   net_magnitude = round(net_magnitude, 1)
   net_angle = atan2(total_vertical, total_horizontal)
   net_angle = degrees(net_angle)
   net_angle = round(net_angle, 1)
   return (net_magnitude, net_angle)
 

#Below are some lines of code that will test your function.
#You can change the value of the variable(s) to test your
#function with different inputs.
#
#If your function works correctly, this will originally
#print: (87.0, 54.4)
print(find_net_force_from_file("a_few_angles.txt"))
 

 

#####

I'm stuck not sure how to move forward

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!