Transfer function / state space model of RLC series circuit and simulation by Python

Overview

Control engineering study notes. The second.

Last time finds the transfer function / state space model from the equation of motion of the mass point of the spring-mass damper system, and the Python library of the control system "[[ I simulated using "Python Control Systems Library" (https://python-control.readthedocs.io/en/0.8.3/index.html).

This time, for ** RLC series circuit **, we will find the transfer function / state space model from the circuit equation and simulate it using Python.
![2020-04-28_09h00_39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/152805/f10d0f88-eba1-d2ca-9ebc-75858504a7d3. png)

Circuit equation

Normally, the circuit equation is formulated using ** current ** $ i (t) $ as follows.

v_{i}(t) = Ri(t) + L\frac{\mathrm{d}}{\mathrm{dt}}i(t) + \frac{1}{C}\int^{t}_{0} i(\tau)\,\mathrm{d} \tau

However, here, in order to correspond to the dynamical system of previous, ** charge ** $ q (t) $ using the following relationship. I will write the circuit equation using.

i(t) = \frac{\mathrm{d}}{\mathrm{dt}}q(t)

The circuit equation with the charge $ q (t) $ is as follows.

v_{i}(t) = R\frac{\mathrm{d}}{\mathrm{dt}}q(t) + L\frac{\mathrm{d}^2}{\mathrm{dt}^2}q(t) + \frac{1}{C}q(t)

Organize the formula by changing the derivative to dot notation.

L \ddot{q}(t) + R \dot{q}(t) + \frac{1}{C}q(t) = v_{i}(t)

This has the same form as the equation of motion of the mass point with one degree of freedom (below) in the spring-mass damper system shown in previous. You can see that.

m\ddot{y}(t) + c\dot{y}(t) + ky(t) = f(t)

Model the system with a transfer function

In the model of mechanics, the transfer function was calculated with the force $ f (t) $ as the ** input ** to the system and the displacement $ y (t) $ as the ** output ** from the system.

If it corresponds to that, the transfer function is obtained by using the input voltage $ v \ _i (t) $ as the ** input ** for the system and the charge $ q (t) $ as the ** output ** from the system. However, it is not easy to directly observe (measure) the ** charge ** $ q (t) $ ** **. Also, in reality, it is the voltage of each element that is of interest. Therefore, ** in the case of an electric circuit, the transfer function is calculated using the voltage as the output **.

Here, the input voltage $ v \ i (t) $ is the ** input ** $ u (t) $ to the system, the voltage across the capacitor $ v {C} (t) $ is the system ** output **. Find the transfer function with $ y (t) $.

Input \, \, \, u (t) = v_ {i} (t)
Output \, \, \, y (t) = v_ {C} (t) = \ frac {1} {C} q (t)

Make the above output an expression for $ q (t) $.

q(t) = Cy(t)

Change this to the above circuit equation $ L \ ddot {q} (t) + R \ dot {q} (t) + \ frac {1} {C} q (t) = v_ {i} (t) $ Substitute to get:

LC \ddot{y}(t) + RC \dot{y}(t) + y(t) = u(t)

Laplace transforming this to find the system's ** transfer function ** $ G (s) = Y (s) / U (s) $ (at this time, $ \ dot {y} (Assuming 0) = 0 $ and $ y (0) = 0 $).

LCs^2Y(s) + RCsY(s) + Y(s) = U(s)
\big(LCs^2+ RC s + 1\big) Y(s) = U(s)
G(s) = \frac{Y(s)}{U(s)} = \frac{1}{LC s^2 + RC s + 1}

In the dynamic model, the transfer function with force as input and displacement as output, mass as $ m $, damping coefficient as $ c $, and spring constant as $ k $ is as follows. You can see that both the dynamical system and the circuit system have very similar shapes.

G(s) = \frac{Y(s)}{F(s)} = \frac{1}{m s^2 + c s + k}

Modeling / simulation with Python

Use Python's ** Python Control Systems Library ** for simulation. Set the transfer function to model the system and simulate the ** step response **.

In the dynamical system, we simulated the ** impulse response ** to observe the displacement when a force is applied to the mass point for a moment, but in the electric circuit, the input voltage $ v_ {i} (t) = 1 \ Since it is more natural to observe the capacitor voltage $ v_ {C} $ when, (t \ ge0) $ is set, let's look at the ** step response **.

Preparation (Google Colab.environment)


!pip install --upgrade sympy
!pip install slycot
!pip install control

Step response simulation (transfer function)


import numpy as np
import control.matlab as ctrl
import matplotlib.pyplot as plt

R = 2       #Resistance value
L = 1e-3    #Inductance
C = 10e-6   #Capacitance

sys = ctrl.tf((1),(L*C,R*C,1))
print(sys)

t_range = (0,0.01)
y, t = ctrl.step(sys, T=np.linspace(*t_range, 400))

plt.figure(figsize=(7,4),dpi=120,facecolor='white')
plt.hlines(1,*t_range,colors='gray',ls=':')
plt.plot(t,y)
plt.xlim(*t_range)
plt.ylim(bottom=0)
plt.show()

Execution result


      1
------------
s^2 + s + 10

f1.png

Assuming that the input voltage is $ v_ {i} (t) = 1 , (t \ ge0) $, the capacitor voltage $ v_ {C} $ oscillates first and finally $ 1 , \ [\ You can see that it settles in mathrm {V} ] $.

