‘UndefVarError: xx not defined’

function ackley(x)
d = length(xx);
c = 2pi;
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(cx2);
term1 = -a * exp(-b
sqrt(sum1/d));
term2 = -exp(sum2/d);
y = term1 + term2 + a + exp(1);
end

when calling y=ackley.(x)

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

3 Likes

Have a look at this: https://cn.julialang.org/blog/2015/10/auto-diff-in-julia/#performance_comparison_the_ackley_function

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:

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
1 Like

Thanks to all of you. Problem has resolved.