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
回答(2 个)
Fangjun Jiang
2011-11-12
1 个投票
This is no surprise. It is a typical floating point precision problem.
Walter Roberson
2011-11-12
1 个投票
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?
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!