How to change from matrix to polynomial using SYM? Mine results in ERROR - Error using symengine (line 59)

7 views (last 30 days)
Here's my code:
% initializing values
D = sym('D');
g1 = [1 0 1 1];
g2 = [1 1 0 1];
g = [g1;g2]; % 4 by 2 matrix, g1 will be transformed to g_D(1) and g2 => g_D(2) as below
[no_of_g, size_of_g] = size(g);
% setting all g_D = 0
for i=1:no_of_g
g_D(i)=0;
end
for i=1:no_of_g
for j=1:size_of_g
g_D(i)=g_D(i)+ g(i,j)*D^(j-1); % expected to have g_D(1)=1+D^2+D^3 but ERROR
end
end
ERROR shows: The following error occurred converting from sym to double: Error using symengine (line 59) DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use VPA.
Error in test (line 15) g_D(i)=g_D(i)+ g(i,j)*D^(j-1);

Accepted Answer

John D'Errico
John D'Errico on 14 Dec 2015
Edited: John D'Errico on 14 Dec 2015
Easy enough.
g1 = [1 0 1 1];
syms D
D.^(0:3)*g1.'
ans =
D^3 + D^2 + 1
  5 Comments
John D'Errico
John D'Errico on 14 Dec 2015
Edited: John D'Errico on 14 Dec 2015
p = D.^(0:3)*g1.';
sym2poly(p)
ans =
1 1 0 1
As a test...
p = D.^(0:3)*flip(g1).'
p =
D^3 + D + 1
sym2poly(p)
ans =
1 0 1 1
smileyz_1990
smileyz_1990 on 14 Dec 2015
Edited: smileyz_1990 on 14 Dec 2015
Apparently I have so much to learn, I mean I never heard of flip!
I tried using poly2sym and coeffs, but didn't work for some reason or I didn't get the results I want.
Anyhoo, you're awesome thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!