Search code examples
matlabsymbolic-math

How to extract Symbolic coefficient of TRANSFER FUNCTION in MATLAB


How to extract coefficient in symbolic math MATLAB.

eg, i have transfer function like

H(S) = (a*S^2 + b*S^ + a*c*S + d*a) / (S^3 + b*c*S^2 + d*S^1 + a*d)

I want to get coefficient of 'S' term in H(s) as vector array like

num = [a b a*c d*a]
den = [1 b*c d a*d]

Solution

  • You can apply numden to extract the numerator and the denominator polynomical expression:

    [numexpr, denexpr] = numden(sym(H))  %// 'sym' makes sure that H is symbolic
    

    and then you can extract the symbolic coefficients of S by using the coeffs command (remember to apply expand on each expression first to obtain their polynomial forms).

    However, note that coeffs returns only the non-zero coefficients. To overcome this issue, I suggest the following:

    %// Extract numerator coefficients
    [numcoef, numpow] = coeffs(expand(numexpr), S);
    num = rot90(sym(sym2poly(sum(numpow))), 2);
    num(num ~= 0) = coeffs(expand(numexpr), S);
    
    %// Extract denominator coefficients
    [dencoef, denpow] = coeffs(expand(denexpr), S);
    den = rot90(sym(sym2poly(sum(denpow))), 2);
    den(den ~= 0) = coeffs(expand(denexpr), S);
    

    P.S: you can also apply sym2polys tool from the MATLAB Exchange on numexpr and denexpr instead.

    Also note that it is more common for the last elements in the coefficient vectors to be associated with the highest powers of S, so the result of this solution will be in reverse order to what you have described in your question.

    Example

    %// Create symbolic function
    syms a b S
    H = b * S / (a + S^2)
    [numexpr, denexpr] = numden(sym(H));
    
    %// Extract numerator coefficients
    [numcoef, numpow] = coeffs(expand(numexpr), S);
    num = rot90(sym(sym2poly(sum(numpow))), 2);
    num(num ~= 0) = coeffs(expand(numexpr), S);
    
    %// Extract denominator coefficients
    [dencoef, denpow] = coeffs(expand(denexpr), S);
    den = rot90(sym(sym2poly(sum(denpow))), 2);
    den(den ~= 0) = coeffs(expand(denexpr), S);
    

    The result is:

    num =
        [0, b]
    
    den =
        [a, 0, 1]