How to create symbolic polynomials from a coefficient vector and symbolic vector?

2 views (last 30 days)
Hello,
I have a coefficient column vector looking something like
x = [1 2 3]'
that aligns with the polynomial p(z) = x_0 + x_1*z + x_2*z^2 + ... + x_n-1*z^(n-1). My question is, how would one create a symbolic vector, something like
p = [1 z z^2]
so that when I take the matrix product
p*x
and print it I get a 1x1 "matrix" of the expression 1 + 2z + 3z^2?
Furthermore, how can I generalize the creation of p to extend for arbitrary powers z^3, z^4, ...?

Accepted Answer

John D'Errico
John D'Errico on 30 Sep 2018

As Walter shows, you can do it using provided tools easily enough. Just to understand how things work, you can also do it directly.

x = [1 2 3]';
syms z
p = z.^(0:(numel(x)-1));
polynom = p*x
polynom =
3*z^2 + 2*z + 1

Note that the symbolic toolbox has chosen to reorder the terms.

  2 Comments
Jonathan Lee
Jonathan Lee on 30 Sep 2018
Do you know of any way to maintain the floating-point-ness of the coefficients? (i.e. 0.5124243523423 instead of 1395801/12985102)
Whenever I use Walter's or your solution, it always converts x into a symbolic array.
John D'Errico
John D'Errico on 30 Sep 2018
Edited: John D'Errico on 30 Sep 2018
You never said that was important, but you said you WANTED a symbolic result. syms converts floating point numbers into rational versions of those same numbers. That way, syms maintains super high precision, rather than using floating point arithmetic.
To convert those rational forms back into floating point numbers, just use vpa on the result.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 30 Sep 2018
poly2sym(fliplr(x.'), z)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!