Mistake in simple automatic differentiation computation?

Metronome

Junior Member
Joined
Jun 12, 2018
Messages
153
Suppose we have an extremely simple program...

myFunction(x)
    \ \ \ \ myExpression = 2cos(2x) - sin(4x)
    \ \ \ \ return myExpression

...and want to apply automatic differentiation to it at the input x=π2x = \frac{\pi}{2}. Since there is only a single line of any substance, this is essentially equivalent to differentiating f(x)=2cos(2x)sin(4x)f(x) = 2\cos(2x) - \sin(4x). I have higher aspirations to compute programs of multiple lines and multiple variables, but I seem to be making a mistake even in the basic case.

A dual number function, as I understand it in a way that will generalize to the multivariate case (perhaps no longer called a dual number), is the sum of a function and its total differential, all evaluated at a particular point of which each coordinate is also a dual number. So what we want to find is...(f(x)+df)x=x0+dx(f(x) + df)|_{x = x_0 + dx}...which in this problem is (f(x)+f(x)ϵ)x=x0+ϵ=(2cos(2x)sin(4x)4sin(2x)ϵ4cos(4x)ϵ)x=π2+ϵ=2cos(π+2ϵ)sin(2π+4ϵ)4sin(π+2ϵ)ϵ4cos(2π+4ϵ)ϵ=2cos(2ϵ)sin(4ϵ)+4sin(2ϵ)ϵ4cos(4ϵ)ϵ(f(x) + f'(x)\epsilon)|_{x = x_0 + \epsilon} = (2\cos(2x) - \sin(4x) - 4\sin(2x)\epsilon - 4\cos(4x)\epsilon)|_{x = \frac{\pi}{2} + \epsilon} = 2\cos(\pi + 2\epsilon) - \sin(2\pi + 4\epsilon) - 4\sin(\pi + 2\epsilon)\epsilon - 4\cos(2\pi + 4\epsilon)\epsilon = -2\cos(2\epsilon) - \sin(4\epsilon) + 4\sin(2\epsilon)\epsilon - 4\cos(4\epsilon)\epsilon.

This is the part where I think I'm messing it up, but I can't see how. There is not any division or fancier operation to contend with, so I reason that I can replace each individual sinusoid with just the portion of its Maclaurin Expansion which is affine in ϵ\epsilon; any higher order terms are not going to be rescued from ϵ2\epsilon^2 annihilation in the final answer by just addition, subtraction, and non-fractional multiplication. Thus cos(kϵ)=1\cos(k\epsilon) = 1 and sin(kϵ)=kϵ\sin(k\epsilon) = k\epsilon for any constant kk. Then my answer reduces to 2(1)(4ϵ)+4(2ϵ)ϵ4(1)ϵ-2(1) - (4\epsilon) + 4(2\epsilon)\epsilon - 4(1)\epsilon or...28ϵ-2 - 8\epsilon
However, the answer should be 24ϵ-2 - 4\epsilon, as computed in Mathematica...

AutoDiff.png
Do I just have a mistake in the algebra, or have I reasoned or understood the setup incorrectly?
 
You want to use f(x0)f(x_0), not f(x0+ϵ)f(x_0+\epsilon). This is analogous to an approximation formula f(x0+ϵ)f(x0)+ϵf(x0)f(x_0+\epsilon) \approx f(x_0) + \epsilon f^\prime(x_0) -- you don't use x0+ϵx_0+\epsilon in the right-hand side.
 
You messed it up using differentials and evaluations all in one equation. That mixes functions, functionals, and values.

Why do you want to evaluate f+f f+f' ?

We have f=M2cosM2sinM4 f=M_2\circ \cos\circ M_2 -\sin\circ M_4 where Ma(x)=ax M_a(x)=a\cdot x with dMa=Ma,dM_a=M_a, and
df=d(M2cosM2)d(sinM4)=dM2d(cosM2)cosM4dM4=M2sinM2dM2cosM4M4=M22sinM2M4cosM4\begin{array}{lll} df&= d(M_2\circ \cos\circ M_2)-d(\sin\circ M_4)\\[6pt] &=dM_2\,d(\cos\circ M_2)-\cos\circ M_4\,dM_4\\[6pt] &=-M_2 \sin\circ M_2 \,dM_2-\cos\circ M_4\,M_4\\[6pt] &=-M_2^2 \sin\circ M_2 - M_4\cos\circ M_4 \end{array}in case you insist on writing it as differential forms. The ordinary way is
(2cos(2x)sin(4x))=2sin(2x)2cos(4x)4=4sin(2x)4cos(4x) (2\cos(2x)- \sin(4x))'=-2\sin(2x)\cdot 2-\cos(4x)\cdot 4=-4\sin(2x)-4\cos(4x) or
ddxx=π/2(2cos(2x)sin(4x))=4sin(π)4cos(2π)=4 \left. \dfrac{d}{dx}\right|_{x=\pi/2}(2\cos(2x)- \sin(4x))=-4\sin(\pi)-4\cos(2\pi)=-4 or
ddxx=π/2+ε(2cos(2x)sin(4x))=4sin(π+2ε)4cos(2π+4ε)=4cos(4ε)+4sin(2ε)=4+8ε+32ε216ε33128ε43+O(ε5)\begin{array}{lll} \left. \dfrac{d}{dx}\right|_{x=\pi/2+\varepsilon}(2\cos(2x)- \sin(4x))&=-4\sin(\pi+2\varepsilon)-4\cos(2\pi+4\varepsilon)\\[18pt] &=-4 \cos(4 \varepsilon) + 4 \sin(2 \varepsilon) \\[12pt] &=-4 + 8 \varepsilon + 32 \varepsilon^2 - \dfrac{16\varepsilon^3}{3}- \dfrac{128\varepsilon^4}{3} + O(\varepsilon^5) \end{array}
I hope I made no typos.
 
Got it! So yeah my setup was wrong. It was what blamocur said about the input not being a dual number, except that's how to go down the analytic solution path of regular calculus. For the automatic differentiation path it's the opposite; I should have kept the dual number structure of the input but not the function, and then it turns into just tracing code execution. What I did was merge them into automalytic differentiation (which doesn't exist but still outputs dual numbers as answers)!
 
Top