System Controlling: Continous to discrete system

Sunday, April 22, 2012

Continous to discrete system

In a real environment we are using discretized system,  so because of this we need to discretize our system. This page is contains a short description about the zoh and an example written in Python using numpy's.

Discretization with zero order hold element on the input

It's given the following continuous-time state space model:
$$\dot{x(t)} = Ax(t) + B u(t)$$$$y(t) = Cx(t) + Du(t)$$
It can be shown that the following discrete-time state space model expresses how the state x evolves along a discrete time axis $$x[k+1] = A_{d}x[k] + B_{d}u[k]$$$$y[k] = C_{d}x[k] + D_{d}u[k]$$
Where: $$A_{d} = e^{ATs}$$ $$B_{d} = \left( \int_{\tau=0}^{Ts}e^{A \tau}d\tau \right)B$$ $$C_{d} = C$$ $$D_{d} = D$$Where \(T_{s}\) is the sampling time
There is a trick how can we calculate the \(A_{d}, B_{d}\) discrete matrices: $$M = \left[ {\begin{array}{cc} A & B\\ 0 & 0 \end{array} } \right]$$ $$\left[ {\begin{array}{cc} M_{11} & M_{12}\\ 0 & I \end{array} } \right] = e^{MT_{s}}$$ After the calculation we can get easily the \(A_{d}, B_{d}\) discrete matrices: $$A_{d} = M_{11}, B_{d} = M_{21}$$ The following code written in Python contains the theory above.
from numpy import hstack , vstack, zeros
from scipy.linalg import expm
from control.matlab import tf2ss, ss2tf

def c2d ( sys , Ts, method = 'ZOH' ) :
    """
    @summary: From continuous time convert discrete time
 
    @param sys:  an instance of the LTI class or a tuple describing the system.
    The following gives the number of elements in the tuple and
    the interpretation:

    2: (num, den)        
    4: (A, B, C, D)
    @param Ts: Sampling time
    @param method: The name of the discretization method
    
    @return: The discrete system    
    """
    if(len(sys) == 2):
        (A, B, C, D) = tf2ss( sys )
    else:
        (A, B, C, D) = sys
 
    n = A.shape[ 0 ]
    nb = B.shape[ 1 ]
    if(method == 'ZOH'):
        ztmp= zeros ( ( nb , n + nb ) ) 
        tmp = hstack ( ( A , B ) ) 
        tmp = vstack ( ( tmp , ztmp ) ) 
        tmp = expm( tmp * Ts )
 
        A = tmp [ 0 : n , 0 : n ]
        B = tmp [ 0 : n , n : n+nb ]
  
  
    sysd = ( A , B , C , D  ) 
    if(len(sys) == 2):
       return ss2tf ( sysd )
    return sysd
Also it's possible to download from the github.

No comments:

Post a Comment