# Complex Line Integrals

This example shows how to calculate complex line integrals using the `'Waypoints'`

option of the `integral`

function. In MATLAB®, you use the `'Waypoints'`

option to define a sequence of straight line paths from the first limit of integration to the first waypoint, from the first waypoint to the second, and so forth, and finally from the last waypoint to the second limit of integration.

### Define the Integrand with an Anonymous Function

Integrate

$${\oint}_{C}\frac{{e}^{z}}{z}\phantom{\rule{0.2222222222222222em}{0ex}}dz$$

where $$C$$ is a closed contour that encloses the simple pole of $${e}^{z}/z$$ at the origin.

Define the integrand with an anonymous function.

fun = @(z) exp(z)./z;

### Integrate Without Using Waypoints

You can evaluate contour integrals of complex-valued functions with a parameterization. In general, a contour is specified, and then differentiated and used to parameterize the original integrand. In this case, specify the contour as the unit circle, but in all cases, the result is independent of the contour chosen.

g = @(theta) cos(theta) + 1i*sin(theta); gprime = @(theta) -sin(theta) + 1i*cos(theta); q1 = integral(@(t) fun(g(t)).*gprime(t),0,2*pi)

q1 = -0.0000 + 6.2832i

This method of parameterizing, although reliable, can be difficult and time consuming since a derivative must be calculated before the integration is performed. Even for simple functions, you need to write several lines of code to obtain the correct result. Since the result is the same with any closed contour that encloses the pole (in this case, the origin), instead you can use the `'Waypoints'`

option of `integral`

to construct a square or triangular path that encloses the pole.

### Integrate Along a Contour That Encloses No Poles

If any limit of integration or element of the waypoints vector is complex, then `integral`

performs the integration over a sequence of straight line paths in the complex plane. The natural direction around a contour is counterclockwise; specifying a clockwise contour is akin to multiplying by `-1`

. Specify the contour in such a way that it encloses a single functional singularity. If you specify a contour that encloses no poles, then Cauchy's integral theorem guarantees that the value of the closed-loop integral is zero.

To see this, integrate `fun`

around a square contour away from the origin. Use equal limits of integration to form a closed contour.

```
C = [2+i 2+2i 1+2i];
q = integral(fun,1+i,1+i,'Waypoints',C)
```

q = -3.6082e-16 + 6.6613e-16i

The result is on the order of `eps`

and effectively zero.

### Integrate Along a Contour with a Pole in the Interior

Specify a square contour that completely encloses the pole at the origin, and then integrate.

```
C = [1+i -1+i -1-i 1-i];
q2 = integral(fun,1,1,'Waypoints',C)
```

q2 = 0.0000 + 6.2832i

This result agrees with the `q1`

calculated above, but uses much simpler code.

The exact answer for this problem is $$2\pi i$$.

2*pi*i

ans = 0.0000 + 6.2832i