How to get the factor format of a polynomial

262 views (last 30 days)
Hi,
I want to get the factorized format of a symbolic polynomial.
I know how to get the factors in symbolic way:
syms x
factor(x.^2-1)
ans =
[ x - 1, x + 1]
This is an array with factors.
But, I want to get as answer something like
(x - 1)*(x + 1)
how could I get it? I remember in ancient versions this was the result.
Do I have to build this expression?
In this case, any sugestion in order to make it?
Thanks in advance.

Accepted Answer

Alan Stevens
Alan Stevens on 17 Apr 2021
syms x
y=x^2-1;
a = factor(y)
a = 
b = a(1)*a(2)
b = 
  2 Comments
Moisés Antonio Fonseca Beltrán
Edited: Moisés Antonio Fonseca Beltrán on 17 Apr 2021
Alan, thanks for your answer.
Logical and simple. I like it!!
Buy I tried it in a polynomial with a scalar factor, and it ignores the factorization.
I show you the problem I found.
clc
syms s
disp('---------------------------------')
disp('Define a polynomial, P:')
P = poly([-3+1j,-3-1j,-4,-5])
disp('Making P as symbolic, symP:')
symP = poly2sym(P)
disp('Extracting the factors of symP:')
symPfactors = factor(symP)
disp('Building the factorized form of symP:')
prod(symPfactors)
disp('It works ;)!!')
disp('---------------------------------')
disp('Defining another polynomial, Q')
Q = 10*[1,1]
disp('Making Q as a symbolic, symQ')
symQ = poly2sym(Q,s)
disp('Extracting the factors of symQ:')
symQfactors = factor(symQ)
disp('Building the factorized form of symQ:')
prod(symQfactors)
disp('Now, it does not :(')
disp('---------------------------------')
disp('Defining another polynomial, R')
R = 10*[1,2,1]
disp('Making R as a symbolic, symR')
symR = poly2sym(R,s)
disp('Extracting the factors of symR:')
symRfactors = factor(symR)
disp('Building the factorized form of symR:')
prod(symRfactors)
disp('Now, it does!!')
disp('---------------------------------')
disp('Defining another polynomial, T')
T = 10*conv([1,1],[1,2,5])
disp('Making T as a symbolic, symT')
symT = poly2sym(T,s)
disp('Extracting the factors of symT:')
symTfactors = factor(symT)
disp('Building the factorized form of symT:')
prod(symTfactors)
disp('It works again!!')
As result, Matlab shows me something like:
---------------------------------
Define a polynomial, P:
P =
1 15 84 210 200
Making P as symbolic, symP:
symP =
x^4 + 15*x^3 + 84*x^2 + 210*x + 200
Extracting the factors of symP:
symPfactors =
[x + 5, x + 4, x^2 + 6*x + 10]
Building the factorized form of symP:
ans =
(x + 4)*(x + 5)*(x^2 + 6*x + 10)
It works ;)!!
---------------------------------
Defining another polynomial, Q
Q =
10 10
Making Q as a symbolic, symQ
symQ =
10*s + 10
Extracting the factors of symQ:
symQfactors =
[10, s + 1]
Building the factorized form of symQ:
ans =
10*s + 10
Now, it does not :(
---------------------------------
Defining another polynomial, R
R =
10 20 10
Making R as a symbolic, symR
symR =
10*s^2 + 20*s + 10
Extracting the factors of symR:
symRfactors =
[10, s + 1, s + 1]
Building the factorized form of symR:
ans =
10*(s + 1)^2
Now, it does!!
---------------------------------
Defining another polynomial, T
T =
10 30 70 50
Making T as a symbolic, symT
symT =
10*s^3 + 30*s^2 + 70*s + 50
Extracting the factors of symT:
symTfactors =
[10, s + 1, s^2 + 2*s + 5]
Building the factorized form of symT:
ans =
10*(s + 1)*(s^2 + 2*s + 5)
Now, it does!!
>>
I tried to not include the scalar factor (10), but, when I try to write it in the final result, Matlab expands the product again, when the polynomial has two factors.
Does Matlab always expand the polynomials with scalar (and two) factors?
If so, should I take apart the scalar factor and build it at the end?
Anyway, now I am using your solution.
Thanks in advance.
Alan Stevens
Alan Stevens on 18 Apr 2021
Yes, it looks like you'll have to deal with the scalar factor by hand.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!