If you have studied control, I would like to have the program actually calculate the familiar ** unit step response ** and output it to a graph.
** Subject to this article **
--Those who are studying the control field --Those who are interested in simulation using Python --Python beginners
\frac{d^{2}x(t)}{dt^{2}} + \alpha \frac{dx(t)}{dt} + \beta x(t) = u(t)
This time, I would like to create a program for the system represented by the above differential equation. Since the calculation is complicated and troublesome in such a mathematical model, in the control field, the Laplace transform is used to convert the time domain to the frequency domain before the calculation. However, if you let the computer calculate it, it will be derived in an instant.
Python is installed by default on Mac (OS X / macOS). If it is too old, you may need to update it, but we will omit it here.
Python uses a package management system called pip
. The package installed here can be read and used from the program side like ʻimport. If you are asked to upgrade, run
pip install --upgrade pip`.
$ pip install numpy
Requirement already satisfied: numpy in /usr/local/lib/python2.7/site-packages
$ pip install scipy
Collecting scipy
Downloading ..... (21.8MB)
.....
Installing collected packages: scipy
Successfully installed scipy-0.18.1
$ pip install matplotlib
Collecting matplotlib
Downloading ..... (11.2MB)
.....
Installing collected packages: ..... pytz, pyparsing, matplotlib
Successfully installed ..... pytz-2016.7 six-1.10.0
Write the following program in your favorite editor and save it with the extension py
.
sample.py
from scipy.integrate import odeint
from math import *
import numpy
import matplotlib.pyplot as plot
#Let's change
alpha = 17
beta = 777
def derivative(x, t):
dx = [ x[1], - beta * x[0] - alpha * x[1] + 1.0 ]
return dx
x_init = [0.0, 0.0]
time = numpy.linspace(0.0, 5.0, 10000)
x = odeint(derivative, x_init, time)
#Output part
plot.figure()
plot.plot(time, x[:, 0])
plot.show()
If you pass the program file name as an argument to python, it will be executed.
$ ls
sample.py
$ python sample.py
Recommended Posts