Search code examples
matlab

Get symbolic expression from residue result


Using the residue function, I get the result as some vector variables:

[r,p,k] = residue(b,a)

Is there any way to build a symbolic expression (on the variable of my choice, e.g. 'x') from the return result, like:

eq = residue2sym(r,p,k)
pretty(eq)

Solution

  • In the end I implemented it myself:

    function eq = residue2sym( R,P,K )
    
    syms s
    
    eq = 0;
    
    lastR = NaN;
    lastP = NaN;
    multiplicityCounter = 1;
    for i = 1:length(R)
        rc = R(i);
        pc = P(i);
    
        if(~isnan(lastP) )
            if(pc == lastP && abs(lastR) < 1e-15)   % Quando existe multiplicidade, o R correspondente ao primeiro P
                                                    % da multiplicidade deveria
                                                    % ser 0, mas estranhamente
                                                    % aparece como -4.4409e-16.
                                                    % O que deveria ser uma
                                                    % comparação com 0 então
                                                    % vira uma comparação para
                                                    % um módulo bem pequeno
                multiplicityCounter = multiplicityCounter + 1;
            else
                eq = eq + lastR/(s - lastP)^multiplicityCounter;
                multiplicityCounter = 1;
            end
        end
        lastR = rc;
        lastP = pc;
    end
    if(~isnan(lastP))
        eq = eq + lastR/(s - lastP)^multiplicityCounter;
    end
    
     if(~isempty(K))
         eq = eq + sum(s.^(0:length(K)));
     end
    
    eq = vpa( eq, 2);
    
    end