Modeled with a state space model

Describe your system as a state-space model $ \ mathcal {P} $ as follows:

python


\mathcal{P}\,:\,\left\{
\begin{array}{l}
\dot{\boldsymbol{x}} = \mathbf{A}\boldsymbol{x} + \mathbf{B}\boldsymbol{u}&Equation of state\\
\boldsymbol{y} = \mathbf{C}\boldsymbol{x} + \mathbf{D}\boldsymbol{u}&Output equation (observation equation)
\end{array}
\right.

As a preparation, the circuit equation $ LC \ ddot {y} (t) + RC \ dot {y} (t) + y (t) = u (t) $, $ \ ddot {y} (t) = .. Transform it into .$.

\ddot{y}(t) = -\frac{1}{LC} y(t)- \frac{R}{L} \dot{y}(t) + \frac{1}{LC} u(t)

As $ x \ _1 (t) = y (t) $, $ x \ _2 (t) = \ dot {y} (t) $, ** state ** $ \ boldsymbol {x} = \ [x \ _1 , , x \ _2] ^ T $ is set.

From this, $ \ dot {x} \ _1 (t) = \ dot {y} (t) = x \ _2 $. Also, $ \ dot {x} \ _2 (t) =-\ frac {1} {LC} y (t)-\ frac {R} {L} \ dot {y} (t) + \ frac {1} {LC} u (t) $.

From the above, the following ** equation of state ** can be obtained.

python


\left[
    \begin{array}{c}
      \dot{x}_1 \\
      \dot{x}_2
    \end{array}
  \right]
=\left[
    \begin{array}{cc}
      0 & 1  \\
      -\frac{1}{LC} & -\frac{R}{L} 
    \end{array}
  \right]
  \left[
    \begin{array}{c}
      x_1 \\
      x_2
    \end{array}
  \right]
+ \left[
    \begin{array}{c}
      0 \\
      \frac{1}{LC}
    \end{array}
  \right] u

python


\dot{\boldsymbol{x}} 
=\left[
    \begin{array}{cc}
      0 & 1  \\
      -\frac{1}{LC} & -\frac{R}{L} 
    \end{array}
  \right]
\boldsymbol{x} + \left[
    \begin{array}{cc}
      0 \\
      \frac{1}{LC}
    \end{array}
  \right] u
\dot{\boldsymbol{x}} =\mathbf{A}\boldsymbol{x} + \mathbf{B} u

Also, obtain the following ** output equation ** (observation equation) (** capacitor voltage ** $ v_ {C} $ corresponding state $ x_1 $ and ** current ** corresponding $ Cx \ _2 (= C \ dot {y} (t) = C \ dot {v} _ {C} (t) = C \ big (\ frac {1} {C} \ dot {q} (t) \ big) = \ dot {q} (t) = i (t) ,) Observing $).

python


y = \left[
    \begin{array}{cc}
      1 & 0\\
      0 & C\\
    \end{array}
  \right]   \left[
    \begin{array}{c}
      x_1 \\
      x_2
    \end{array}
  \right]
y =\mathbf{C}\boldsymbol{x} + \mathbf{D} u

However, $ \ mathbf {D} = 0 $.

Modeling / simulation with Python

Simulate ** step response ** in the state space model.

Preparation for using Japanese in graphs (Google Colab.environment)


!pip install japanize-matplotlib

Simulation of step response (state space model)


import numpy as np
import control.matlab as ctrl
import matplotlib.pyplot as plt
import japanize_matplotlib

R = 2       #Resistance value
L = 1e-3    #Inductance
C = 10e-6   #Capacitance

A = [[0,1],[-1/(L*C), -R/L]]
B = [[0],[1/(L*C)]]
C = [[1,0],[0,C]]
D = 0

sys = ctrl.ss(A,B,C,D)
#print(sys)

t_range = (0,0.01)
y, t = ctrl.step(sys, T=np.linspace(*t_range, 400))

fig, ax = plt.subplots(nrows=2, figsize=(7,5),dpi=120,sharex='col')
fig.patch.set_facecolor('white') 
fig.subplots_adjust(hspace=0.1)  
for i,s in enumerate(['Capacitor voltage','Current']) :
  ax[i].hlines(0,*t_range,colors='gray',ls=':')
  ax[i].plot(t,y[:,i])
  ax[i].set_ylabel(s)
  ax[i].set_xlim(*t_range)
plt.show()

f2.png

Recommended Posts

Transfer function / state space model of RLC series circuit and simulation by Python
Transfer function / state space model of spring / mass / damper system and simulation by Python
[Control engineering] Calculation of transfer functions and state space models by Python
I implemented "Basics of Time Series Analysis and State Space Model" (Hayamoto) with pystan
Custom state space model in Python
Explanation of production optimization model by Python
[Scientific / technical calculation by Python] Fitting by nonlinear function, equation of state, scipy
Implementation of particle filters in Python and application to state space models
Easy introduction of python3 series and OpenCV3
Implement a model with state and behavior (3) --Example of implementation by decorator
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Basic operation of Python Pandas Series and Dataframe (1)
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
Python implementation of CSS3 blend mode and talk of color space
[Python3] Call by dynamically specifying the keyword argument of the function