I would like to compute the derivative of a complex-valued function (Holomorphic function) numerically in MATLAB.
I have computed the function in a grid on the complex plane, and I've tried to compute the derivative using the Cauchy–Riemann relations.
Given: u = real(f), v = imag(f), x = real(point), y = imag(point)
The derivative should be given by: f' = du/dx + i dv/dx = dv/dy - i du/dy
where 'd' is the derivative operator.
I've tried the following code:
stepx = 0.01;
stepy = 0.01;
Nx = 2/stepx +1;
Ny = 2/stepy +1;
[re,im] = meshgrid([-1:stepx:1], [-1:stepy:1]);
cplx = re + 1i*im;
z = cplx.^3;
The derivative should be given by:
f1 = diff(real(z),1,2)/stepx +1i* diff(imag(z),1,2)/stepx;
or
f2 = diff(imag(z),1,1)/stepy - 1i* diff(real(z),1,1)/stepy;
But the two derivatives, which are suppose to be equal, do not match.
What am I doing wrong?
Let's count the number of elements which differs for less than stepx (assuming stepx = stepy):
lm = min(size(f1));
A = f1(1:lm,1:lm);
B = f2(1:lm,1:lm);
sum(sum(abs(A - B) <= stepx))
and using the fix proposed by @A. Donda
f1i = interp1(1 : Ny, f1, 1.5 : Ny);
f2i = interp1(1 : Nx, f2 .', 1.5 : Nx) .';
sum(sum(abs(f1i - f2i) <= stepx))
In the second case they all differ for less than stepx as it should be, while in the first case it's not true.