Ackley Function value

조회 수: 3 (최근 30일)
hadi
hadi 2011년 11월 12일
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

답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 11월 12일
This is no surprise. It is a typical floating point precision problem.

Walter Roberson
Walter Roberson 2011년 11월 12일
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?

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by