# Automatic differentiation with a for loop in the function definition

**URL:** https://discourse.julialang.org/t/automatic-differentiation-with-a-for-loop-in-the-function-definition/64196
**Category:** General Usage
**Tags:** question
**Created:** [July 7, 2021, 6:45am UTC](https://discourse.julialang.org/t/automatic-differentiation-with-a-for-loop-in-the-function-definition/64196 "2021-07-07T06:45:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![aymercan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aymercan/32/26890_2.png) [@aymercan](https://discourse.julialang.org/u/aymercan)
#### Post date: [July 7, 2021, 6:45am UTC](https://discourse.julialang.org/t/automatic-differentiation-with-a-for-loop-in-the-function-definition/64196/1 "2021-07-07T06:45:34Z")

</div>

Hi Julia community,

I am using Julia for my economics research and this is my first question posted here. Hopefully it is a simple one that more experienced users can easily answer.

I am trying to use automatic differentiation to compute the derivative of the two functions below.

```julia

function simplefunction_v1(x)    
    T = 3
    temp = x.^(0:T)
    return temp
end

function simplefunction_v2(x)    
    T = 3    
    temp = zeros(T+1)
    temp[1] = 1.0
    temp[2] = x
    for iT = 3:T+1
        temp[iT] = x*temp[iT-1]
    end
    return temp
end

```

These two functions return equivalent results for a scalar x. I run the following to use the ForwardDiff package to calculate the derivative of each function evaluated at x=2.0.

```julia
using ForwardDiff
ForwardDiff.derivative(simplefunction_v1,2.0)
ForwardDiff.derivative(simplefunction_v2,2.0)

```

While the second line (simplefunction\_v1) above works as expected, I get an error from running the third line (simplefunction\_v2).

It goes without saying that my actual application is quite a bit more involved, where I cannot get rid of the recursive relationship in the function definition. I would greatly appreciate any direction as to how to get the second version of the function working with automatic differentiation.

Thanks a lot.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [July 7, 2021, 9:50am UTC](https://discourse.julialang.org/t/automatic-differentiation-with-a-for-loop-in-the-function-definition/64196/2 "2021-07-07T09:50:55Z")

</div>

> [@aymercan](#):
>
> ```julia
> temp = zeros(T+1)
> 
> ```

is not generic. `temp = zeros(eltype(x),T+1)`

---

<div class="post-metadata">

### Author: ![aymercan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aymercan/32/26890_2.png) [@aymercan](https://discourse.julialang.org/u/aymercan)
#### Post date: [July 7, 2021, 9:55am UTC](https://discourse.julialang.org/t/automatic-differentiation-with-a-for-loop-in-the-function-definition/64196/4 "2021-07-07T09:55:34Z")

</div>

Perfect. Thank you!
