Is there a way to Rewrite a Symbolic Expression in Terms of nthroot?

6 views (last 30 days)
Suppose I have a symbolic expression
syms x real
g = x^(1/3) - 1
g = 
I'd like to evaluate g for negative values of x, either symbolically or numerically with a matlabFunction, and return the real root. But
vpa(subs(g,x,-0.5))
ans = 
gf = matlabFunction(g);
gf(-0.5)
ans = -0.6031 + 0.6874i
Instead, what I'd like to have is:
gg = nthroot(x,3) - 1;
vpa(subs(gg,-0.5))
ans = 
ggf = matlabFunction(gg);
ggf(-0.5)
ans = -1.7937
Suppose g is the result of some other sym operation, so I don't have control over how it's defined.
Is there a simple, automatic, way to change the x^(1/3) to nthroot(x,3). I can do a subs
subs(g,x^(1/3),nthroot(x,3))
ans = 
ans = function_handle with value:
@(x)nthroot(x,3.0)-1.0
But that requires that I know the term(s) I'm subsing for. I want something more generic. So far, the only concept I've thought of is repeated calls to children(), use symFunType to find the power terms, then find those that have the rational exponents with positive denominators, do the nthroot substitution on those, and then reconstruct the expression. I haven't actually tried this yet.
Is there a simpler way, something akin to rewrite(g,'nthroot')?

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jul 2023
syms x real
g = x^(1/3) - 1
g = 
mapSymType(g, 'power', @(X) nthroot(children(X,1), 1/children(X,2)))
ans = 
This particular implementation will fail of there are any ^ that are not 1/odd integer. The work-around would be to insert piecewise() checks, like piecewise(mod(1/children(X,2),2)~=0, X, nthroot(children(X,1), 1/children(X,2)))
  4 Comments
Paul
Paul on 18 Jul 2023
Edited: Paul on 18 Jul 2023
IDK. What's weird is that matlabFunction converted nthroot to a function called surd, but that function doesn't exist (in 2021b). I neglected to post the complete error message; here it is.
>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.11.0.1769968 (R2021b)
>> gfun(-0.5)
Unrecognized function or variable 'surd'.
Error in symengine>@(x)surd(x,3.0)
But,
>> which -all surd
'surd' not found.
And here is the meta info on gfun
>> functions(gfun)
ans =
struct with fields:
function: '@(x)surd(x,3.0)'
type: 'anonymous'
file: 'C:\Program Files\MATLAB\R2021b\toolbox\symbolic\symbolic\symengine.p'
workspace: {[1×1 struct]}
within_file_path: ''
So it looks like matlabFunction thinks there's a function over in the SMT called surd, and translates nthroot to that instead of /matlab/elfu/nthroot. But there is no function called surd in 2021b.
BUT, I went back to the oldest release on the support website and found surd in 2018a. The symbolic nthroot was introduced in 2018b. Maybe someone didn't think to update matlabFunction in 2018b to map symbolic nthroot to elfun/nthroot (?) and the symbolic surd is now no longer
Doesn't matter. I'll need to update if I need the matlabFunction functionality.
Also, the piecewise method seems to work, but the the condition should be ==
syms x real
g = (x-1)^(1/3)*(x-3)^(1/4)
g = 
% ||
mapSymType(g, 'power', @(X) piecewise(mod(1/children(X,2),2)==0, X, nthroot(children(X,1), 1/children(X,2))))
ans = 

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!