Problem 45281. A "Complex" Converter: Rectangular Form <-> Polar Form
BACKGROUND / MOTIVATION:
Complex numbers can be an important tool in an electrical engineer's toolbox because they can help us describe / work with sinusoidal signals.
Sinusoidal signals can be found all over the place in day-to-day life (in music, light, communication systems, power systems, etc.):
- "Why Study Sinusoids?": https://www.youtube.com/watch?v=yXjXJ5OlNyQ
- "Euler's formula": https://www.khanacademy.org/science/electrical-engineering/ee-circuit-analysis-topic/ee-ac-analysis/v/ee-eulers-formula
When working with complex numbers, sometimes it's easier to work with the "rectangular/Cartesian form" (z = x + j*y) and sometimes it's easier to work with the "polar form" (r ∠ θ).
An analogy:
- Using the rectangular form is sometimes like using decimals: it can make the numbers easier to add / subtract
- Using the polar form is sometimes like using fractions: it can the numbers easier to multiply / divide
- In the end, the two forms are equivalent, but sometimes they're easier to work with in one form instead of another.
PROBLEM DESCRIPTION
Write a function which converts between the rectangular form and the polar form.
You can view a comparison of the two forms here: https://www.khanacademy.org/science/electrical-engineering/ee-circuit-analysis-topic/ee-ac-analysis/v/ee-complex-numbers
The variable "form" will be used to determine whether the function converts from rectangular to polar or from polar to rectangular.
The function takes the following inputs:
- "input1" - a variable which is either "x" (the real component in rectangular form) or "r" (the radius in polar form)
- "input2" - a variable which is either "y" (the complex component in rectangular form) or "theta" (the angle, in degrees, in polar form)
- "form" - a variable which is set to either "r2p" (to convert from rectangular to polar) or "p2r" (to convert from polar to rectangular)
The function will output the variable "output" in the form of a column vector [output1;output2] where:
- "output1" - a component of the output which is either "x" (the real component in rectangular form) or "r" (the radius in polar form)
- "output2" - a component of the output which is either "y" (the complex component in rectangular form) or "theta" (the positive angle, in degrees, in polar form)
The test suite will round the components of your output vector to 4 decimal places.
FEEDBACK
Please feel free to leave feedback on this problem in the comments! :)
Solution Stats
Problem Comments
-
1 Comment
It's a good problem, and I liked that you've included even a motivation for it (most problems at Cody are unpolished and unlike yours). However, most Cody players would probably skip this problem because programming problems are usually very objective, they will give us a task with maybe some examples of expected output.
Solution Comments
Show commentsProblem Recent Solvers29
Suggested Problems
-
Remove any row in which a NaN appears
8588 Solvers
-
116 Solvers
-
Create an index-powered vector
768 Solvers
-
454 Solvers
-
Converts numbers into characters
1322 Solvers
More from this Author1
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!