Scilab can handle complex numbers and operations with complex numbers. In this tutorial we are going to explain:

- how to define complex numbers in Scilab
- how to perform mathematical operations with complex numbers
- what is the complex conjugate
- which are the predefined Scilab functions for complex numbers
- how to plot complex numbers in Scilab, using the Re-Im plane

For a better understanding of complex numbers read the following articles:

### Define complex numbers in Scilab

There are several ways of defining complex numbers in Scilab. Let’s assume that we have the following complex numbers:

\[ \begin{split}z_{1} &= 2 + i\\

z_{2} &= 1 + 2 \cdot i

\end{split} \]

First method uses the special variable `%i`

, which is predefined in Scilab for complex numbers. We will define the complex numbers using the Scilab console:

--> z1=2+%i z1 = 2. + i --> z2=1+2*%i z2 = 1. + 2.i

Another method is to use the predefined Scilab function `complex()`

. The function expects two arguments, the real part and imaginary part of the complex number. If we use the `complex()`

function to define our *z _{1}* and

*z*complex numbers, we’ll get:

_{2}--> z1=complex(2,1) z1 = 2. + i --> z2=complex(1,2) z2 = 1. + 2.i

If we only input one argument in the `complex()`

function, the imaginary part will be set as `0`

.

### Mathematical operations with complex numbers in Scilab

Now that we have the complex numbers defined, we can perform various mathematical operations: addition, subtraction, multiplication and division.

--> z1+z2 ans = 3. + 3.i --> z1-z2 ans = 1. - i --> z1*z2 ans = 5.i --> z1/z2 ans = 0.8 - 0.6i

We ca also use complex numbers as arguments for other functions such as: `sqrt()`

, `sin()`

, `cos()`

, etc.

--> sqrt(z1) ans = 1.4553467 + 0.3435607i --> sin(z1) ans = 1.4031193 - 0.4890563i --> cos(z1) ans = -0.6421481 - 1.0686074i

### Complex conjugate

The complex conjugate of a complex number is another complex number which has the real part equal in sign and magnitude and the imaginary part equal in magnitude but opposite in sign. For example, for the complex number *x+y·i*, the complex conjugate is *x-y·i*.

If we represent a complex number in the *Re-Im* plane, its complex conjugate will be mirrored around *Re* axis.

Scilab has the predefined function `conj()`

, which outputs the complex conjugate of a complex number input as argument. For example, we can find the complex conjugate of our *z _{1}* and

*z*number with:

_{2}--> conj(z1) ans = 2. - i --> conj(z2) ans = 1. - 2.i

If we multiply a complex number with its complex conjugate, the result will be a real number (or a complex number with the imaginary part 0). For a complex number and its complex conjugate:

\[ \begin{split}z &= x + y \cdot i\\

\overline{z} &= x – y \cdot i

\end{split} \]

their multiplication will always give:

\[z \cdot \overline{z} = x^2+y^2\]Let’s try this in Scilab:

--> z1*conj(z1) ans = 5. --> z2*conj(z2) ans = 5.

### Predefined Scilab functions for complex numbers

A summary and a short description of the Scilab function related to complex numbers is given below:

`complex()`

: creates a complex number`conj()`

: creates the complex conjugate of a complex number`imag()`

: extracts the imaginary part of complex numbers, polynomials, or rationals`imult()`

: multiplication by`i`

the imaginary unitary`isreal()`

: checks if a variable is stored as a complex number`polar()`

: return the polar form of a complex number`real()`

: extracts the real part of complex numbers, polynomials, or rationals

We already used `complex()`

and `conj()`

in the previous examples. Further we use `real()`

and `imag()`

, to exact the real and imaginary parts from a complex number:

--> real(z1) ans = 2. --> imag(z1) ans = 1.

The check if a certain variable is complex or not, we can use the `isreal()`

function:

--> isreal(z1) ans = F

The answer, as expected, is `False`

.

In the article Complex numbers representation, we have calculated the complex numbers *z _{1}* and

*z*in polar form:

_{2}z_{1}=2.24 \angle 0.4636\\

z_{2}=2.24 \angle 1.1071

\end{split} \]

The Scilab `polar()`

function returns two values, the angle and the radius. For example, if we apply it to the complex number z_{2}, we’ll get the same results as above:

--> [radius angle]=polar(z2) angle = 1.1071487 radius = 2.236068

The `2.24`

radius is the two decimals rounded version of `2.236068`

.

The function `imult()`

does the multiplication of the argument with the imaginary unit `%i`

. Using `imult()`

is a more efficient way to multiply a variable *x* by *i* than `y = %i*x`

, without the problems occurring when *x* comprises “special” floating point numbers as `%inf`

and `%nan`

.

--> imult(5) ans = 5.i --> imult(%inf) ans = Infi --> imult(%nan) ans = Nani

### Compute and plot complex numbers in Scilab

The following Scilab script:

- defines the complex numbers
*z*and_{1}*z*_{2} - performs mathematical operations
- plots the result in the Re-Im plane
- calculates the polar form of the complex numbers
- displays the real part, imaginary part, radius and angle in the Scilab console

clc() // Define complex numbers a1=2; b1=1; a2=1; b2=2; z1=complex(a1,b1); z2=complex(a2,b2); // Mathematical operation z3=z1+z2; a3=real(z3); b3=imag(z3); // Plot figure(0) clf() hf=gcf() hf.background = -2; ha=gca() ha.data_bounds = [-5,-5;5,5]; xgrid(); plot([0 a1],[0 b1],'b','LineWidth',3) plot([0 a2],[0 b2],'r','LineWidth',3) plot([0 a3],[0 b3],'g','LineWidth',3) xlabel('Real axis (Re)','FontSize',2) ylabel('Imaginary axis (Im)','FontSize',2) legend('$\Large{z_{1}}$','$\Large{z_{2}}$','$\Large{z_{3}}$') plot(0,0,'sk') plot(a1,b1,'sk') plot(a2,b2,'sk') plot(a3,b3,'sk') xstring(a1,b1,'$\Large{z_{1}=2+i}$') xstring(-a2,b2,'$\Large{z_{2}=1+2i}$') xstring(a3,b3,'$\Large{z_{3}=3+3i}$') title('x-engineer.org','FontSize',1) // Calculate polar form r1=sqrt(a1^2+b1^2); r2=sqrt(a2^2+b2^2); r3=sqrt(a3^2+b3^2); phi1=atan(b1/a1)*180/%pi; phi2=atan(b2/a2)*180/%pi; phi3=atan(b3/a3)*180/%pi; // Display polynomial and polar parameters mprintf('%s\t%s\t%s\t%s\n', 'a', 'b', 'r', 'phi') mprintf('%4.2f\t%4.2f\t%4.2f\t%4.2f\n', a1, b1, r1, phi1) mprintf('%4.2f\t%4.2f\t%4.2f\t%4.2f\n', a2, b2, r2, phi2) mprintf('%4.2f\t%4.2f\t%4.2f\t%4.2f', a3, b3, r3, phi3)

The default operation in the script is addition, but it can be run for subtraction, multiplication and division. After each run, to properly display the polynomial form, update the third `xstring()`

function with the values displayed in the Scilab console. Below are the plots for all four mathematical operations.