Question: A vector in cartesian space can be represented in two ways. In component form, we write the vector in terms of its components: r =
A vector in cartesian space can be represented in two ways. In component form, we write the vector in terms of its components:
hathathat::
Or we can express a vector in terms of its magnitude and direction, where the magnitude is
and the azimuthal angle angle in the plane is
and the polar angle angle from the positive axis is
We can invert these relations to get and in terms of the magnitude and angles:
We'd like to be able to translate between these two forms of vectors.
Tasks
In the next cell, create and document two functions called comptomagdirec, and magdirectocomp
Each function should take in up to three arguments, and return up to three values
comptomagdirec should take in an x component, a y component, and optionally a z component. It should return the magnitude, azimuthal angle, and potentially the polar angle if the user provided a value for in that order. The angles should be given in radians.
magdirectocomp will do the exact opposite. It should take in a magnitue, an azimuthal angle in radians and optionally a polar angle also in radians It should return at least the and components, and optionally the component if the polar angle was given.
Notes and Hints
Note that can be any angle from to but arctan will only give angles from to You could account for this with an if block to conditionally add to the angle. But even better is the math module's atan which can take care of everything for you. Give it a google to see how it works.
You will need to set default values for arguments. Be sure to document these arguments appropriately according to the Numpydoc standard.
You may call the arguments to your functions whatever you want. Id suggest x y and z for comptomagdirec and mag, phi, and theta for magdirectocomp, but the tests are indifferent to what you call them.
Big hint: None makes for a good default value for the optional third parameters; users will never use it intentionally, so you won't confuse it for a valid input.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
