I'm trying to find complex line/path integrals over a few circular closed paths using the integral command. My code for the integral of 1/(z-i)^2 over the circle {z:|z|=2} traversed once anticlockwise is as follows:
fun = @(z) 1 ./((z-1i) .^ 2);;
g = @(t) 2 .*(cos(t) + 1i .* sin(t));
gprime = @(t) 2 .*(-sin(t) + 1i .* cos(t));
q1 = integral(@(t) fun(g(t)) .* gprime(t),0,2 .* pi)
(I would expect the answer to be 0 and matlab gives 6.6613*10^(-16)-4.4409*10^(-16)i).
My code for the integral of e^z/(z(z^2-9)) over the circle {z:|z-2|=3} traversed once anticlockwise is as follows:
fun = @(z) exp(z) ./(z .* (z.^2-9));
g = @(t) 2+3 .*(cos(t) + 1i .* sin(t));
gprime = @(t) 2+3 .*(-sin(t) + 1i .* cos(t));
q1 = integral(@(t) fun(g(t)) .* gprime(t),0,2 .* pi)
(I would expect the answer to be pi/9(e^3-2)i, but matlab gives 5.4351+6.3130i).
As can be seen above, my problem is that while the code gives accurate values when the circular path is centred at the origin, it fails otherwise; sometimes giving an accurate imaginary part but inaccurate real part or just a completely inaccurate answer.
Can anyone see what is going wrong?
I have answered to the first question in a comment.
For the second question, you have made an error in the computation of the derivative : additive constant 2 should disappear. In this way the result you will obtain is 6.3130i, in full compliance with the theoretical value.
fun = @(z) exp(z) ./(z .* (z.^2-9));
g = @(t) 2+3 .*(cos(t) + 1i .* sin(t));
gprime = @(t) 3 .*(-sin(t) + 1i .* cos(t));
q1 = integral(@(t) fun(g(t)) .* gprime(t),0,2 .* pi)
I take the opportunity to advise you, instead of the previous method, to compute complex integrals by "way points" method (see for example https://uk.mathworks.com/help/matlab/math/complex-line-integrals.html) ; here, it would be
C=[-2+i,-2-i,5-i,5+i];
integral(@(z) (exp(z) ./(z .* (z.^2-9))),1,1,'WayPoints',C)
where C is a square (more generaly any square) enclosing the desired poles and only them. (always take the second and third parameters to be 1,1)
Appendix : Rapid personal check of the theoretical value by residue theorem :
2i pi(Res(f,0)+Res(f,3))=2i pi(1/(-9)+e^3/(27-9)).