
Matlab's expand() function
Let me tell you about this function in Matlab called "expand()" that can be really useful when working with polynomials and algebraic expressions.
expand(x)
The idea is to take a polynomial or algebraic expression and expand it, and the syntax is as simple as calling the function with the expression you want to expand as a parameter.
Note. Now, it's important to note that "expand()" uses symbolic computation, so you need to define the variables of your polynomial or expression as symbols first.
Let me show you an example to make it clearer.
Consider the binomial square:
$$ (x+y)^2 $$
We have two variables, x and y, so we need to define them as symbols first, like this:
>> syms x y
And then we can define our binomial and assign it to a variable:
>> P = (x+y)^2
To expand the binomial square, we just need to call "expand()" with our binomial as a parameter:
>> expand(P)
And the result will be:
ans =
x^2 + 2*x*y + y^2
Which is the expansion of the binomial square.
$$ (x+y)^2 = x^2 +2xy + y^2 $$
Let's try another example.
Consider the algebraic expression:
$$ (2x + 3y)^3 + (4x - 2y)^3 $$
We need to define the variables x and y as symbols again.
>> syms x y
And then we define our expression.
>> E = (2*x+3*y)^3 + (4*x-2*y)^3
To expand the expression, we call "expand()" with our expression as a parameter:
>> expand(E)
And the result will be:
ans =
72*x^3 - 60*x^2*y + 102*x*y^2 + 19*y^3
Which is the expanded form of the expression.
$$ 72x^3 - 60x^2y + 102xy^2 + 19y^3 $$
Verify. If you want to verify the result, you can always perform the algebraic calculations by hand, but I can assure you that Matlab is pretty accurate. $$ (2x + 3y)^3 + (4x - 2y)^3 = $$ $$ = (2x)^3+3 \cdot (2x)^2 \cdot 3y + 3 \cdot 2x \cdot (3y)^2 + (3y)^3 + \\ \ \ \ +(4x)^3 + 3 \cdot (4x)^2 \cdot (-2y) + 3 \cdot (4x) \cdot (-2y)^2 + (-2y)^3 $$ $$ = 8x^3+ 36x^2y + 54xy^2 + 27y^3 +64x^3 - 96 x^2y + 48xy^2 -8y^3 $$ $$ = (8x^3+64x^3)+ (36x^2y- 96 x^2y) + (54xy^2 + 48xy^2) + (27y^3-8y^3) $$ $$ = 72x^3 - 60 x^2y + 102xy^2+ 19y^3 $$ The result is correct.