Search code examples
matlabsymbolic-math

How to evaluate a matlab symbolic expression properly?


Lets say I have a matrix like this:

syms p;
K = [p^2+3 0; 2 5*p];
p_initial = 2;

Whats the proper/fastest way of getting K(p_initial), that is the resulting matrix if I insert 2 for p. Further, I want the resulting matrix to be of type double, not of symbolic type.

Thanks in advance


Solution

  • Use subs to substitute variables in symbolic expressions

    subs(K,'p',p_initial)
    
    ans =
    
    [ 7,  0]
    [ 2, 10]