MATLAB: Subs command in Symbolic Maths; Error: Devide by zero.

denominator identificationdevide by zerosingularitysubssymbolic maths

For a symbolic maths problem;
FX=myfun(formula, given_matrix,my_values,my_variables);
X1=[3 -1 0 5 0];
return=subs(FX,my_variables,X1);
The problem is that my FX might have X1(3) or X1(5) or any possible combination in denominator causing "Devide by ZERO".
QUESTION: Though it is easy to avoid it mathematically, but in the programming, this type of situation may arise in any loop as FX and X1 are updated in every loop. IS THERE A WAY TO IDENTIFY NUMERATORS AND DENOMINATORS OF SUCH INTERMEDIATE FUNCTIONS?? Thanks a lot for reading.

Best Answer

This may be ‘cheating’ but it works:
X1=[3 -1 0 5 0];
X1(X1 == 0) = eps
yields:
X1 =
3.0000e+000 -1.0000e+000 222.0446e-018 5.0000e+000 222.0446e-018
The idea is to replace the zero values with ‘eps’ to prevent NaN, Inf, and the like. It sort of invokes L’Hôpital’s rule in some situations, and while it may create values on the order of 1/eps in others, won’t throw an error. (You can test for out-of-range values later in your code.) I prefer eps to realmin because eps is the lowest value that your computer will add or subtract, so it won’t get ‘lost’ in the process. It also won’t underflow or overflow, as would realmin.