Replace built-in symbolic function

1 次查看(过去 30 天)
Is there any way to provide own function for symbolic to double conversion of certain built-in functions like exp?
When converting from symbolic to double I need to use some conditional logic, i.e. if argument of exp is larger than some upper limit a different function then exp should be used to obtain a double value. Analogously for values lower then zero.
  1 个评论
John D'Errico
John D'Errico 2018-6-6
Not really. Well, yes, I suppose you could write a parser, looking for calls to exp. That would be pure hell to write.
Could you overload the function exp? I suppose you could. But that would potentially break so much of existing MATLAB tools, and make things slow to boot.
What is the purpose of this exercise? Let me guess. You have a problem with underfows and overflows. So you want exp to under/overflow at less extreme points? Again, dangerous as hell, especially if you come back a year later, having completely forgotten you did that. Can you say nasty bug?
I'd suggest using better numerical methods to deal with the situation. Very possibly, that may mean never going into symbolic form in the first place, often just a crutch to avoid needing good computational methods.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-6-6
Example:
syms x
y = exp(1/cos(x))
MyExp(x) = piecewise(x<-50,gamma(x),x>50,sqrt(x),exp(x));
ny = subs(y, exp(1/cos(x)), MyExp(1/cos(x)))
ny =
piecewise(1/cos(x) < -50, gamma(1/cos(x)), 50 < 1/cos(x), (1/cos(x))^(1/2), exp(1/cos(x)))
This is not as clean as one might hope, as one needs to know the expressions that are the arguments to exp() in order to do the substitutions.
I did some experiments with dropping into the MuPAD engine, but unfortunately the difference between operations and operands for MuPAD purposes is messing things up.
  1 个评论
Walter Roberson
Walter Roberson 2018-6-6
The better approach would be to use MyExp in your formulas instead of exp()

请先登录,再进行评论。

产品


版本

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by