Script adapted into a funtion Function not returning equal values

조회 수: 2 (최근 30일)
C Meek
C Meek 2012년 7월 22일
Hey everyone,
I'm having a problem where two identical pieces of code are returning different values when run as a script then a function. Trying to keep this as simple as possible, I have two vectors Z and Zt, which represent curves with a number of prominent peaks. I find the peak locations of each curve, then compare their separation distance using an adapted Gaussian function:
1 - exp( (-mu0*Phi(ii)) / (2 * vp^2));
This is an extract from a loop, where Phi contains the difference between each peak of Z and Zt. mu0 and vp won't particularly matter here.
In particular, I have Z and Zt set so that their peak locations are in exactly the same place - and as a result the expression above will be
1 - exp(0) = 0;
for all entries in Phi.
This is exaclty what I want, but when I run this with a function call, it is returning very small values of order 10^-5, instead of zero.
Anyone have any ideas of why this may be?

채택된 답변

Geoff
Geoff 2012년 7월 22일
Floating point precision errors. If you need to test for zero, then test for something like:
if abs(value) < 1e-5
10^-5 does sound rather large for a zero value though.. Are you using single-precision floats?
  댓글 수: 1
C Meek
C Meek 2012년 7월 23일
I'm using double precision. Turns out that the unexpected small numbers were coming from another part of my code which I hadn't taken into account - so all is well, when I adapt it according zero is returned ok!
Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by