how to express vector in the form of a polynomial?
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Iori Yagami Kusanagi
el 18 de Oct. de 2018
Respondida: Guillaume
el 18 de Oct. de 2018
I have a vector [0 0 7 7 0 0 1], and I need display it 7x^3+7x^4+x^7. Can someone help me please?
0 comentarios
Respuesta aceptada
madhan ravi
el 18 de Oct. de 2018
Editada: madhan ravi
el 18 de Oct. de 2018
P= poly2sym(sym([0 0 7 7 0 0 1]))
P = poly2sym(sym([7 0 0 7 7 0 0 1])) %this will show your desired result
Remark: from the right constant and towards left x raised to the power
2 comentarios
Walter Roberson
el 18 de Oct. de 2018
Even more compact:
P = poly2sym([0 0 7 7 0 0 1])
It is not necessary to sym() the numeric values first.
Note that there is no control over the order of the symbolic expression when you do this. In a simple polynomial, the order will be by descending power. In some cases involving ratios of polynomials, the order can be a bit tricky to figure out.
madhan ravi
el 18 de Oct. de 2018
Editada: madhan ravi
el 18 de Oct. de 2018
Oh thank you sir Walter , learned something new.
Más respuestas (1)
Guillaume
el 18 de Oct. de 2018
The first problem with your vector is that it doesn't appear you've left any room for the polynomial constant. A polynomial of degree 7 has 8 coefficients, not 7.
Secondly, the order of the coefficients in your vector is the complete opposite of matlab's convention. That means that none of the functions in matlab that deal with polynomial (e.g. polyval) will work with your vector. So, first thing I'd recommend is swap around your vector, and add the constant term
p = [1 0 0 7 7 0 0 0] %corresponds to x^7 + 7*x^4 + 7*x^3
Evaluating your polynomial at any value is then easy, e.g what are the value of x^7+7*x^4+7*x^3 at -1, 0, 1:
>>polyval(p, [-1 0 1])
ans =
-1 0 15
-1, 0, and 15 respectively.
Now, as for building your 7x^3+7x^4+x^7, note that this will only be useful for display, not for any calculation. As far as I know there are no functions in matlab to build a string representation of a polynomial (maybe in the symbolic toolbox?), it's not too hard to make yours:
function str = poly2str(p)
%p: polynomial vector, using matlab ordering, p(end) is the constant
exponents = numel(p)-1:-1:0;
exponents = exponents(p ~= 0); %don't use 0 coefficients
coefficients = p(p ~= 0);
scoeffs = compose("%g*", coefficients);
scoeffs(coefficients == 1) = ""; %no need for "1" coefficients
sexps = compose("x^%d", exponents);
sexps(exponents == 1) = "x"; %no need for "x^1"
sexps(exponents == 0) = ""; %no need for "x^0"
str = strjoin(compose("%s%s", scoeffs', sexps'), "+");
end
Usage:
>> poly2str(p)
ans =
"x^7+7*x^4+7*x^3"
>> poly2str([0.5 -1 1 5])
ans =
"0.5*x^3+-1*x^2+x+5*"
The function could be refined so as not to have +-. An exercise left to the reader...
0 comentarios
Ver también
Categorías
Más información sobre Polynomials en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!