MATLAB Digest - March 2004
Calculating Complex Integrals Using MATLAB
by Tim Farajian
Although anyone who has taken a calculus course is familiar with integrals,
many people are not familiar with the concept of complex integrals. This
article explains one part of the concept of complex integrals, and shows
how the MATLAB integration function quad, although not its intended use,
can be used to calculate some of these. We also consider the int function
provided by the Symbolic Math Toolbox and its ability to evaluate integrals
of complex functions.
In addition, the class of non-analytic functions, which the quad
and int functions are not designed to integrate, is considered. For this
class of functions, the method of parameterization that can evaluate
their integrals is introduced.
Real Integrals
The int and quad functions are two MATLAB integration functions. The quad function numerically calculates the definite integral of a function of one
variable. The int function symbolically calculates the definite and indefinite
integrals of a function over one of its variables.
Example 1
We can numerically calculate the definite integral
r = quad('3*sin(x/2)',0,1)
r =
0.7345
Example 2
We can also symbolically calculate the indefinite integral
![]()
r = int('a*sin(b*x)','x','x1','x2')
r =
-a*(cos(b*x2)-cos(b*x1))/b
Complex Integrals
If we consider z to
be a complex variable (a variable that can have complex values), then
the function f(z) is considered a complex function. The
integral of a complex function is referred to as a complex integral,
which is shown using the following notation
![]()
where C is the path, on the complex plane, over which the integral is to be calculated. When this path is a closed curve, this notation is given as
For a real integral, as in Example 1, the real variable x can have any value on the real axis between the two limits of integration. Therefore, we can say this integral is calculated over the straight line path from 0 to 1 on the real axis.

Figure 1
By definition, a complex variable z can be any value on the complex plane. Thus, when we discuss integrals of complex functions, we must define the path C on the complex plane over which this integral is to be performed. This complicates the process of evaluating complex integrals.
Analytic Functions
Consider the use of the quad function with complex values for the limits
of integration. This computes the integral over a straight line path between
the two specified endpoints.
Example 3
Calculate the complex integral
where C1 is the straight line path from -1 + i to 2 - i.

Figure 2
Solution
The quad function is capable of performing this operation.
F1 = quad('sin(z)', -1+i, 2-i) %Calculate integral
F1 =
1.4759 - 0.0797i
We can compute the integral over C3 in Figure 2 by summing two straight line integrals.
f = 'sin(z)'; %Define function
F2 = quad(f, -1+i, 2+i) + quad(f, 2+i, 2-i) %Calculate integral
F2 =
1.4759 - 0.0797i
We find that F2 is the same as F1 for at least 4 decimal places. It can
be shown that the integral of this function between these two endpoints will
theoretically be the same regardless of whether the path is a straight line,
a curved line, or a series of connected lines. Therefore, quad can be used
to evaluate this integral over the path C2 in Figure 2 by only
specifying its endpoints, as was done for F1.
This is a consequence of the fact that this function is analytic1 on the entire complex plane. A function f(z) is analytic over a domain R if f'(z) exists at every point within R.
Assume a function f(z) is analytic over a domain R, which is simply connected1 (has no holes). Also, assume C1 and C2 are any two arbitrary paths between the point Z1 and Z2 within R, then
![]()
This is referred to as path independence1.
Example 4
Calculate the integral of the complex function
![]()
over the paths C1, C2, and C3 in Figure 3.

Figure 3
Solution
The function f(z) is
differentiable at all points on the complex plane, except at the singular
point
1 + 2i. Therefore, f(z) is
analytic within any domain that does not contain 1 + 2i.
f = '(z + 1)./(z - 1 - 2i)'; %Define function
F1 = quad(f, 0, 2) + quad(f, 2, 2+4i) %Compute integral
F1 =
-4.2832 +10.2832i
We find that the integrals over C1 and C2 are equal for at least 4 decimal places.
F2 = quad(f, 0, 1.5+i) + quad(f, 1.5+i, 2+4i) %Compute integral
F2 =
-4.2832 +10.2832i
This is due to the fact that there is a simply connected domain that contains both 0 and 2 + 4i, but does not contain 1 + 2i.
However, we find the integral over C3 produces a different result.
F3 = quad(f, 0, 1+4i) + quad(f, 1+4i, 2+4i) %Compute integral
F3 =
8.2832 - 2.2832i
This is due to the fact that there is no simply connected domain that contains C1 and C2, that does not contain 1 + 2i.
The result of this is that the integral of f(z), between 0 and 2 + 4i, is purely dependent on whether the path traverses clockwise or counter-clockwise around 1 + 2i.
Note that the following command returns a warning and a result with NaN values.
F = quad(f, 0, 2+4i) %Compute integral
Warning: Divide by zero.
F =
NaN + NaNi
This is due to the fact that 1 + 2i lies on the straight line path between 0 and 2 + 4i, and f(1 + 2i) does not exist.
However, the int function is able to recognize this fact, and, for this
example, returns the value of the integral over a counter-clockwise path
around the singular point.
syms z %Define symbolic variable
f = (z+1)/(z-1-2*i); %Define symbolic function
F = int(f, 0, 2+4i); %Compute integral
disp(double(F)) %Display the double precision value of the result
-4.2832 +10.2832i
Nonanalytic Functions
There are many complex functions that are not analytic anywhere on the complex plane. Therefore, in general, the integrals of these functions, between any two specified endpoints, will be different depending on the path taken between them. For these situations, we need to be able to perform the integral over a specific path.
For example, consider the function
![]()
where
denotes the
conjugate of z. Since f(z) involves
, it is neither
differentiable nor analytic anywhere on the complex plane. Therefore, we
cannot assume the integral of f(z) over any path will be the same
as that over the straight line path, as computed by the quad function.
Although we can use quad to approximate a curved path using a series of
connected lines, this method is subject to a high degree of error. For these
situations, we must utilize the parametric representation of this
path.
Parametric Representations of the Path
Consider a curve C on
the complex plane. We may be able to parameterize this curve by introducing
a real parameter t such that every single point on C corresponds
to a unique value of
a < t < b, where the values t = a and t = b correspond
to the curve’s endpoints.
For example, a circle on the complex plane can be represented as C: z(x,y) = x + iy where
x2 + y2 = 1. This circle
can be parameterized by the substitutions x(t) = cos(t) and y(t) = sin(t), where t
is a real parameter. Therefore,
a path on this circle is defined by C: z(t) = x(t) + iy(t), where t goes from t0 to t1.
If t0 < t1, then the path
will be counter-clockwise along the circle.

