why coding this function returns un exact numerical results?

조회 수: 4 (최근 30일)
Cantor Set
Cantor Set 2021년 6월 22일
댓글: Walter Roberson 2021년 6월 23일
Ackley's Function (a=20,b=0.2) d=2.
function [ y ] = objfun14 (phen )
d=size(phen,2);
v=cos(2*pi*phen); x=(1/d)* sum(v,2);
t=-20*exp(-0.2*sqrt((1/d)*sum(phen.^2,2))) - exp( x ) +20+exp(1);
y=sum(t,2);
end
%when phen=[0 0] the output should be t=0 however excuting the code gives t=8.8818e-16
  댓글 수: 1
Scott MacKenzie
Scott MacKenzie 2021년 6월 22일
t=8.8818e-16 is pretty close to zero. You are dealing with floating point arithmetic, that's why the result is not exactly what you expect. See Floating-Point Numbers for further details.

댓글을 달려면 로그인하십시오.

답변 (1개)

Kishan Dhakan
Kishan Dhakan 2021년 6월 23일
Try using 'format long', which shows 15 decimal places. If it still doesn't work, then your value is probably smaller than what many call 'machine epsilon' and hence is getting rounded to zero.
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 6월 23일
The other way around, the function is returning -4*eps when the user expects 0

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by