Second derivative with complex numbers

02092021, 06:06 AM
(This post was last modified: 02092021 06:08 AM by peacecalc.)
Post: #1




Second derivative with complex numbers
Hello all,
I'm shure, in former threads has discussed that topic. After programming the first derivative with complex numbers in my DM42, I tried the same with the second derivative. For rememberance, the taylorseries for the first derivative (truncating after the first derivative): \[ f(x+i\cdot h) \approx f(x) + f'(\tau)_{\tau = x}\cdot i h \quad \hbox{ take the imaginary part with}\quad \Im(f(x)) = 0 \quad \hbox{ because it is only real, one gets:} \quad f'(x) \approx \frac{1}{h}\Im(f(x+ih)) \] Now let us see the taylor series until the second derivative (and then truncating further terms): \[ f(x+i\cdot h) \approx f(x) + f'(\tau)_{\tau = x}\cdot i h + \frac{1}{2} f''(\tau)_{\tau = x}\cdot (i h)^2 \quad \hbox{with} \quad i^2 =  1 \Rightarrow f(x+i\cdot h) \approx f(x) + f'(\tau)_{\tau = x}\cdot i h  \frac{1}{2} f''(\tau)_{\tau = x}\cdot h^2 \] now we take the real part: \[ \Re(f(x+i\cdot h)) \approx f(x)  \frac{1}{2} f''(\tau)_{\tau = x}\cdot h^2 \quad\hbox{the term:} \quad f'(\tau)_{\tau = x}\cdot i h \quad \hbox{vanish because it is only imagenary and one gets:} \quad f''(x) \approx  \frac{2}{h^2} \cdot \left( \Re(f(x+i\cdot h))  f(x) \right) \] Maybe it is an advantage, because you only have to calculate one difference instead of three if you calculate the first order derivative without doing that in the complex plane. Please pay attention to, only the real part is taken from f(x+ih). Sincerely peacecalc 

02092021, 03:45 PM
Post: #2




