Sum of vectors program

--Originally published at Hector Martinez Alcantara

Today I finished a program that can tell you the sum of three, or as many vectors as you want and the vector that balance the system.

There is a menu that shows two options.

In the case A you type 3 forces with their angles, and then the program shows the sum of components in X, components in Y, the resulting vector with its angle, and the vector with the angle that balances the system.

In the case B you type how many forces you want, then you type the forces with their angles, and then the program shows the sum of components in X, components in Y, the resulting vector with its angle, and the vector with the angle that balances the system.

import math
#------------------SHOWS THE VECTOR AND IT'S COMPONENTS-----------------
def Vector(compx,compy):
 magnr=math.hypot(float(compx), float(compy))
 print("\nSumatoria de componentes en X " + str(compx))
 print("Sumatoria de componentes en Y " + str(compy))
 print("\nVector Resultante:")
 if compx<0.0 and compy>0.0:
 print("Segundo sector")
 angr=math.degrees(math.atan(compy/compx)) + 180.0
 elif compx<0.0 and compy<0.0:
 print("Tercer sector")
 angr=math.degrees(math.atan(compy/compx))+ 180.0
 elif compx>0.0 and compy<0.0:
 print("Cuarto sector")
 angr=360+math.degrees(math.atan(compy/compx))
 else:
 print("Primer sector")
 angr=math.degrees(math.atan(compy/compx))
 print("La magnitud del vector resultante es "+ str(magnr) +"N")
 print("El angulo del vector resultante es "+ str(angr) +"° del eje +x")
 print("\nVector Complementario:")
 if compx<0.0 and compy>0.0:
 print("Cuarto sector")
 angr= angr + 180.0
 elif compx<0.0 and compy<0.0:
 print("Primer sector")
 angr= angr - 180
 elif compx>0.0 and compy<0.0:
 print("Segundo sector")
 angr= angr - 180.0
 else:
 print("Tercer sector")
 angr= angr + 180.0
 print("La magnitud del vector complementario es "+ str(magnr) +"N")
 print("El angulo del vector complementario es "+ str(angr) +"° del eje +x")
#--------------MAIN PROGRAM--------------------------------
y=0
while y!=1:
 print("\nCasos:")
 print("A) Tres fuerzas, encontrar la fuerza 
pone en equilibrio el sistema") print("B) Determine cuantas y cuales fuerzas actuan sobre el sistema, y cual lo pone en equilibrio\n") op=input() op=op.lower() #---------------CASE A------------------------------------- if op=='a': magn1=input("Ingrese magnitud de fuerza 1\n") ang1=input("Ingrese angulo de fuerza 1 con respecto al eje +x\n") magn2=input("Ingrese magnitud de fuerza 2\n") ang2=input("Ingrese angulo de fuerza 2 con respecto al eje +x\n") magn3=input("Ingrese magnitud de fuerza 3\n") ang3=input("Ingrese angulo de fuerza 3 con respecto al eje +x\n") compx=(float(magn1)*math.cos(math.radians(float(ang1))))+(float(magn2)*math.cos(math.radians(float(ang2))))+(float(magn3)*math.cos(math.radians(float(ang3)))) compy=(float(magn1)*math.sin(math.radians(float(ang1))))+(float(magn2)*math.sin(math.radians(float(ang2))))+(float(magn3)*math.sin(math.radians(float(ang3)))) Vector(compx,compy) #---------------CASE B------------------------------------- elif op=='b': cant=input("¿Cuántas fuerzas actuan sobre el sistema?\n") magnitudes=[] angulos=[] for x in range(int(cant)): magn=input("Ingrese magnitud de fuerza " + str(x+1) +"\n") ang=input("Ingrese angulo de fuerza " + str(x+1) + " con respecto al eje +x\n") magnitudes.append(float(magn)) angulos.append(float(ang)) compx=0 compy=0 for x in range(int(cant)): print(str(x)) compx=float(compx) + (float(magnitudes[x])*math.cos(math.radians(float(angulos[x])))) compy=float(compy) + (float(magnitudes[x])*math.sin(math.radians(float(angulos[x])))) Vector(compx,compy) #---------------NO OPTION----------------------------------- else: print("La opción "+ op +" no es una opcion")

The function Vector receives the sum of the components in X, Y  and realize the operations to find the resultant vector and its angle, then it only add 180 degrees to find the vector who balance the system.

Then the program receives an option and convert it to lower, if is ‘a’ enter to the case A, and if it’s ‘b’, enter to the case B, if its none of the other cases, it shows a message who says that’s not an option.

In the case A I do one by one the steps to do the sum of vectors, in the case B I ask first, how many forces are in the system, then in a loop I append the magnitude and the angle of the vectors to a list, and in another loop I sum all the components, finally I use the function Vector to show the resultant vectors.

And that’s all, thanks for reading.