# ‘UndefVarError: xx not defined’

**URL:** <https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278>\
**Category:** New to Julia\
**Created:** [August 4, 2020, 6:14pm UTC](https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278 "2020-08-04T18:14:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RohitRathore1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohitrathore1/32/22848_2.png) [@RohitRathore1](https://discourse.julialang.org/u/RohitRathore1)\
**Post date:** [August 4, 2020, 6:14pm UTC](https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278/1 "2020-08-04T18:14:34Z")

</div>

function ackley(x)  
d = length(xx);  
c = 2_pi;  
b = 0.2;  
a = 20;  
for ii in 1:d  
xi = xx(ii);  
sum1 = sum1 + xi^2;  
sum2 = sum2 + cos(c_xi);  
end  
sum1 = sum1 + x1^2;  
sum2 = sum2 + cos(c_x2);  
term1 = -a \* exp(-b_sqrt(sum1/d));  
term2 = -exp(sum2/d);  
y = term1 + term2 + a + exp(1);  
end

when calling y=ackley.(x)

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [August 4, 2020, 6:18pm UTC](https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278/2 "2020-08-04T18:18:47Z")

</div>

I have to agree with Julia; I can’t find `xx` defined anywhere in that code. Where do you believe it would be defined?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 4, 2020, 7:44pm UTC](https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278/4 "2020-08-04T19:44:18Z")

</div>

Have a look at this: [JSoC 2015 project: Automatic Differentiation in Julia with ForwardDiff.jl](https://cn.julialang.org/blog/2015/10/auto-diff-in-julia/#performance_comparison_the_ackley_function)

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [August 4, 2020, 9:37pm UTC](https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278/5 "2020-08-04T21:37:29Z")

</div>

I guess you tried to translate the code from R or Matlab implementation. Here is how you can do it in Julia with minimal changes to your idea:

```julia
function ackley(x)
    d = length(x)
    c = 2*pi
    b = 0.2
    a = 20
    sum1 = 0
    sum2 = 0
    for i in 1:d
        xi = x[i]
        sum1 += xi^2
        sum2 += cos(c*xi)
    end
    sum1 += x[1]^2
    sum2 += cos(c*x[2])
    term1 = -a * exp(-b*sqrt(sum1/d))
    term2 = -exp(sum2/d)
    y = term1 + term2 + a + exp(1)
end

```

---

<div class="post-metadata">

**Author:** ![RohitRathore1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohitrathore1/32/22848_2.png) [@RohitRathore1](https://discourse.julialang.org/u/RohitRathore1)\
**Post date:** [August 5, 2020, 7:58am UTC](https://discourse.julialang.org/t/undefvarerror-xx-not-defined/44278/6 "2020-08-05T07:58:52Z")

</div>

Thanks to all of you. Problem has resolved.
