Replace built-in symbolic function
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
John D'Errico
il 6 Giu 2018
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.
Risposte (1)
Walter Roberson
il 6 Giu 2018
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 Commento
Walter Roberson
il 6 Giu 2018
The better approach would be to use MyExp in your formulas instead of exp()
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!