MATLAB: Error Conversion to double from sym is not possible

My code is defined as follows:
U = sym('u', [101 1]);
syms u P Q
P(1,1) = 1;
Q(1,1) = 0;
for k = 1:100
P(k+1,1) = .05*(-.3*P(k,1) +.65*Q(k,1)+U(k,1))+P(k,1);
Q(k+1,1) = .05*(-.65*Q(k,1) + .3*P(k,1)-U(k,1))+Q(k,1);
end
plot(P,U)
I know that the subs() function may be a solution to the conversion for symbolic functions so I also tried a "plot(P, double(subs(U,P)))" command instead, but this also pops up as an error. Does anyone know of another way for me to possibly plot this symbolic matrix?

Best Answer

  • Change
    P(1,1) = 1;
    Q(1,1) = 0;
    to
    P(1,1) = sym(1);
    Q(1,1) = sym(0);
    otherwise the arrays will be numeric and not able to store symbolic data.
    I see that you tried subs(U,P) but unfortunately for you, all of your P and Q are defined in terms of the undefined symbols u1, u2, and so on. Your P(2) is defined in terms of U(1), your P(3) is defined in terms of P(2) and U(2) and so P(3) is defined in terms of U(1) and U(2), and so on, with your final P(101) being defined in terms of all of the U(1:100).
    Perhaps your intent is that U(1) will gain a numeric value, and that U(2) will gain a numeric value because U(1) has gained a numeric value, and U(3) would gain a numeric value because now U(2) has gained a numeric value, and so on, but subs() does a simultaneous substitution, so at best after one step, U(1) would be given a numeric value and U(2) would still be in terms of the symbol U(1) and so on. If you are trying to define U iteratively in such a way that by the end each U will have a numeric value, you cannot do it through simultaneous substitution with subs(): you would need to do the assignment iteratively just like you do for P and Q.