I have an optimization problem that involves series of high order 
derivatives of f(x). 
So, to get a decent value, I need to calculate higher order derivative at 
least to 40~50th order. 

Initially, I tried to use ForwardDiff package with automatic 
differentiation but kept facing the following error message

<div id="inner-editor"><br class="Apple-interchange-newline">using 
DualNumbers using ForwardDiff f(x::Vector) = sum(sin, x) + prod(tan, x) * 
sum(sqrt, x); x = rand(5) g = ForwardDiff.derivative(f);</div>

using DualNumbers

using ForwardDiff

f(x::Vector) = sum(sin, x) + prod(tan, x) * sum(sqrt, x);

x = rand(5)

g = ForwardDiff.derivative(f);

LoadError: MethodError: `derivative` has no method matching 
derivative(::Function)
Closest candidates are:
  derivative(::Any, !Matched::Any)
while loading In[28], in expression starting on line 4



So, I moved on to D(f) instead from Root package and it works quite well 
for a small order (like 10th order derivative)

<div id="inner-editor"><br class="Apple-interchange-newline">using 
DualNumbers using ForwardDiff using Roots using ForwardDiff using JuMP # 
Need to say it whenever we use JuMP using Gadfly function hder(f, n::Real) 
# subroutine to calculate higher order derivative temp = f; if n==0 return 
temp else for i=1:1:n temp = D(temp) end return temp end end</div>

using DualNumbers

using ForwardDiff

using Roots

using ForwardDiff

​

using JuMP  

using Gadfly

​

function hder(f, n::Real) # subroutine to calculate n-th order derivative 

    temp = f;

    if n==0

        return temp 

    else

        for i=1:1:n

            temp = D(temp)

        end

        return temp

    end

end

Out[7]:

hder (generic function with 1 method)

f(x) = (cos(x)) * exp(-1/5 * x)
g0 = hder(f, 0)
g1 = hder(f, 1)
g2 = hder(f, 2)
g3 = hder(f, 3)
g4 = hder(f, 4)
g5 = hder(f, 5)
g6 = hder(f, 6)
g7 = hder(f, 7)
g8 = hder(f, 8)

plot([g0, g1, g2, g3, g4, g5, g6, g7, g8], 0, 5pi)

<https://lh3.googleusercontent.com/-RCrcnLVFPhE/V4tfcG9uSEI/AAAAAAAAGUU/unxjN7x60jgPaEcCmcRlykx8WpbMxJBnACLcB/s1600/Screen%2BShot%2B2016-07-17%2Bat%2B11.35.09.png>
However, if I push the limit and ask to calculate, like 30th order 
derivative, the program never ends and keep calculating for hours.
Is there a better way to do this type of task? either by using ForwardDiff 
package or by modifying my function?
I just learned Julia last week, so please understand if my question sounds 
stupid. 



Reply via email to