Factorise symbolic expression (square of a sum) in MATLAB
Asked Answered
M

1

8

If I start with the following symbolic expression:

a^2 + 2*a*b + b^2

Then run simplify (or factor), I get the expected result:

>> simplify(a^2 + 2*a*b + b^2)

(a + b)^2

Now when I run the same example, but adding another term, no factorisation occurs:

>> simplify(a^2 + 2*a*b + b^2 + 1)

a^2 + 2*a*b + b^2 + 1

How can I get these functions to return the more practical version of this expression ((a + b)^2 + 1)? I have tried all of the obvious options with these functions (like 'Steps', 'IgnoreAnalyticConstraints', etc.) but to no avail.

Context: I have the expression ax^2 - 2*ax*bx + bx^2 + ay^2 - 2*ay*by + by^2 which I need to convert back into (ax - bx)^2 + (ay - by)^2 so it can then be treated correctly as r^2. I know I could use some blunt substitution rules, but for something so simple I feel like I'm missing an obvious 'non-hack' solution.

Malignancy answered 10/2, 2016 at 1:12 Comment(5)
you use mathematica instead...Slain
I think the Matlab symbolic library is a bit limited. Symbolic calculations are not the greatest strength of Matlab.Fredericksburg
Switch to R and use Ryacas . I haven't found any tool to call yacas from Matlab.Cyanogen
Thanks for the suggestions guys. I'd already completed the problem in Mathematica but was hoping to use MATLAB for a wider application (the scope of what I'm doing is much larger than in the question and requires MATLAB for other bits). If this is already reaching the limit of MATLAB's symbolic math toolbox, I'll have to look into a different approach.Malignancy
If you really need to use MATLAB to do symbolic math, look into the Maple Toolbox for MATLAB. This was the symbolic engine in MATLAB before TMW switched to MuPad a few years ago on cost considerations. Far superior IMHO to MuPad.Percheron
A
1

you can run simplify on the two terms separately.

simplify(ax^2 - 2*ax*bx + bx^2) + simplify(ay^2 - 2*ay*by + by^2)

It seems like you already know how it should be simplified anyway.

Also, you eventually want to write it as r^2. This is not generally possible for all second-order expressions, so don't bother trying to find a general solution.

Acima answered 20/4, 2016 at 13:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.