3

I am trying to figure out why I am getting all zeros back in the code below.

a = 7.0e16;
e = 100000;
r = 8.3140;
t = 253:2:325;

k = a.*exp(-e./t.*r);

k returns as 1x37 array consisting of only zeros.

Is it because my numbers are too large or too small?

HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
user1757273
  • 71
  • 1
  • 2
  • 9

1 Answers1

4

You are getting underflow. Things become zero in the exp, and multiplying by a big number afterwards is too late. It looks like a physics equation - in which case you want to divide by r, not multiply. Try

exp(log(a)-e./(t*r))

It should work

EDIT - have to add, not multiply, log(a) in the exponent...

Floris
  • 45,857
  • 6
  • 70
  • 122
  • your first suggestion was right! and i didnt want a in the exponent. i wanted to multiple the result of exponent by a – user1757273 Jan 21 '13 at 21:53
  • Glad it worked. Mathematically, `a*exp(b)` is the same as `exp(ln(a)+b)` when `a>0`. I was trying to make the number in the exponent larger - didn't have Matlab handy so wasn't quite sure how far off underflow you were... – Floris Jan 22 '13 at 03:38
  • @Floris I'm having the same problem but in my case it is `(-0.1 * exp(-0.2 * distance * distance))` where distance is approx. around 450. The result is zero. How can I do that? – Gowtham Mar 21 '15 at 15:34
  • @Gowtham for sure the umber you are trying to calculate is very small. Take the logarithm of your expression (on paper, not with Matlab!) and you will see just _how_ very small it is. What do you want to do with the result? – Floris Mar 21 '15 at 15:55
  • I just want to add it with another similar expression. The answer is always zero since both of the expression returns zero. I need a result in the range of 1.** to 10.** – Gowtham Mar 21 '15 at 16:08
  • If you add two very small numbers, your result will be a very small number. Maybe I don't understand what you are trying to do. The expression you have, that is `-0.1 * exp( -0.2*450*450)` is approximately `-8.44E-17589` (because `log10(e)*0.2*450*450 = 17588.92`). Most floating point processors consider that equal to zero. To preserve accuracy you need to scale your inputs. – Floris Mar 21 '15 at 20:45