In MATLAB letters i and j denote the imaginary unit and both may be used to write complex
numbers. The complex number can be written entering
z=1+3i
z=1+3j
Sometimes you need to write ∗ between the imaginary part and i. For instance, to write the
number , if you enter
z=1+sqrt(3)i
MATLAB sends an error message. You must write
z=1+sqrt(3)*i
and the answer is
z=
1.0000 + 1.7321i
You can work in symbolic way writing:
z=sym((1+sqrt(3)*i))
Then you get
z=
3^(1/2)*i + 1
MATLAB can work with complex numbers as easily as with real numbers.
Example
Write in the form the following complex numbers:
z1=(3+5i)*(4-i)
z2=(3-i)/(4+5i)
z3=(1+sqrt(3)*i)^3
Some useful commands to work with complex numbers are:
• real(z) real part of z.
• imag(z) imaginary part of z.
• abs(z) modulus of z.
• conj(z) conjugate of z.
• angle(z) single argument of z in . If z then the value is 0
Example
Find the real part, imaginary part, modulus, conjugate and an argument of
.
z = (3+5i)/(1-2i)
md = abs(z)
arg = angle(z) %Argument written in radians in ]-pi,pi]
arg_dg = angle(z)*180/pi %Argument in degrees
re = real(z)
im = imag(z)
w = conj(z)
Some useful commands to work with complex numbers are:
Example
Find the moduli and arguments of the elements of the matrix
A=[1+i sqrt(3)-i; -sqrt(2)-sqrt(2)*i -1]
abs(A)
angle(A) %Arguments in radians
angle(A)*180/pi %Arguments in degrees
Apart from angle, these commands can be used in symbolic calculus, but you need to
specify that x and y are real numbers. angle only works in numerical computation.
Example
Let be a complex number such that and . Find the real
and imaginary parts of .
syms x y real % x and y real variables
z=x+i*y;
w=(1+z)/z;
u=simplify(real(w));
pretty(u)
v=simplify(imag(w));
pretty(v)
Exercise
Given the complex number . Find
1. the real part
2. the imaginary part
3. the modulus
4. the principal argument
5. the conjugate
FUNCIONES ELEMENTALES
In MatLab we have these elementary functions,
• z^(1/n) Principal n-th root of z: if and 0 if .
• exp(z) Complex exponential function .
• log(z) Principal logarithm of z: , if and
if .
• sin(z) Complex sine function .
• asin(z) Complex sine function .
• cos(z) Complex cosine function .
• acos(z) Complex cosine function .
Remarks:
1. The expression only calculates the principal nth root. To find the set of
all the nth roots of z we need,
2. The MatLab function log only calculates the principal logarithm. The set of
all logarithms of a complex number w (that is, the solutions of the equation
) is determined by: where
represents the principal logarithm.
Example
Find the sixth roots of
Applying the remark 1 with the roots are:
k=[0:5];
z=(1+i)^(1/6)*exp(i*2*pi*k/6);
By raising the variable z to the sixth, we must obtain, except for rounding errors, the
complex :
z.^6
ans =
1.0000 + 1.0000i 1.0000 + 1.0000i 1.0000 + 1.0000i 1.0000 + 1.0000i 1.0000 + 1.0000i
1.0000 + 1.0000i
Remark: Rounding errors can be observed using format long.
Example
Find 5 different solutions of the equation ecuación .
According to the definition of a logarithm, the solutions of said equation are the complexes
z that make a logarithm of . Solving for z and using observation 2 to express
all logarithms of a complex, the solutions to the equation are of the form
with $k \in\Z$. To obtain 5 solutions, write in Matlab:
k=[0:4]; % 5 valores
z=3*i+log(1+2i)+k*2*pi*i
z=
0.8047 + 4.1071i 0.8047 +10.3903i 0.8047 +16.6735i 0.8047 +22.9567i 0.8047 +29.2399i
Remark: It is possible to solve the previous equation using the solve command. For
example, to obtain the solution based on the principal logarithm, that is, the one that
corresponds to , execute:
syms z
ecuacion=exp(z-3i)==1+2i;
ans =
log(1 + 2i) + 3i
To obtain all the solutions it would be necessary to specify it in the solve command as
follows:
[solucion,parametro,condiciones]=solve(ecuacion,z,'ReturnConditions',true
)
solucion =
log(1 + 2i) + pi*k*2i + 3i
parametro =
condiciones =
in(k, 'integer')
Exercise
Find 5 different solutions of the equation ecuación .
REPRESENTANDO NUMEROS COMPLEJOS
To draw complex number we have the following commands
• plot(z) draw z.
• plot(z,s) draw z where s is a string to specify the line type, marker
symbol, and color.
• polar(a,r,s) a is an argument and r the modulus.
• compass(a,b,s) draw vector (a,b) with origin at (0,0).
• compass(z) the same as compass(real(z),imag(z)).
• quiver(x,y,u,v,l) draw vector (u,v)with origin at (x,y). l is opcional.
Example
Draw the complex numbers 1, and using plot and compass.
z=[1 1+i -1+3i]
plot(z)
plot(z,'*b')
axis([-2,2,-1,4])
compass(z)
Example
In a window, draw in blue the triangle T whose vertexes are , and . Then,
add in red the triangle obtained by turning T an angle of around the origin. Finally, add in
green the triangle obtained when T is translated by the vector .
z=[0 1+i -1+3i];
z=[z 0]; % 0 added close the triangle
plot(z,'b')
hold on
plot(i*z,'r')
plot(z-i,'g')
hold off
Example
Find the vertexes of a regular pentagon with its centre at the origin and a vertex at .
Plot the polygon.
z=1+i;
clear v
v(1)=z;
for k=2:5
v(k)=v(k-1)*exp(i*2*pi/5);
end
disp(v)
plot([v,v(1)])
axis equal
Example
In a file named nroots.m write a function v=nroots(z,n) to find
where θ is an argument of z, n is a natural number, z is a complex number written is the
form and v is the vector containing the n-th roots of z.
function y=nroots(z,n)
% Find n-th roots of a complex number z
% Inputs:
% z complex number in the form a+ib
% n positive integer
% Outputs:
% y n-th roots of z
%
% Example: y=nroots(1+i,2)
%---------------------------------
if floor(n)~=n || n<=0
error('n has to be a positive integer')
end
%
r=(abs(z))^(1/n);
k=0:1:n-1;
theta=(angle(z)+2*k*pi)/n;
y=r*(cos(theta)+i*sin(theta));%Also y=r*exp(i*theta)
Example
By using compass plot in blue color and in red color in the same figure.
z=1+i;
w=nroots(z,5)
compass(z,'b')
hold on
compass(w,'r')
hold off
You can also use the command solve to find roots of complex numbers. For instance,
by writing s=solve(z^5-1-i,z) Matlab returns the fifth roots of , .
Example
Find the vertexes of a regular hexagon centred at with a vertex at . Plot the
polygon.
v1=2+3*i;% vertex
c=1+2*i;% centre
n=6;% number of points
w=c + nroots((v1-c)^6,6);
plot([w,w(1)])
axis equal
hold on
plot(c,'*r')
axis equal
Exercise
Find the vertexes of a regular heptagon centred at with a vertex at . Plot the
polygon.
1. Find the vertices
2. Plot the center in color red and vertices in color blue.
3. Plot the heptagon in color green.
REPRESENTANDO IMÁGENES DE CONJUNTOS
This section studies the graphical representation of transformations in the complex plane. It
will basically consist of the visualization of the original set (using a mesh of points) and its
transformed. To generate a mesh in $\mathbb{C}$ you can use the function:
• [U,V]=meshgrid(u,v) The input arguments are u and v
. The output argument U is a matrix of order whose
rows are equal to u. The second output argument, V, is another matrix of
the same order as U, whose columns match v.
From the mesh stored in U and V complex numbers can be obtained in binomial form:
z=U+i*V
or in polar form:
z=U.*exp(i*V)
according to what is appropriate in each case. Let's see an example of both below:
Example
Graphically represent the sets:
• , using a mesh of .
• , using a mesh of .
1. The natural way to describe the set A is by its Cartesian coordinates:
, . We will use x and y in the notation instead of U and
V:
clear; close all; fig1 = figure('WindowState','maximized');
u=linspace(1,5,10); v=linspace(1,3,6);
[x,y]=meshgrid(u,v); % Generate 10x6 points on [1,5]x[1,3]
z=x+i*y; % We use Cartesian Coordinates
plot(z,'b.','MarkerSize',40); % We use larger blue dots. (Size 40)
axis equal; % We use the same scale in both axes.
axis([0,6,0,4]); % Area to view: [0,6]x[0,4]
2. The natural way to describe the set B is by its polar coordinates: ,
. We will use r and t instead of U and V in the notation:
clear; close all; fig1 = figure('WindowState','maximized');
u=linspace(1,5,8);v=linspace(pi/4,3*pi/4,10);
[r,t]=meshgrid(u,v); % Generate 8x10 points on [1,5]x[pi/4,3*pi/4]
z=r.*exp(i*t); % We use Polar Coordinates
plot(z,'b.','MarkerSize',40); % We use larger blue dots. (Size 40)
axis equal; % We use the same scale in both axes.
axis([-5, 5, 0, 6]); % Area to view: [-5, 5]x[0, 6]
To visualize a transformation it would be enough to generate a mesh w, an image
of that of z and represent both meshes. For example:
Example
• Represent in a figure with two subwindows the set A of the previous
example and its transform by the translation .
• Represent in a figure with two subwindows the set B of the previous
example and its transformation using the function .
1. We first generate the original mesh and the transformed one:
u=linspace(1,5,10); v=linspace(1,3,6);
[x,y]=meshgrid(u,v);%genera 10x6 puntos sobre [1,5]x[1,3]
z=x+i*y; % Usamos coordenadas CARTESIANAS
w=z+1-i; % malla TRANSFORMADA
And we represent graphically with two subwindows:
subplot(1,2,1) % primera subventana
plot(z,'b*')
axis equal
axis([0,6,0,4])
subplot(1,2,2) % segunda subventana
plot(w,'r.');
axis equal
axis([0,6,0,4])
2. We first generate the original mesh and the transformed one:
clear; close all; fig1 = figure('WindowState','maximized');
u=linspace(1,5,8);v=linspace(pi/4,3*pi/4,10);
[r,t]=meshgrid(u,v); % Generate 8x10 points on [1,5]x[pi/4,3*pi/4]
z=r.*exp(i*t); % We use Polar Coordinates
w=z.^2; % Image of the mesh
And we represent graphically with two subwindows:
subplot(1,2,1); % First subwindow
plot(z,'b.','MarkerSize',40); % We use larger blue dots. (Size 40)
axis equal; % We use the same scale in both axes.
axis([-5, 5, 0, 6]); % Area to view: [-5, 5]x[0, 6]
subplot(1,2,2); % Second subwindow
plot(w,'r.','MarkerSize',40); % We use larger red dots. (Size 40)
axis equal; % We use the same scale in both axes.
axis([-30,0,-30,30]); % Area to view: [-30,0]x[-30,30]
Exercise
Represent the image of the set with the
function . Generate a window with two subwindows:
1. In the first window plot the set C using blue points of size 50.
2. In the second windows plot the set using red points of size 40.
DERIVADA COMPLEJA
The definition of the complex derivative is a generatization of the real to complex numbers
Definition.
Consider , a an interior point of A and . f is differentiable at a if
exists. This number is called derivative of f at the point a and usually is
written as or .
The main rules of the real differentation are verified for complex differentation, but in this
case we have this special property.
Cauchy-Riemann Equations
Given consider and . The function f is
differentiable at if, and only if, u and v are differentiable at and verify the
equations
In addition,
Example
Given the complex function ,
• using the real variables x and y, , find the function
and
• Check the Cauchy-Riemann equations.
clear; syms z; syms x y real;
f(z) = z^3; h(x,y) = expand(f(x+i*y));
u(x,y) = real(h(x,y));
v(x,y) = imag(h(x,y));
u_x = diff(u,x);
u_y = diff(u,y);
v_x = diff(v,x);
v_y = diff(v,y);
disp(simplify(u_x == v_y))
disp(simplify(u_y == -v_x))
Limits
As in real variable, the calculation of limits can be done with the limit command.
• limit(f,z,z0) Find (z symbolic variable, f symbolic function
of z and z0 a symbolic variable or a constant).
Differentation
As in real variable, the calculation of derivatives can be done with the diff command.
• diff(f,z) Find (z symbolic variable and f symbolic function of
z).
Integratation
As in real variable, the calculation of integrals can be done with the int command.
• int(f,z) Find (z symbolic variable and f symbolic function of
z).
Example
Given the function
,
• Check the Cauchy-Riemann equations.
• Find the complex derivative of h (interpretating as a complex function)
• Find the function f(z) such that h(x,y) = (real(f(x+iy)),imag(f(x+iy)))
% Cauchy-Riemann
clear; syms x y real;
h(x,y) = [x^3 - 3*x^2*y - 3*x*y^2 + 2*x*y + y^3, x^3 + 3*x^2*y - x^2 -
3*x*y^2 - y^3 + y^2]
hf = h(x,y);
u(x,y) = hf(1);
v(x,y) = hf(2);
u_x = diff(u, x);
u_y = diff(u, y);
v_x = diff(v, x);
v_y = diff(v, y);
disp(simplify(u_x == v_y))
disp(simplify(u_y == -v_x))
% Derivative
df = u_x+i*v_x
syms z zb;
% The complex function
dfz(z) = simplify(subs(df(x,y),[x, y],[(z+zb)/2,(z-zb)/(2*i)]))
% The function is the integral plus a constant C, but the constant is C =
h(0,0);
Cv = h(0,0); C = Cv(1)+i*Cv(2); % We must write C as a+bi form
f(z) = int(dfz(z), z) + C
Zero order.
In Matlab you can study the order of a zero using the instruction taylor. This command
calculates the Taylor polynomial:
which is formed by a finite number of addends of the power series of a function. From this
polynomial, the order of a zero is easily calculated.
The operation of this instruction is described below.
• taylor(f,z,z0,'Order',n) Find Taylor polynomial of degree of
the function in powers of (n natural number, z symbolic
variable, f symbolic function of z and z0 a symbolic variable or a
constant).
• taylor(f,z,z0) By default .
• taylor(f,z) By default and .
Example
Given the function ,
• find the zero order at the point .
• find the order of the zero.
clear; syms z; syms x y real;
f(z) = sin(z) - z + z^3/6;
T(z) = taylor(f,z,0,'Order',8)
pn = sym2poly(T(z));
m = length(pn) - find(pn,1,'last')
Exercise
Given the function
,
• Check the Cauchy-Riemann equations.
• Find the complex derivative of h (interpretating as a complex function)
• Find the function f(z) such that h(x,y) = (real(f(x+iy)),imag(f(x+iy)))