# Finite differences of a complex function

22 views (last 30 days)
carlos g on 10 Jul 2020 at 12:04
Answered: Steven Lord on 10 Jul 2020 at 15:41
I have a function (a complex array of values uul(:)) for which I would like to compute its derivative. I simply use central finite differences
uulder=(uul(3:end)-uul(1:end-2))./deltaeta(2:end-1)'/2;
uulderabs=(abs(uul(3:end))-abs(uul(1:end-2)))./deltaeta(2:end-1)'/2;
I plot this:
>> plot(eta,abs(uul))
>> hold on
>> plot(eta(1:end-2),abs(uulder))
>> hold on
>> plot(eta(1:end-2),uulderabs)
It seems the red curve is incorrect while the yellow one is correct. However, I would need the vector whose absolute value gives the yellow curve (and not taking absolute values beforehand like with uulderabs). I need this because with the yellow curve I am losing the real and imaginary parts and I would like to be able to use them. What is it that I am doing wrong?

#### 1 Comment

Benjamin Bauer on 10 Jul 2020 at 14:56
What exactly makes you think the red curve is incorrect?
Note that the red curve's values are not supposed to match the tangent slopes of the blue one: If we say the values in uul are evaluated from a function then the blue curve shows and the red curve shows which is definitely not the same as (displayed by the yellow curve).
However, if you still want some graphical validation of your derivatives you should try to plot
absuulder = real(uul.*conj(uulder))./abs(uul)
which should match the yellow curve.

Steven Lord on 10 Jul 2020 at 15:41
For real numbers, the operators ' and .' give the same result.
xR = 1:10;
y1R = xR'
y2R = xR.'
isequal(y1R, y2R)
For complex numbers, they don't.
xC = complex(1:10, 10:-1:1);
y1C = xC'
y2C = xC.'
isequal(y1C, y2C)
See the documentation for either transpose or ctranspose for a brief explanation of the difference between the two.
I suspect your deltaeta is complex.