MATLAB: How to make sure that ratios of very large numbers(e^ā€‹1000+1)/(eā€‹^1000-1) is not given as NaN

MATLABnan

I am trying to plot a function
T = (exp(x) + 1)/(exp(x) - 1)
where x ranges from say -10000 to 10000. Since exp(x) will be very large, the MATLAB is giving NaN instead of actual values. How to overcome this?
Thank you.

Best Answer

You can use an alternate formula for the large values:

T = (1 + 1./exp(x)) ./ (1 - 1./exp(x))

Note that once x gets large enough (19), the ratio calculation becomes equivalent to the "first order" approximation 1 + 2/exp(x) because of the limits of double precision arithmetic.