Ackley Function value

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

Respuestas (2)

Walter Roberson
Walter Roberson el 12 de Nov. de 2011

1 voto

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?

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 12 de Nov. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by