# Problem 45281. A "Complex" Converter: Rectangular Form <-> Polar Form

• Created by Ali

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.):

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

20.13% Correct | 79.87% Incorrect
Last Solution submitted on Aug 16, 2023

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!