MATLAB: Wrong limit when L’hopital’s rule is applied

l'hopitallimitMATLABsymbolic

I am trying to find the limit as z tends to e of the expression  ( cosh(log(z)-1)-1)/sin(z-e). Using L'Hopital's rule you can find that this limit is zero, but consider this code:
 
%problem 3
clear
syms z
v=(cosh(log(z)-1)-1)/(sin(z-exp(1)));
our_limit=limit(v,z,exp(1))
%work below shows that limit is zero at z=1
clear z v
z=linspace (2,3,1000);
v=(cosh(log(z)-1)-1)./(sin(z-exp(1)));
plot(z,v);grid
The output of this code is "our_limit =NaN" even though the graph does pass thru zero when z=e.

Best Answer

When working with floating point numbers and symbolic variables, it is good practice to convert floats to symbolic form. For example, in the code you provided, "exp(1)" is internally converted to a double precision number, before it is passed to the symbolic tool. As a result, when the limit is calculated, the quantity "z-exp(1)" inside the sine is evaluated as "exp(sym(1))-exp(1)" (since z is a symbolic variable) which is not precisely equal to zero. Replacing "exp(1)" with "exp(sym(1))", should resolve the issue. Here is an example:
 
clear
syms z
v=(cosh(log(z)-1)-1)/(sin(z-exp(sym(1))));
our_limit=limit(v,z,exp(sym(1)))
For more information, there is a very informative article about this issue <https://www.mathworks.com/matlabcentral/answers/286147-problem-finding-limits-when-l-hopital-s-rule-is-needed here>.