% A complex number can be represented in either % rectangular or polar form. % % In rectangular form, we write % % z = a + bj, % % where % % a = Re(z) % % and % % b = Im(z). % % In polar form, we write % % z = r phase theta, % % where % % r = |z| % % is the magnitude and theta is the argument, % or phase, of z. % % We will see how to mechanically convert % between these two representations. % First, suppose that a complex number is given % in rectangular form: % % z = a + bj. z = 3.2 - 4.7j; fprintf('\nThe complex number z:\n') z % Extract the real and imaginary components. a = real(z); b = imag(z); fprintf('\nThe real component a:\n') a fprintf('\nThe imaginary component b:\n') b % To convert from rectangular form to polar % form, first calculate the magnitude. % % From the Pythagorean theorem, % % __________ % / 2 2 % r = |z| = / a + b . % V r = sqrt(a^2 + b^2); fprintf('\nThe magnitude calculated manually:\n') r % MATLAB can calculate the same value directly % using abs(...). fprintf('\nThe magnitude using abs(z):\n') abs(z) % Next, calculate the argument theta. % % Geometrically, % % b % tan(theta) = -. % a % % It may therefore be tempting to calculate % % atan(b/a). % % This is not reliable because the quotient b/a % does not contain enough information to % determine the quadrant. % % Instead, use % % atan2(b, a). % % The function atan2(...) considers the signs % of both a and b and therefore determines the % correct quadrant. theta = atan2(b, a); fprintf('\nThe argument calculated using atan2:\n') theta % MATLAB can calculate the principal argument % of a complex number directly using angle(...). fprintf('\nThe argument using angle(z):\n') angle(z) % These angles are measured in radians. % % We can convert the argument to degrees using % rad2deg(...). fprintf('\nThe argument in degrees:\n') rad2deg(theta) % We have therefore mechanically converted % % z = a + bj % % into % % z = r phase theta. % % The conversion formulas are % % __________ % / 2 2 % r = |z| = / a + b % V % % and % % theta = atan2(b, a). % % In MATLAB, these can be calculated more % directly as % % r = abs(z) % % theta = angle(z). % Now suppose instead that we are given a % complex number in polar form: % % z = r phase theta. % % For example, suppose r = 5; theta = pi/3; fprintf('\nMagnitude r:\n') r fprintf('\nArgument theta in radians:\n') theta fprintf('\nArgument theta in degrees:\n') rad2deg(theta) % To convert from polar form to rectangular % form, use % % a = r*cos(theta) % % and % % b = r*sin(theta). a = r*cos(theta); b = r*sin(theta); fprintf('\nThe real component a:\n') a fprintf('\nThe imaginary component b:\n') b % The rectangular representation is therefore % % a + bj. z = a + 1j*b; fprintf('\nThe rectangular representation:\n') z % Thus, the mechanical conversion from polar to % rectangular form is % % a = r*cos(theta) % % b = r*sin(theta) % % followed by % % z = a + bj. % % Equivalently, we can write this directly as % % z = r*cos(theta) + j*r*sin(theta) % % = r*(cos(theta) + j*sin(theta)). z_trig = r*(cos(theta) + 1j*sin(theta)); fprintf('\nUsing the trigonometric form:\n') z_trig % Recall Euler's formula: % % exp(j*theta) % % = cos(theta) + j*sin(theta). % % Therefore, % % r*(cos(theta) + j*sin(theta)) % % = r*exp(j*theta). % % Consequently, our notation % % r phase theta % % represents exactly the same complex number as % % r*exp(j*theta). % % Thus, % % r phase theta % % = r*(cos(theta) + j*sin(theta)) % % = r*exp(j*theta). z_exp = r*exp(1j*theta); fprintf('\nUsing r*exp(j*theta):\n') z_exp % The rectangular, trigonometric, exponential, % and phase representations all describe the % same complex number: % % a + bj % % r*(cos(theta) + j*sin(theta)) % % r*exp(j*theta) % % r phase theta. % Let us verify that converting from rectangular % to polar and then back to rectangular returns % the original complex number. z = -2.5 + 6.3j; fprintf('\nOriginal rectangular value:\n') z % Rectangular to polar: r = abs(z); theta = angle(z); fprintf('\nMagnitude:\n') r fprintf('\nArgument in radians:\n') theta fprintf('\nArgument in degrees:\n') rad2deg(theta) % Polar back to rectangular: a = r*cos(theta); b = r*sin(theta); z_reconstructed = a + 1j*b; fprintf('\nReconstructed rectangular value:\n') z_reconstructed % We can also reconstruct the number directly % from the exponential representation. z_reconstructed_exp = r*exp(1j*theta); fprintf('\nReconstructed using r*exp(j*theta):\n') z_reconstructed_exp % Apart from possible floating-point rounding, % both reconstructed values equal the original % complex number. % There is one important special case. % % If % % z = 0, % % then % % r = |z| = 0. % % However, the argument of zero is not % mathematically defined. % % MATLAB returns fprintf('\nangle(0):\n') angle(0) % as a computational convention, but % mathematically % % arg(0) % % is undefined because the zero vector has no % direction. % Finally, remember that arguments are not % unique. % % If theta is an argument of a nonzero complex % number, then % % theta + 2*k*pi % % is also an argument for every integer k. % % This follows because % % exp(j*(theta + 2*k*pi)) % % = exp(j*theta)*exp(j*2*k*pi) % % = exp(j*theta). % % MATLAB's angle(z) returns the principal % argument. % % Thus, the central conversion formulas are: % % RECTANGULAR TO POLAR % % z = a + bj % % __________ % / 2 2 % r = |z| = / a + b % V % % theta = atan2(b, a) % % or, directly in MATLAB, % % r = abs(z) % theta = angle(z) % % % POLAR TO RECTANGULAR % % z = r phase theta % % a = r*cos(theta) % b = r*sin(theta) % % z = a + bj % % = r*(cos(theta) + j*sin(theta)) % % = r*exp(j*theta).