## Complex Numbers in Matlab and Octave

Matlab and Octave have the following primitives for complex numbers:octave:1> help j j is a built-in constant - Built-in Variable: I - Built-in Variable: J - Built-in Variable: i - Built-in Variable: j A pure imaginary number, defined as `sqrt (-1)'. The `I' and `J' forms are true constants, and cannot be modified. The `i' and `j' forms are like ordinary variables, and may be used for other purposes. However, unlike other variables, they once again assume their special predefined values if they are cleared *Note Status of Variables::. Additional help for built-in functions, operators, and variables is available in the on-line version of the manual. Use the command `help -i <topic>' to search the manual index. Help and information about Octave is also available on the WWW at http://www.octave.org and via the help-octave@bevo.che.wisc.edu mailing list. octave:2> sqrt(-1) ans = 0 + 1i octave:3> help real real is a built-in mapper function - Mapping Function: real (Z) Return the real part of Z. See also: imag and conj. ... octave:4> help imag imag is a built-in mapper function - Mapping Function: imag (Z) Return the imaginary part of Z as a real number. See also: real and conj. ... octave:5> help conj conj is a built-in mapper function - Mapping Function: conj (Z) Return the complex conjugate of Z, defined as `conj (Z)' = X - IY. See also: real and imag. ... octave:6> help abs abs is a built-in mapper function - Mapping Function: abs (Z) Compute the magnitude of Z, defined as |Z| = `sqrt (x^2 + y^2)'. For example, abs (3 + 4i) => 5 ...

octave:7> help angle angle is a built-in mapper function - Mapping Function: angle (Z) See arg. ...Note how helpful the ``See also'' information is in Octave (and similarly in Matlab).

### Complex Number Manipulation

Let's run through a few elementary manipulations of complex numbers in Matlab:>> x = 1; >> y = 2; >> z = x + j * y z = 1 + 2i >> 1/z ans = 0.2 - 0.4i >> z^2 ans = -3 + 4i >> conj(z) ans = 1 - 2i >> z*conj(z) ans = 5 >> abs(z)^2 ans = 5 >> norm(z)^2 ans = 5 >> angle(z) ans = 1.1071Now let's do polar form:

>> r = abs(z) r = 2.2361 >> theta = angle(z) theta = 1.1071Curiously, is not defined by default in Matlab (though it is in Octave). It can easily be computed in Matlab as

`e=exp(1)`

.
Below are some examples involving imaginary exponentials:
>> r * exp(j * theta) ans = 1 + 2i >> z z = 1 + 2i >> z/abs(z) ans = 0.4472 + 0.8944i >> exp(j*theta) ans = 0.4472 + 0.8944i >> z/conj(z) ans = -0.6 + 0.8i >> exp(2*j*theta) ans = -0.6 + 0.8i >> imag(log(z/abs(z))) ans = 1.1071 >> theta theta = 1.1071 >>Here are some manipulations involving two complex numbers:

>> x1 = 1; >> x2 = 2; >> y1 = 3; >> y2 = 4; >> z1 = x1 + j * y1; >> z2 = x2 + j * y2; >> z1 z1 = 1 + 3i >> z2 z2 = 2 + 4i >> z1*z2 ans = -10 +10i >> z1/z2 ans = 0.7 + 0.1iAnother thing to note about matlab syntax is that the transpose operator

`'`(for vectors and matrices)

*conjugates*as well as transposes. Use

`.'`to transpose without conjugation:

>>x = [1:4]*j x = 0 + 1i 0 + 2i 0 + 3i 0 + 4i >> x' ans = 0 - 1i 0 - 2i 0 - 3i 0 - 4i >> x.' ans = 0 + 1i 0 + 2i 0 + 3i 0 + 4i

**Next Section:**

Factoring Polynomials in Matlab

**Previous Section:**

Solving Linear Equations Using Matrices