I would like to calculate the derivative of a complex-valued function (holomorphic function) numerically in MATLAB.
I calculated the function in the grid on the complex plane, and I tried to calculate 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 as follows: f '= du / dx + i dv / dx = dv / dy - i du / dy
where 'd' is a derived operator.
I 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 must be defined as follows:
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 supposed to be equal, do not match.
What am I doing wrong?
Let the number of elements be calculated that differs less than stepx (provided that 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 suggested 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))
, stepx, , .