Ackley Function value

3 Ansichten (letzte 30 Tage)
hadi
hadi am 12 Nov. 2011
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

Antworten (2)

Fangjun Jiang
Fangjun Jiang am 12 Nov. 2011
This is no surprise. It is a typical floating point precision problem.

Walter Roberson
Walter Roberson am 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?

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by