Complex Numbers are the combination of real numbers and imaginary numbers in the form of p+qi where p and q are the real numbers and i is the imaginary number. An imaginary number is defined where i is the result of an equation a^2=-1. We can use i or j to denote the imaginary units. As complex numbers are used in any mathematical calculations and Matlab is mainly used to perform mathematical calculations. So, complex numbers form an important part of learning Matlab.
Complex Numbers Generation in MATLAB
Complex Numbers can be created or declared in Matlab using a ‘complex’ function. We can also create complex numbers by finding the square root of any negative number. In Matlab, we can use i or j to denote the imaginary part of the complex number.
Here X is a complex number which contains 2 parts i.e. real and imaginary part. 4 is the real part and 5 is the imaginary part. We can find the real and imaginary parts using functions in Matlab.
- a= real(X) = 4 (This gives the real part of the complex number)
- b= imag(X)= 5 (This gives the imaginary part of the complex number)
- complex (6,7) = 6+7i (This function is used to create complex number)
We can also create complex arrays in Matlab which can also be declared using the complex functions.
- a = complex (x, y)
There are certain conditions for x and y that we should follow like x and y should not be single or double. A complex scalar can be created if two inputs are scalar in nature like,
- X= complex (5,3)
- X= 5.0000+3.0000i
Similarly, a complex vector can be created if we have two inputs as vectors.
- a = complex (X, Y)
4 + 3i
5 + 5i
6 + 1i
7 + 2i
We can create a complex number having only one scalar like,
- X = complex (10)
- X= 10.0000+0.0000i
There are certain conditions that the input and output arguments should follow like,
The input arguments contain real and imaginary parts like x any y. x and y should be scalar, vector, multi-dimensional array or matrix in MATLAB. x and y size should be the same. They should be of the same data type but there are few exceptions like double can be used with single and integer can be combined with a double which is scalar.
The output of the array can be vector, scalar, matrix or multidimensional array depending on the input arguments. The size of the output should be the same as the input. If the input arguments are of different data types than the output is determined by,
- If either of the input arguments is single in nature than the output should also be single.
- If either of the input arguments is an integer in nature, then the output should be of an integer data type.
We can check whether the matrix is real or imaginary by using isreal function.
X = [2+i,1]; Isreal(X)
Isreal (X (2))
To extract the real and imaginary parts, we can use real and imag functions in Matlab like,
Operations and Functions of Complex Numbers in MATLAB
There are several operations and functions that can be performed using complex numbers in Matlab like
- abs: This function is used to find the modulus of any complex number in the form of p+qi. abs(2+3i) = square root of [2^2+3^2] = (13) ^0.5
- angle: To find the phase angle of the complex number.
There are certain tips that should be followed for the proper functioning of complex numbers in Matlab like,
- We should avoid using i and j as part of any variable names as they are used in denoting the imaginary parts of the complex number.
- We should avoid using j or i if the imaginary part is 1. Instead, we can use 1j or 1i.
- We can create a complex function in Matlab when i and j are used as variable names in some part, input arguments are not of single or double type and the imaginary part is zero.