MATLAB: Trying to create an array

simple problem

So I have this array with 2 conjugate pairs and 1 real value
sp =
-0.0895 + 0.9901i
-0.0895 – 0.9901i
-0.2342 + 0.6119i
-0.2342 – 0.6119i
-0.2895 + 0.0000i
I'm trying to create another array called P(k)
with the 3 equations P1, P2,and P3
The equation for the conjugate pairs is
P(k) = (s – sp1(k)) * (s – sp2(k))
where sp1 is the first complex number of the pair and sp2 is the second complex number of the pair
Ex:
sp1 = -0.0895 + 0.9901i
sp2 = -0.0895 – 0.9901i
The equation for real numbers
P(k) = (s – sp(k))
where sp(k) is that given real value
Ex:
sp = -0.2895 + 0.0000i
The answer is
P = [s^2 + 0.1789s +0.9883,
s^2 +0.4684s +0.4293,
s + 0.2895]
I TRIED THIS:
syms s
Ns = 3
%Complex Roots
X = sp(imag(sp)~=0);
S = isreal(X)
%Real Roots
Y = sp(imag(sp)==0);
T = isreal(Y)
for k = 1:Ns
if T == 0
P(k) = vpa(simplify((s-X(2*k-1))*(s-X(2*k))))
else
P(k) = vpa(simplify(s-Y(k-2)))
end
end

Best Answer

Try this:
sp = [
-0.0895 + 0.9901i
-0.0895 - 0.9901i
-0.2342 + 0.6119i
-0.2342 - 0.6119i
-0.2895 + 0.0000i];
for k = 1:2:numel(sp)
ixr = unique(min([1 2]+(k-1),numel(sp)))
p = poly(sp(ixr))
if all(p ~= 0)
ki = ceil(k/2);
P{ki,:} = p;
end
end
syms s
for k = 1:numel(P)
Ps(k,:) = poly2sym(P{k},s);
end
Ps = vpa(Ps,4)
producing:
Ps =
s^2 + 0.179*s + 0.9883
s^2 + 0.4684*s + 0.4293
s + 0.2895
.