Figure 4
Example 5
Find the parametric representation of the path C: z(x,y) = x + iy, where y2 - x + 1 = 0, as shown in Figure 4.

Figure 4
Solution
We can introduce the substitution y = r where -2 < r < 2. Therefore, from the
equation y2 - x + 1 = 0,
we find that x = r2 - 1. This results in the parametric
representation of the path C: z(t) = r2 + 1 + ir, where
-2 < r < 2.
|
|
, and 0 < t < 1. In this example, |
C: z(t) = 16t2 + (-16 + 4i)t + (5 - 2i)
We can generate the normalized parametric representation of this path using
syms t y %Define symbolic variable
z0 = 5-2i; z1 = 5+2i; %Define endpoints
f = y^2 + 1; %Define path
y = imag(z0 + t*(z1 - z0)); %Generate y(t)
x = subs(f, 'y', y); %Generate x(t)
z = simple(x + i*y) %Generate z(t)
z =
5-16*t+16*t^2-2*i+4*i*t
The PathRep function (available on MATLAB
Central) parameterizes a path
specified as a function of x or y.
PATHREP generates the parametric representation of a path on the complex
plane using the real parameter t over the range 0<=t<=1.
z = PathRep(zi, zf, f) where
zi is the initial point of the path.
zf is the final point of the path.
f is the path given either as y(x) or x(y). By default,
the path is assumed to be a straight line.
z is the parametric representation given as a function of t.
For this example, we can generate the parametric representation using
z = PathRep(5-2i, 5+2i, 'y^2 + 1') %Determine parametric representation
Integral Evaluation via Parametric Representations
Using the parametric representation z(t) of the path C, we can determine the
integral of a function
f[z(t)] over this path via
where
denotes the
derivative of z(t) with respect to t.
Example 6
| Evaluate | |
, where | |
and C is the path shown in Figure 6. Note that z - i |
Solution
We can use symbolic variables to determine the integrand.
syms t real %Define parameter
z = PathRep(5-2i, 5+2i, 'y^2 + 1'); %Determine parameterized path
f = conj(z)/(z-i); %Define function
Integrand = f*diff(z,t); %Determine integrand
Then, either quad or int can be used to evaluate the integral. The
quad function requires that the input function be given in vectorized form.
F = vectorize(char(Integrand)); %Create vectorized function
Fq = quad(F, 0, 1) %Numerically evaluate integral
Fq =
1.4023 - 2.4213i
Fi = int(Integrand, 't', 0, 1); %Symbolically evaluate integral
Fi = double(Fi) %Convert to double precision value
Fi =
1.4023 - 2.4213i
Therefore, as expected, we find these two functions both return the same results, accurate to at least 4 decimal places.
Conclusion
The quad and int functions are intended to be used to compute real integrals. However,
we can use these functions to directly compute some complex integrals over
straight line paths. The integrals of analytic functions are independent
of the path, therefore, the straight line integral computed by the quad function
is sufficient to represent the integral over any path between the same endpoints.
For non-analytic functions, we can parameterize the path, and use Equation
(1) to transform the complex integral into a real integral. The PathRep
function can be used to determine the parametric representation of the path. Then,
the quad or int functions can be used to compute this real integral.
Reference
1 Esfandiari, Ramin, Applied Mathematics for Engineers, 3rd Ed., Atlantis Publishing, 2003.Subscribe Now
The MATLAB Digest is the MathWorks electronic news bulletin for the MATLAB and Simulink community. To subscribe, become a Mathworks Account member.
Store