*How do I* Simplify by substituting user defined variables for several repetitive expressions in a symbolic Matlab result (answer)

10 views (last 30 days)
I have a very long result. It is the result of symbolically differentiating a somewhat complicated expression. The result is probably 2000 characters long. There are many repetitive expressions that arise in the result. Not only will it make it look nicer to substitute many of the expressions for simpler ones, but it is actually also necessary in order to proceed with coding.
I have two specific examples that I am concerned with.
First, I have many repetitive expressions such as "1/(ti-tj)+1/(ti-tk)" which arise a lot inside what I will call sub-expressions, and I would prefer to change all instances of this to 2(1-Eta)/S2. Another would be that I want to convert all 2*(1-Eta)*(ti-tj)*(ti-tk) to a simple "S1".
Second, in differentiating there are now terms such as D(L)(ti) which mean the derivative of L with respect to ti. I would like Matlab to replace all of these with "L1". If it does this, there will then be many cancellation with other expressions already using L1(L1 is a symbolic variable used by me and appears many times in the initial equation and result), and this will greatly simplify the result. If it can't do this I need to go through and manually change them, which gets very tedious with results that are 4+ pages long.
Thanks in advance for any replies to my query,
Braden

Accepted Answer

Walter Roberson
Walter Roberson on 20 Nov 2015
You are probably going to need to work at the MuPAD level, even if only by calling evalin(symengine) or feval(symengine)
However, you need to understand that expression trees need to match exactly to do this kind of manipulation. You can might be able to find all the subtrees that are 1/(ti-tj)+1/(ti-tk) and replace them, but 2/(ti-tj)+2/(ti-tk) is a different subtree that is not composed of the subtree _mult(2, 1/(ti-tj)+1/(ti-tk)) . Likewise, if you were to be searching for A*x^5 + B*x^4 + C*x^3 + 1 then the tree A*x^5 + B*x^4 + C*x^3 + 2 will not match it because although we as humans can easily see that is (A*x^5 + B*x^4 + C*x^3 + 1) + 1, MuPAD is not going to know that. And although you can search for a pattern A*x^5 + B*x^4 + C*x^3 + A_NUMERIC_CONSTANT, as humans we would also like to be able to automatically match A*x^5 + B*x^4 + 2*C*x^3 + 1 as being (A*x^5 + B*x^4 + C*x^3 + 1) + C*x^3 .. and then you want A*x^5 + B*x^4 + 2*C*x^3 + 5 to be recognized as (A*x^5 + B*x^4 + C*x^3 + 1) + (C*x^3 + 4)... when do you stop in the effort to match?
You might also find that in some of the subtrees, the same values are present but in a different order, depending on exactly how the trees got constructed. Subtrees are not sorted automatically (especially multivariate ones), so subtree x + y + 1 might be y + 1 + x . Negatives can get messy, -x-y might be _plus(_mult(-1,x),_mult(-1,y)) or might be _mult(-1,_plus(x,y)) or what you observe visually as (-x-y)/z might be stored as _mult(_plus(x,y),_mult(-1,_power(y,-1)))

More Answers (0)

Community Treasure Hunt

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

Start Hunting!