Vectors

--Originally published at Migue´s Blog

Vectors are quantities that have a magnitude and a direction and are represented with arrows.

imagen de vector
                                                                     Created in GlowScript by Rhett Allain

An vector is composed of two components, one component in the x-axis, and another one in the y-axis, this two components are the cathetus of right-angled triangle, and the vector is the hypotenuse.

To calculate this components we should use the functions “sin” and “cos”.

I have created a program that can calculate this components using the functions.

Captura de pantalla de 2016-08-29 10-33-38

To calculate the components python needs the functions, but python doesn’t have all the functions, because if python has them python would be enormous!

So we need to import the modules that we need.

The modules are files that contain Python definitions and statements.

In this case we need the functions “Sin” and “Cos” that are included in the module math, so we need to import them.

First we import the module math with the function “import” and after that from the module “math” we import the modules “Sin” and “Cos”

Captura de pantalla de 2016-08-29 10-48-29

Now we can use the functions that we need, and we can ask to the user to introduce the magnitude and the angle of the vectors.

But we have another problem, if we introduce the angle in degrees the program will give us results that are not correct because python works with radians, so we need to transform the degrees to radians.

To do that we only use a simply mathematical formula, we need to multiply the angle in degrees  by Pi and then divide the result by 180, to get a more accurate result we can use the constant Pi that python already knows, to call it we only write math.pi in the code.

Captura de pantalla de 2016-08-29 11-00-49

Now we can calculate the components without no problems.

To calculate the component

Captura de pantalla de 2016-08-29 11-04-34
Captura de pantalla de 2016-08-29 11-07-56
Captura de pantalla de 2016-08-29 11-12-09
“X” we multiply the angle by the function “cos” and then by the magnitude of the vector.

And to calculate the component in “Y” we multiply the angle by the function “sin” and the by the magnitude of the vector.

Captura de pantalla de 2016-08-29 11-04-34

Finally the program prints the result to show us the components in “x” and “y”

Captura de pantalla de 2016-08-29 11-07-56

This is the program running on the Ubuntu’s terminal.

Captura de pantalla de 2016-08-29 11-12-09