Easy on Mac! Plot of unit step response using Python

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.

1. If you chose Mac, you are right!

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.

2. Install the required packages

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

3. Programming

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()

4. Run

If you pass the program file name as an argument to python, it will be executed.

$ ls
sample.py

$ python sample.py
スクリーンショット 2016-11-11 1.10.31.png

Recommended Posts

Easy on Mac! Plot of unit step response using Python
Handling of python on mac
Using multiple versions of Python on Mac OS X (2) Usage
Environment construction of python3.8 on mac
Using multiple versions of Python on Mac OS X (1) Multiple Ver installation
Video processing using Python + OpenCV on Mac
python on mac
How to build an environment for using multiple versions of Python on Mac
Install Python on Mac
Install Python 3 on Mac
Install Python 3.4 on Mac
Drawing on Jupyter using the plot function of pandas
Build a Python environment on your Mac using pyenv
Minimum memo when using Python on Mac (pyenv edition)
Minimum notes when using Python on Mac (Homebrew edition)
Using NAOqi 2.4.2 Python SDK on Mac OS X El Capitan
Install pygame on python3.4 on mac
Building a Python environment on a Mac and using Jupyter lab
python: Basics of using scikit-learn ①
Update python on Mac to 3.7-> 3.8
Install pandas 0.14 on python3.4 [on Mac]
Notes on installing Python on Mac
Broadcast on LINE using python
Create a virtual environment for python on mac [Very easy]
Using OpenCV with Python @Mac
[Python] I wrote the route of the typhoon on the map using folium
Stream speech recognition using Google Cloud Speech gRPC API on python3 on Mac!
Ansible playbook for setting up Python preferences using pyenv on Mac
Building a Python environment on Mac
If python on mac goes missing
Image capture of firefox using python
Easy encryption of file contents (Python)
Easy installation of OpenCV on RaspberryPi 3+
Introducing Python using pyenv on Ubuntu 20.04
Notes on using MeCab from Python
Preparing python using vscode on ubuntu
Create a Python environment on Mac (2017/4)
Removal of haze using Python detailEnhanceFilter
Python environment construction memo on Mac
Install Python 3.7 Anaconda on MAC, but Python 2
Install python3 on Mac (El Capitan)
Easy visualization using Python but PixieDust
Study on Tokyo Rent Using Python (3-2)
Notes on installing Python using PyEnv
Implementation of desktop notifications using Python
Notes on using rstrip with python.
If Python 3.5.0 installation fails on Mac
Study on Tokyo Rent Using Python (3-3)
Install Python on CentOS using pyenv
(Beginner) Notes on using pyenv on Mac
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
Installation method using the pip command of the Python package (library) Mac environment
[Definitive Edition] Building an environment for learning "machine learning" using Python on Mac
Notes on setting pyenv and python environment using Homebrew on Mac OS Marvericks
Python: Basics of image recognition using CNN
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
Install opencv on Mac using Anaconda Navigator
Automatic collection of stock prices using python
About building GUI using TKinter of Python
Execute Python code on C ++ (using Boost.Python)