Find the transfer function of one degree of freedom system with PythonControl.

Purpose

Formulate the equation of state of a one-degree-of-freedom system (spring mass damper system), and convert the equation of state to a transfer function with Python Control. For comparison, the method and description of obtaining the transfer function from the Laplace transform are also described.

Advance preparation

Install Python Control

Equation of state and transfer function

The equation of state is expressed by the following equation.

\dot{x}(t)=Ax(t)+Bu(t) \\
y(t)=Cx(t)+Du(t)

Laplace transform of the equation of state

sX(s)=AX(s)+BU(s) \\
Y(s)=CX(s)+DU(s)

When you convert the expression,

sX(s)-AX(s)=BU(s) \\
(sI-A)X(s)=BU(s) \\
X(s)={(sI-A)}^{-1} BU(s) \\

The transfer function G (s) is

G(s)=\frac{Y(s)}{U(s)}=\frac{{(sI-A)}^{-1} BU(s) +DU(s)}{U(s)}=(sI-A)^{-1} B +DU

Obtained from.

In Python-Control By inputting the matrix of A, B, C, D into the control.ss2tf function, the transfer function can be obtained according to the above equation. http://python-control.readthedocs.io/en/latest/generated/control.ss2tf.html

Equation of state equation

Banemasu.jpg

m: Mass [kg] k: Spring constant [N / m] c: Viscous damping coefficient [N ・ s / m] f: Force [N]

If you say The above equation of motion is

m\ddot{x}+c\dot{x}+kx=f \\

Will be.

Find the transfer function from the equation of state.

Transform the formula here

m\ddot{x}+c\dot{x}+kx=f \\
m\ddot{x}=-c\dot{x}-kx+f \\
\ddot{x}=-c/m \dot{x}-k/m x+f/m \\

And.

x_{1}=x  \\
x_{2}=\dot{x} \\

If you say

\\
\dot x_{1} ={x_{2}} \\
\dot x_{2}= -c/m \dot{x_{2}}-k/m x_{1}+f/m \\

If you write this in a matrix

\\
\begin{bmatrix}
\dot x_{1}  \\
\dot x_{2}  
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
-k/m & -c/m
\end{bmatrix}
\begin{bmatrix}
x_{1}  \\
x_{2}  
\end{bmatrix}
+
\begin{bmatrix}
0 \\
1/m 
\end{bmatrix}
f

further,

y=If x,\\

y=
\begin{bmatrix}
1 & 0
\end{bmatrix}
\begin{bmatrix}
x_{1}  \\
x_{2}  
\end{bmatrix}

Therefore,


\\
\begin{bmatrix}
\dot x_{1}  \\
\dot x_{2}  
\end{bmatrix}
=
\begin{bmatrix}
0 & 1 \\
-k/m & -c/m
\end{bmatrix}
\begin{bmatrix}
x_{1}  \\
x_{2}  
\end{bmatrix}
+
\begin{bmatrix}
0 \\
1/m 
\end{bmatrix}
f \\
y=
\begin{bmatrix}
1 & 0
\end{bmatrix}
\begin{bmatrix}
x_{1}  \\
x_{2}  
\end{bmatrix}

Compared to the equation of state

x(t)=
\begin{bmatrix}
x_{1}  \\
x_{2}  
\end{bmatrix}

,
u(t)=f
,

A=
\begin{bmatrix}
0 & 1 \\
-k/m & -c/m
\end{bmatrix}
,
B=
\begin{bmatrix}
0 \\
1/m 
\end{bmatrix}
,
C=
\begin{bmatrix}
1 & 0 
\end{bmatrix}
,
D=[0]

Will be.

From the above, The code to find the transfer function with python control is as follows.

-signal_degree_of_freedom_system_1.py

#!/usr/bin/env python
from control.matlab import *
from matplotlib import pyplot as plt
    
def main():
    k=3.0
    m=0.1
    c=0.01
    A = [[0., 1], [-k/m, -c/m]]
    B = [[0.], [1./m]]
    C = [[1., 0.0]]
    D = [[0.]]
    sys1 = ss2tf(A, B, C, D)
    print sys1
   
    bode(sys1)    
    plt.show()
    
if __name__ == "__main__":
  main()

Execution result

$ ./signal_degree_of_freedom_system_1.py
       10
----------------
s^2 + 0.1 s + 30

Figure_1.png

The transfer function is obtained by Laplace transforming the equation of state.

From the equation of state equation, when u (t) = f,

m\ddot{x}(t)+c\dot{x}(t)+kx(t)=u(t) \\

Laplace transform

ms^2X(s)+cX(s)+kX(s)=U(s) \\

Furthermore, if y = x,

ms^2Y(s)+cY(s)+kY(s)=U(s) \\
\frac{Y(s)}{U(s)}=\frac{1}{ms^2+cs+k}

The transfer function is

G(s)=\frac{Y(s)}{U(s)}=\frac{1}{ms^2+cs+k}

From the above, The code to find the transfer function with python control is as follows.

#!/usr/bin/env python
from control.matlab import *
from matplotlib import pyplot as plt

def main():
  k=3.0
  m=0.1
  c=0.01
  num = [0, 0,1] 
  den = [m, c, k]
  sys = tf(num, den) 
  print sys
  bode(sys)    
  plt.show()

if __name__ == "__main__":
  main()

Execution result

$ ./signal_degree_of_freedom_system_2.py 
          1
--------------------
0.1 s^2 + 0.01 s + 3

Figure_1-2.png

Sample code

Sample code is stored below. https://github.com/nnn112358/python-control_test

Recommended Posts

Find the transfer function of one degree of freedom system with PythonControl.
Find the optimal value of a function with a genetic algorithm (Part 2)
Find out the day of the week with datetime
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Find the sum of unique values with pandas crosstab
Find out the location of packages installed with pip
I tried to find the entropy of the image with python
Save the output of GAN one by one ~ With the implementation of GAN by PyTorch ~
I evaluated the strategy of stock system trading with Python.
Check the scope of local variables with the Python locals function.
If you give a list with the default argument of the function ...
Find the numerical solution of the second-order ordinary differential equation with scipy
Let's summarize the degree of coupling between modules with Python code
[Python] A simple function to find the center coordinates of a circle
Winner of the one with the highest degree of similarity to the original story in "The winner of the one who wrote the most stinky Hototogisu"
Find the definition of the value of errno
Find the Levenshtein Distance with python
Achieve Linux/dev/null with Windows system function
Find the inertial spindle and moment of inertia from the inertial tensor with NumPy
Find the general terms of the Tribonacci sequence with linear algebra and Python
Find the minimum value of a function by particle swarm optimization (PSO)