calculating a function in matlab with very small values

franvergara66 picture franvergara66 · Apr 4, 2012 · Viewed 7.2k times · Source

I am making a function in matlab to compute the following function:

enter image description here

for this function we have:

enter image description here

This is my implementation in matlab of the function:

function [b]= exponential(e)
%b = ? 
b= (exp (e) -1)/e;

When I test the function with very small values, indeed the limit of the function is to 1, but when the number is very small (eg 1*e-20) the limit goes to zero? what is the explanation to this phenomenon?. Am I doing something wrong?.

x= 10e-1 , f (x)= 1.0517

x= 10e-5 , f (x)=  1.0000

x= 10e-10 , f (x)=  1.0000

x= 10e-20 , f (x)=  0

Answer

Ramashalanka picture Ramashalanka · Apr 4, 2012

The problem is that exp(x) is approx 1+x, but is being evaluated as 1 due to the 1 being indistinguishable from 1+x in the floating point representation. There is a MATLAB function expm1(x) (which is exp(x)-1 implemented for small x) that avoids the problem and works well for small arguments:

>> x=1e-100;expm1(x)/x
ans =
     1