Is the exponential function problematic and very expensive to compute in Matlab?

When I write a new term for my model of ODEs that has an exponential term in it, the program almost never finishes debugging, and I hear the fan on my computer go crazy. So I quickly stop debugging and remove that term in my model. And then all is well again.

Will using the exp() function even physically harm my computer, say, for instance, burn out my hard drive?


  • 1
    $\begingroup$ Do you know what is the argument of the exponent for which this takes time ? In one of my fortran code, I have found that computing $x^y$ when $x$ is close to 1 is VERY expensive. This seems to be known problem with glibc, see stackoverflow.com/questions/9272155/… and entropymine.com/imageworsener/slowpow. clang seems not to have this problem. $\endgroup$
    – cfdlab
    Commented Nov 1, 2017 at 3:47
  • 3
    $\begingroup$ side note: it's funny you are concerned that you are harming your computer. Modern CPUs slow down when they detect it's too hot. $\endgroup$
    – Memming
    Commented Nov 1, 2017 at 13:36
  • $\begingroup$ @PraveenChandrashekar Did you use expm1? $\endgroup$ Commented Nov 2, 2017 at 20:06
  • $\begingroup$ @PraveenChandrashekar It should optimize to some floating point operand. If it doesn't, what if you would substitute it with logarithm + multiplication? $\endgroup$
    – user9927
    Commented Nov 4, 2017 at 12:55

1 Answer 1


Computing the term $e^x$ is definitely significantly more expensive than computing a lower-order polynomial -- say $x^4$. But it may be ten to 100 times more expensive at most, not "crazy" expensive. So I suspect that if Matlab takes forever to compute something, then that's because the character of your ODE changes significantly. For example, the presence of that term may make your ODE "stiff" and require a significantly smaller time step than otherwise. That would mean that it's not the evaluation of the right hand side that's expensive, but simply that you need to do many more time steps.

As for physical harm: No, computers are designed for this kind of work. There is no need to worry about the computer; you may have to worry about getting your knees too warm, though :-)

  • 1
    $\begingroup$ Yes, I would guess that adding an exponential term is an easy way to make any system stiff and would guess that this fact is the main cause of sending it from easily computable -> hard to compute. $\endgroup$ Commented Nov 1, 2017 at 6:04
  • $\begingroup$ This is very strange. In C (glibc, to be precise), the exponent is computed in O(1). Not in O(logN), pure constant time, with no loops or subroutine calls. Maybe it can't be used via SIMD and thus its slower for vectors and matrices compared to repeated multiplication. $\endgroup$ Commented Jan 16 at 18:21
  • 1
    $\begingroup$ @PeterZaitcev I said nothing to the contrary. 10-100 times as expensive than a single multiplication is still $O(1)$. $\endgroup$ Commented Jan 16 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.