Elevar un polinomio al cuadrado

Tengo este polinomio el cual contiene números racionales:
g = [3/2 -1/4 -1/3];
secondT = poly2sym(g); % g pero como polinomio
Lo necesito elevar al cuadrado, pero no encuetro ningúna forma para hacer esto. O sea, puedo elevar a g al cuadrado sin ningún problema:
disp(g.^2);
Pero necesito multiplicar los exponentes de la variable (x). Cuando trato de elevar secondT me da este resultado:
(x/4 - (3*x^2)/2 + 1/3)^2

1 Comment

John D'Errico
John D'Errico on 9 Sep 2023
Edited: John D'Errico on 9 Sep 2023
Sorry, that my high school Spanish is far too long out of date. :( Google translate:
I have this polynomial which contains rational numbers:
g = [3/2 -1/4 -1/3];
secondT = poly2sym(g); % g but as a polynomial
I need to square it, but I can't find any way to do this. That is, I can square g without any problem:
disp(g.^2);
But I need to multiply the exponents of the variable (x). When I try to raise secondT it gives me this result:
(x/4 - (3*x^2)/2 + 1/3)^2

Sign in to comment.

 Accepted Answer

Easy enough.
g = [3/2 -1/4 -1/3];
gsym = poly2sym(g);
gsym^2
ans = 
And as you know, it squares the polynomial, but does not expand it. Nothing stops you from using the symbolic toolbox however.
expand(gsym^2)
ans = 
Could you also have done this without using the symbolic toolbox at all? Well, yes, using conv you can multiply polynomials in double precision rthmetic. But then you will be stuck with doubles, not exact fractional coefficients. If you don't push things too far though, you could have done this...
format rat
conv(g,g)
ans =
9/4 -3/4 -15/16 1/6 1/9
And format rat comes to save the day. Again, don't push things too far, as it has limits.

4 Comments

Thanks! But I don't get the expand() answer: (9*x^4)/4 - (3*x^3)/4 - (15*x^2)/16 + x/6 + 1/9
Where - (3*x^3)/4 - (15*x^2)/16 + x/6 came from? I mean, (9*x^4)/4 and 1/9 are 3*x^2/2 and 1/3 raised to the second power, but what are the other terms?
I'm sorry. but you said you wanted to square the polynomial. That is the square of the polynomial you gave. Squaring a polynomial does not just square the individual terms in that polynomial. For example, squaring the polynomial x+1 is not going to generate x^2 + 1^2.
syms x
expand((x+1)^2)
ans = 
And as expected, we have a linear term appear. Again, that is just mathematics. You might as well argue that
(1 + 2)^2 == 1^2 + 2^2
and surely you would be wrong, because the last time I checked, 9 is not equal to 5. So I am not sure what is your question.
If you really just want to square the coefficients of the polynomial, and NOT generate those extra terms, then you can do so of course. But don't say you are trying to square the polynomial, because you are NOT.
You could do this:
g = [3/2 -1/4 -1/3];
format rat
g.^2
ans =
9/4 1/16 1/9
Again, that is NOT the square of the polynomial, but something completely different.
sympref('PolynomialDisplayStyle', 'descend');
syms x c2 c1 c0
gsym = c2*x^2 + c1*x + c0
gsym = 
gsym^2
ans = 
expand(ans)
ans = 
so the -(3*x^3)/4 is 2 * (3/2) * (-1/4)
Thanks man

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023a

Asked:

on 9 Sep 2023

Commented:

on 9 Sep 2023

Community Treasure Hunt

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

Start Hunting!