% The exponential and trigonometric functions % in MATLAB also accept complex arguments. % % One of the most important relationships % involving complex numbers is Euler's formula: % % exp(beta*j) = cos(beta) + j*sin(beta), % % where beta is a real number measured in % radians. % % In MATLAB and Octave, we can calculate this % directly. beta = pi/3; fprintf('\nexp(beta*j):\n') exp(beta*1j) fprintf('\ncos(beta) + j*sin(beta):\n') cos(beta) + 1j*sin(beta) % Apart from possible floating-point rounding, % these two values are the same. % % Notice that the magnitude of % % exp(beta*j) % % is always 1. Changing beta simply changes its % argument. % % Thus, as beta varies, % % exp(beta*j) % % moves around the unit circle. % Euler's formula also allows us to express the % trigonometric functions using exponential % functions. % % Starting with % % exp(beta*j) = cos(beta) + j*sin(beta), % % take the complex conjugate of both sides: % % conj(exp(beta*j)) % % = cos(beta) - j*sin(beta). % % Since % % conj(exp(beta*j)) = exp(-beta*j), % % we have % % exp(-beta*j) = cos(beta) - j*sin(beta). % Adding these two equations gives % % exp(beta*j) + exp(-beta*j) % % = 2*cos(beta). % % Therefore, % % exp(beta*j) + exp(-beta*j) % cos(beta) = -----------------------------. % 2 fprintf('\ncos(beta):\n') cos(beta) fprintf('\nCalculated using exponentials:\n') (exp(beta*1j) + exp(-beta*1j))/2 % Subtracting the two equations instead gives % % exp(beta*j) - exp(-beta*j) % % = 2*j*sin(beta). % % Therefore, % % exp(beta*j) - exp(-beta*j) % sin(beta) = -----------------------------. % 2*j fprintf('\nsin(beta):\n') sin(beta) fprintf('\nCalculated using exponentials:\n') (exp(beta*1j) - exp(-beta*1j))/(2j) % These identities are often written as % % exp(j*beta) + exp(-j*beta) % cos(beta) = ----------------------------- % 2 % % and % % exp(j*beta) - exp(-j*beta) % sin(beta) = -----------------------------. % 2*j % % These are not merely computational tricks. % They show that the exponential and % trigonometric functions are closely connected % through complex numbers. % MATLAB also allows the arguments of the % trigonometric functions themselves to be % complex. % % For example, z = 0.4 + 0.7j; fprintf('\nA complex value z:\n') z fprintf('\nexp(z):\n') exp(z) fprintf('\ncos(z):\n') cos(z) fprintf('\nsin(z):\n') sin(z) % The exponential definitions of sine and % cosine continue to work even when z itself is % complex: % % exp(j*z) + exp(-j*z) % cos(z) = --------------------- % 2 % % and % % exp(j*z) - exp(-j*z) % sin(z) = ---------------------. % 2*j fprintf('\ncos(z) using exponentials:\n') (exp(1j*z) + exp(-1j*z))/2 fprintf('\nsin(z) using exponentials:\n') (exp(1j*z) - exp(-1j*z))/(2j) % The exponential function is also closely % related to powers and logarithms. % % Recall that, for positive real numbers, % % w % z = exp(w*ln(z)). % % In MATLAB, as in most programming languages, % the function % % log(...) % % means the NATURAL LOGARITHM. % % That is, % % log(z) % % means % % ln(z), % % the logarithm with base e. % % It does NOT mean the base-10 logarithm. % % In MATLAB, the base-10 logarithm is explicitly % written % % log10(...). % % Thus, fprintf('\nNatural logarithm of 10:\n') log(10) fprintf('\nBase-10 logarithm of 10:\n') log10(10) % MATLAB extends the natural logarithm to % complex numbers. % % If % % z = r*exp(j*theta), % % then a complex logarithm of z is % % log(z) = log(r) + j*theta. % % Since an argument is not unique, however, % % theta + 2*k*pi % % represents the same direction for every % integer k. % % Consequently, the complex logarithm is % naturally multivalued: % % log(r) + j*(theta + 2*k*pi). % % MATLAB's log(z) returns the PRINCIPAL value, % using the principal argument of z. z = 2 + 3j; fprintf('\nA complex value z:\n') z fprintf('\nThe principal value of log(z):\n') log(z) % We can reconstruct z by exponentiating its % logarithm. fprintf('\nexp(log(z)):\n') exp(log(z)) % This gives us a very powerful way to define % complex powers. % % For complex numbers z and w, with z nonzero, % we can calculate a principal value of % % w % z % % using % % w % z = exp(log(z)*w). % % Here, log(z) is the principal complex % logarithm returned by MATLAB. z = 2 + 3j; w = 0.4 - 0.7j; fprintf('\nz:\n') z fprintf('\nw:\n') w fprintf('\nz^w calculated directly:\n') z^w fprintf('\nz^w calculated using exp and log:\n') exp(log(z)*w) % Apart from possible floating-point rounding, % these calculations give the same principal % value. % % Thus, the exponential and logarithm allow us % to make sense of powers in which both the % base and the exponent may be complex: % % w % z = exp(w*log(z)). % % This is an important extension of the familiar % rules for real exponentials and logarithms. % There is one important mathematical detail. % % Because a nonzero complex number has % infinitely many possible arguments, % % theta + 2*k*pi, % % it also has infinitely many possible % logarithms: % % log(|z|) + j*(theta + 2*k*pi). % % If these different logarithms are used in % % exp(w*log(z)), % % they may produce different values when w is % not an integer. % % Programming languages must choose one value % when evaluating functions such as % % log(z) % % and % % z^w. % % MATLAB uses the principal complex logarithm % to select the principal value. % % Thus, when working with complex numbers, it % is important to distinguish between the % mathematical multivalued logarithm or power % and the single principal value returned by a % programming language.