Ackley Function value
7 views (last 30 days)
Show older comments
Hi
I coded the Ackley function, but when I tested it, I had surprised. I must received 0; but I received 8.88178419700125e-016. By a tiny movement of function's element I received 0. for more complex functions, how can I avoid this situation?
% start of my code:------------------
x=[0 0 0 0 0 0 0];
Dim=size(x,2);
y1=-20*exp(-0.2*sqrt(sum((x.^2),2)./Dim))-exp(sum((cos(2.*pi.*x)),2)./Dim)+20+exp(1);
y2=-20*exp(-0.2*sqrt(sum((x.^2),2)./Dim))-exp(sum((cos(2.*pi.*x)),2)./Dim)+exp(1)+20;
disp(y1);
disp(y2);
% end of my code----------------------
the deference between y1 and y2 formula is: 20+exp(1) and exp(1)+20
0 Comments
Answers (2)
Fangjun Jiang
on 12 Nov 2011
This is no surprise. It is a typical floating point precision problem.
0 Comments
Walter Roberson
on 12 Nov 2011
To avoid the problem, use a hypothetical computer with infinite precision, and wait an infinite amount of time for the answer.
If you work with irrational numbers on any machine with finite precision, you will encounter round-off.
Calculate 1/3 to 25 decimal places. Now multiply that value by 3. Do you get exactly 1 as a result? Or do you instead get 0.9999999999999999999999999 ? If you had used 25000 decimal places, would you have gotten exactly the 1 you "should" get? How many decimal places do you need to use before you do get exactly 1 as the result?
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!