% Two complex numbers are added by adding their % real components and their imaginary components % separately. % % If % % w = a + bj % % and % % z = c + dj, % % then % % w + z = (a + c) + (b + d)j. % % Complex-number addition is commutative, just % like addition of real numbers: % % w + z = z + w. w = 5.3 + 3.2j z = 3.2 - 4.7j fprintf( '\nCalculations with ''w'' and ''z'':\n' ) w + z z + w % We can see how this addition is performed by % separately adding the real components and the % imaginary components. real( z ) + real( w ) imag( z ) + imag( w ) fprintf( 'Re(z) + Re(w) = %f\n', ... real( z ) + real( w ) ) fprintf( 'Im(z) + Im(w) = %f\n', ... imag( z ) + imag( w ) ) % The imaginary component is multiplied by j % when reconstructing the complex number. fprintf( 'j(Im(z) + Im(w)) = %fj\n\n', ... imag( z ) + imag( w ) ) % Putting the real and imaginary components % back together gives the sum of the two % complex numbers. fprintf( '(Re(w) + Re(z)) + j(Im(w) + Im(z)) = %f%+fj\n\n', ... real( z ) + real( w ), ... imag( z ) + imag( w ) ) % Thus, adding complex numbers is equivalent to % separately adding their real and imaginary % components: % % Re(w + z) = Re(w) + Re(z) % % and % % Im(w + z) = Im(w) + Im(z). % Now consider the following five complex % numbers. w1 = -0.217 + 8.326j w2 = -9.653 + 3.757j w3 = -1.592 + 5.909j w4 = 5.608 - 4.460j w5 = 0.928 + 0.350j % Based on the results below, what do you think % is the actual sum of these five complex % numbers? % % Mathematically, addition is associative: % % (a + b) + c = a + (b + c). % % Consequently, changing the order in which % additions are performed should not change % the result. % % On a computer, however, real numbers are % usually represented using floating-point % numbers. Most decimal fractions cannot be % represented exactly using the finite binary % representation used by the computer. % % Consequently, small rounding errors can be % introduced during each arithmetic operation. % Changing the order of the operations may % therefore change these small rounding errors. % % Thus, floating-point addition is not always % exactly associative, even though mathematical % addition is associative. % % Fortunately, these errors are generally very % small for calculations such as these. fprintf( '\nDifferent ways of calculating w1 + ... + w5:\n' ) % Display additional digits so that any small % differences caused by floating-point % arithmetic are visible. format long w1 + w2 + w3 + w4 + w5 (w1 + w2) + (w3 + (w4 + w5)) w1 + (w2 + ((w3 + w4) + w5)) (w1 + w2) + (w3 + w4) + w5