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:
r=rxhat(i)+ryhat(y)+rzhat(z)=(:rx,ry,rz:)
Or we can express a vector in terms of its magnitude and direction, where the magnitude is
|r|=r=rx2+ry2+rz22
and the azimuthal angle (angle in the x-y plane) is
=tan-1(ryrx)
and the polar angle (angle from the positive z-axis) is
=cos-1(rz|r|)
We can invert these relations to get rx,ry and rz in terms of the magnitude and angles:
rx=rsincos,ry=rsinsin,rz=rcos
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 comp_to_mag_direc, and mag_direc_to_comp
Each function should take in up to three arguments, and return up to three values
comp_to_mag_direc 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 z), in that order. The angles should be given in radians.
mag_direc_to_comp 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 x and y components, and optionally the z component if the polar angle was given.
Notes and Hints
Note that can be any angle from 0 to 2, but arctan will only give angles from -2 to +2. You could account for this with an if block to conditionally add to the angle. But even better is the math module's atan2, 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. I'd suggest x , y, and z for comp_to_mag_direc and mag, phi, and theta for mag_direc_to_comp, 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.
A vector in cartesian space can be represented in

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!