Symbolic Math Toolbox hitting divide by zero error when it used to evaluate to NaN

北城余情 提交于 2019-12-07 08:08:44

问题


I've just updated to Matlab 2014a finally. I have loads of scripts that use the Symbolic Math Toolbox that used to work fine, but now hit the following error:

Error using mupadmex
Error in MuPAD command: Division by zero. [_power]
  Evaluating: symobj::trysubs

I can't post my actual code here, but here is a simplified example:

syms f x y
f = x/y
results = double(subs(f, {'x','y'}, {1:10,-4:5}))

In my actual script I'm passing two 23x23 grids of values to a complicated function and I don't know in advance which of these values will result in the divide by zero. Everything I can find on Google just tells me not to attempt an evaluation that will result in the divide by zero. Not helpful! I used to get 'inf' (or 'NaN' - I can't specifically remember) for those it could not evaluate that I could easily filter for when I do the next steps on this data.

Does anyone know how to force Matlab 2014a back to that behaviour rather than throwing the error? Or am I doomed to running an older version of Matlab forever or going through the significant pain of changing my approach to this to avoid the divide by zero?


回答1:


You could define a division which has the behaviour you want, this division function returns inf for division by zero:

mydiv=@(x,y)x/(dirac(y)+y)+dirac(y)
f = mydiv(x,y)
results = double(subs(f, {'x','y'}, {1:10,-4:5}))


来源:https://stackoverflow.com/questions/25892135/symbolic-math-toolbox-hitting-divide-by-zero-error-when-it-used-to-evaluate-to-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!