RE: Second derivative with complex numbers
Here is another way to obtain the derivative formulas
\(\displaystyle f'(x) ≈ {f(x+ih)  f(x) \over ih}\) \(\displaystyle f''(x) ≈ {f'(x)  f'(xih) \over ih} ≈ \frac{{{f(x+ih)  f(x) \over ih}}  {{f(x)  f(xih) \over ih}}}{ih} = {f(x+ih)  2 f(x) + f(xih) \over h^2} \) Weierstrass approximation theorem: we can assume f(x) is polynomial. Conjugate of polynomial is polynomial of conjugate: f(xhi) = conj(f(x+hi)) If f(x) is smooth and real, so does its derivatives → RHS must be real. \(\displaystyle f'(x) ≈ {\Im(f(x+ih)) \over h} \) \(\displaystyle f''(x) ≈ {\Re(f(x+ih)  f(x)) \over h^2/2} \)  Using central difference derivative formula, we also get the same f'(x) formula. Note: below does not assume real f'(x), RHS imaginary parts really cancelled out. \(\displaystyle f'(x) ≈ {f(x+ih)  f(xih) \over 2ih} = {\Im(f(x+ih))  \Im(f(xih)) \over 2h} = {\Im(f(x+ih)) \over h} \) → both derivative formulas should give accuracy similar to central difference formulas. XCas> f(x) := x*(x^3+5*x^221*x) // example from here XCas> f'(4) → 328 XCas> (f(x+h)  f(xh)) / (2h)  x=4, h=1e3 → 328.000021 XCas> im(f(x+h*i)) / h  x=4, h=1e3 → 327.999979 XCas> f''(4) → 270 XCas> (f(x+h)2*f(x)+f(xh))/h^2  x=4, h=1e3 → 270.000002132 XCas> re(f(x+h*i)f(x)) / (h^2/2)  x=4, h=1e3 → 269.999998125 

02112021, 09:49 AM
Post: #3




RE: Second derivative with complex numbers
Hello Albert,
thank you for your lucide comments about that topic. In deed I always thought that the way into the complex plane is an advantage in precision, but it seems not. What a pity! 

02112021, 12:36 PM
Post: #4




RE: Second derivative with complex numbers
It may avoid roundoff if you want high accuracy.
Try the above examples on a real 42S, with h=1e11. The complex first derivative gets 328 exactly, the difference function gets 300. Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE 

02112021, 02:13 PM
Post: #5




RE: Second derivative with complex numbers
(02112021 12:36 PM)Werner Wrote: It may avoid roundoff if you want high accuracy. Yes, but only with first derivative. (assumed im(f(x+hi)) does not hit with catastrophic cancellation) For second derivative, you still hit with subtraction cancellation errors. XCas> f(x) := x*(x^3+5*x^221*x) XCas> f1(x,h) := re((f(x+h)f(xh))/(2h)) // central difference 1st derivative XCas> f'(x) . [f1(x,h), f1(x,h*i)]  x=4, h=1e3 → [2.10000508787e05, 2.10000000607e05] XCas> f'(x) . [f1(x,h), f1(x,h*i)]  x=4, h=1e6 → [1.0320036381e08, 2.09752215596e11] XCas> f2(x,h) := re((f(x+h)2*f(x)+f(xh))/h^2) // central difference 2nd derivative XCas> f''(x) . [f2(x,h), f2(x,h*i)]  x=4, h=1e3 → [2.13206476474e06, 1.87539626495e06] XCas> f''(x) . [f2(x,h), f2(x,h*i)]  x=4, h=1e6 → [0.0221821205923, 0.0506038300227]  If h is not too small, we avoided catastrophic cancellation, error = O(h^2) We may take advantage of it, with h = ε*√i, which make h^2 purely imaginary. XCas> f'(x)  f1(x,h)  x=4, h=1e3*√i → 2.44426701101e12 XCas> f''(x)  f2(x,h)  x=4, h=1e3*√i → 4.78621586808e11 

02112021, 05:49 PM
Post: #6




RE: Second derivative with complex numbers
You never cease to amaze me, Albert!
One more for my files, complex derivatives with h^2 purely imaginary.. Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE 

02112021, 07:01 PM
(This post was last modified: 02122021 01:43 PM by Werner.)
Post: #7




RE: Second derivative with complex numbers
A Free42 program for both first and second derivative.
I took h=sqrt(i), that seems to work as well, and then the division by h^2 for the second derivative is no longer necessary ;) Update: whenever I think I'm being smart, it turns out I make things worse. So the program below uses h = sqrt(i)/1e3.. Update 2 : shorter Instructions: put the name of the function to derive in the ALPHA reg, Xvalue in X, press either F' or F" for first and second derivative, respectively. Code: 00 { 86Byte Prgm } With the polynomial above: Code: 00 { 19Byte Prgm } "FX" 4 XEQ "F'" > 328 (2.91e29) 4 XEQ "F"" > 270 (+2e28) Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE 

02112021, 08:53 PM
Post: #8




RE: Second derivative with complex numbers
I updated the routine above, because I was wrong..
Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE 

02122021, 04:55 PM
Post: #9




RE: Second derivative with complex numbers
(02112021 02:13 PM)Albert Chan Wrote: XCas> f2(x,h) := re((f(x+h)2*f(x)+f(xh))/h^2) // central difference 2nd derivative With h^2 purely imaginary, we can optimized away evaluation of f(x) For h = ε*√i, all formulas below should have error = O(ε^4) f(x) ≈ re((f(x+h) + f(xh))/2) f'(x) ≈ re((f(x+h)  f(xh))/(2h)) f''(x) ≈ re((f(x+h) + f(xh))/h^2) 

02132021, 09:00 AM
Post: #10




RE: Second derivative with complex numbers  
02132021, 02:50 PM
(This post was last modified: 02132021 03:15 PM by Werner.)
Post: #11




RE: Second derivative with complex numbers
That calls for a new rewrite, simpler again.
Another attempt at improvement, I take h = ε*√(2*i) = (ε,ε)  then both h and h^2 are exact (in a decimal calculator. This is the hpmuseum, after all) Code: 00 { 71Byte Prgm } Still, for F', I favour the f'(x) = Im(f(x+ih))/h formula, as it needs only one function evaluation. Then the routines become: Code: 00 { 77Byte Prgm } Cheers, Werner 41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)