Complex numbers in Scilab

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 z1 and z2 complex numbers, we’ll get:

--> 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  =

--> 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.

Complex conjugate

Image: Complex conjugate

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 z1 and z2 number with:

--> 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  =

--> z2*conj(z2)

 ans  =

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  =

--> imag(z1)

 ans  =

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

--> isreal(z1)

 ans  =

The answer, as expected, is False.

In the article Complex numbers representation, we have calculated the complex numbers z1 and z2 in polar form:

\[ \begin{split}
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 z2, we’ll get the same results as above:

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

 angle  = 

 radius  = 

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  =

--> imult(%inf)

 ans  =

--> imult(%nan)

 ans  =

Compute and plot complex numbers in Scilab

The following Scilab script:

  • defines the complex numbers z1 and z2
  • 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

// Define complex numbers

// Mathematical operation

// Plot
hf.background = -2;
ha.data_bounds = [-5,-5;5,5];
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)

// Calculate polar form

// 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.

Complex numbers Cartesian representation - addition


Complex numbers Cartesian representation - subtraction


Complex numbers Cartesian representation - multiplication


Complex numbers Cartesian representation - division


Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!