Have a maple symbolic expression in MATLAB with % symbols, how can I get to just the solution?

1 view (last 30 days)
I have a long solution to polynomials I found using matlab's solve command using the maple symbolic engine. The reusult however has %1, %2 and %3 for substitution. I can isolate the %1, %2 and %3 and get an expression in MATLAB i can assign values to. Is there any way I could isolate the actual solution in terms of an expression I can evaluate with MATLAB. Thanks
Chris

Answers (1)

Walter Roberson
Walter Roberson on 14 Oct 2011
I think this is what I answered the first time around in http://www.mathworks.com/matlabcentral/answers/18235-1-2-and-3-in-solution ??
If the % expressions involve only variables that appear only there, then it might make sense to "isolate the actual solution in terms of an expression" you can evaluate with MATLAB, if by "actual solution" you refer to the entire long expression (probably in RootOf's) that expresses all of the roots.
But if the % expressions involve variables that also appear in other places, then assigning a fixed value to the subexpression is equivalent to assigning a constraint that it is not certain that one would be able to satisfy to get the overall roots. In such a case, you would add the constraint to the solve() equations and try again.
I think maybe I do not understand the question...
  2 Comments
Chris
Chris on 14 Oct 2011
I guess my thought is that the subs() function seems to be taking a long time to execute. I originally tried to convert the expression to matlab code by taking the symbolic solution and using char(solution) which gave me an answer but over 4 million characters long because of all of the substitutions of the %1, %2 and %3s. My idea was to solve numerically for each of %1, %2 and %3, then just substitute those numerical values into the actual solution. To do this my thought was to Isolate the solution and use a "find-replace" all % with a matlab variable name that I assign the values of all of the %s. Make sense?
Thanks again for your help,
Chris
Walter Roberson
Walter Roberson on 14 Oct 2011
Do you have the Extended Symbolic Toolbox or just the Symbolic Toolbox? If you have the Extended Symbolic, then you have full access to Maple and could then potentially use Maple's
CodeGeneration[Matlab]
in combination with codegen[optimize]
The Codegeneration facility is not available with the plain Symbolic Toolbox (not unless perhaps you are interfacing to a full version of Maple.)
With the way Maple works internally, subs() for %1 etc is one of the faster operations: each of those common expressions that are labeled with the same label points to exactly the same symbolic sub-tree, so subs() of all of them is implemented by simply replacing that symbolic sub-tree once with the replacement value.
Anyhow, if you want to do it the text-representation way, then we are back to the evalc() method I described in the other answer.
Note: when you are working with large symbolic expressions like this and doing subs(), then except for the "mandatory simplifications" (like removing + 0, or gathering constants at the same algebraic level), the resulting expression is not evaluated or simplified, which is why often you will see simplify(subs(...)) in Maple programs. subs() is pretty fast. What is slow is formatting large symbolic expressions for 2D printout!! If you have a large symbolic expression, print it out as seldom as you can get away with. And the process of labeling with %1 etc only applies for 2D printouts.
If you have a solution of polynomials, then _usually_ the approach is to concentrate on the RootOf() expressions, rather than the labeled expressions. If you substitute numeric values for the RootOf()'s, then a lot of the rest will usually collapse. Oh, you might still end up with big subtrees like sqrt(2+sqrt(2+sqrt(2+sqrt(....))))) but you can evalf() [Maple level] or vpa (MATLAB level) those out of existence pretty quickly unless you have a free variable caught up in them.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!