mirror of
https://github.com/gabrielkheisa/control-system.git
synced 2025-08-21 11:31:07 +00:00
Add new assignment
This commit is contained in:
229
Assignment 3 - Routh Table/442385_Nanda_Sisken_Assign_3.ipynb
Normal file
229
Assignment 3 - Routh Table/442385_Nanda_Sisken_Assign_3.ipynb
Normal file
@@ -0,0 +1,229 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"id": "aDtFkeqAvLX2"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Import Libraries\n",
|
||||
"import numpy as np\n",
|
||||
"import pandas as pd"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"id": "Letb52yp47Gb"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Define RouthStability Class \n",
|
||||
"class RouthStability():\n",
|
||||
"\n",
|
||||
" ## constructor takes string of coefficiens from polynomial\n",
|
||||
" def __init__(self, den):\n",
|
||||
" self.den = np.array([float(item) for item in den.split()])\n",
|
||||
" self.deg = len(self.den)\n",
|
||||
"\n",
|
||||
" ## append k as constant in array\n",
|
||||
" def set_k(self, k):\n",
|
||||
" self.den = np.append(self.den, float(k))\n",
|
||||
" self.deg += 1\n",
|
||||
"\n",
|
||||
" ## generate routh table and print its state\n",
|
||||
" def calc_routh(self):\n",
|
||||
" height = (self.deg+1)//2\n",
|
||||
" arr = np.zeros((height + 2,height))\n",
|
||||
" for index in range(self.deg):\n",
|
||||
" if index % 2 == 0:\n",
|
||||
" arr[0][index//2] = self.den[index]\n",
|
||||
" else:\n",
|
||||
" arr[1][(index-1)//2] = self.den[index]\n",
|
||||
"\n",
|
||||
" for i in range(2, height+2):\n",
|
||||
" for j in range(height-1):\n",
|
||||
" arr[i][j] = (arr[i-1][0]*arr[i-2][j+1] - arr[i-2][0]*arr[i-1][j+1])\n",
|
||||
" arr[i][j] += 0\n",
|
||||
" try:\n",
|
||||
" arr[i][j] /= arr[i-1][0]\n",
|
||||
" except ZeroDivisionError:\n",
|
||||
" arr[i][j] = 0\n",
|
||||
" \n",
|
||||
" self.df = pd.DataFrame(arr)\n",
|
||||
" self.show_tab()\n",
|
||||
" if self.is_stable() == True:\n",
|
||||
" print(\"SYSTEM IS STABLE\")\n",
|
||||
" else:\n",
|
||||
" print(\"SYSTEM IS UNSTABLE\")\n",
|
||||
"\n",
|
||||
" ## print generated route table\n",
|
||||
" def show_tab(self):\n",
|
||||
" print(self.df)\n",
|
||||
"\n",
|
||||
" ## return generated route DataFrame\n",
|
||||
" def get_table(self):\n",
|
||||
" return self.df\n",
|
||||
"\n",
|
||||
" ## return system stability as boolean\n",
|
||||
" def is_stable(self):\n",
|
||||
" flag = True\n",
|
||||
" for item in self.df[0]:\n",
|
||||
" if item < 0: flag = False\n",
|
||||
" return flag\n",
|
||||
" \n",
|
||||
" ## return the value of a polynomial\n",
|
||||
" def get_poly(self, x):\n",
|
||||
" total = 0\n",
|
||||
" for i in range(self.deg):\n",
|
||||
" total += self.den[self.deg-i-1]*(x**i)\n",
|
||||
" print(total)\n",
|
||||
" return total"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "Je19fGrvtOu4",
|
||||
"outputId": "bcf1f997-2fa1-4838-eac7-3aa5156d4a43"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your polynomial: 1 3 5 7\n",
|
||||
"Enter your K: 9\n",
|
||||
" 0 1 2\n",
|
||||
"0 1.000000 5.0 9.0\n",
|
||||
"1 3.000000 7.0 0.0\n",
|
||||
"2 2.666667 9.0 0.0\n",
|
||||
"3 -3.125000 0.0 0.0\n",
|
||||
"4 9.000000 -0.0 0.0\n",
|
||||
"SYSTEM IS UNSTABLE\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# First Testing\n",
|
||||
"den = input(\"Enter your polynomial: \")\n",
|
||||
"k_in = input(\"Enter your K: \")\n",
|
||||
"\n",
|
||||
"rs = RouthStability(den)\n",
|
||||
"rs.set_k(k_in)\n",
|
||||
"rs.calc_routh()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "kFgcxNcscafI",
|
||||
"outputId": "e67322ca-5a85-4434-a690-655b07c9404a"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your polynomial: 11 15 19 21\n",
|
||||
"Enter your K: 29\n",
|
||||
" 0 1 2\n",
|
||||
"0 11.000000 19.0 29.0\n",
|
||||
"1 15.000000 21.0 0.0\n",
|
||||
"2 3.600000 29.0 0.0\n",
|
||||
"3 -99.833333 0.0 0.0\n",
|
||||
"4 29.000000 -0.0 0.0\n",
|
||||
"SYSTEM IS UNSTABLE\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Second Testing\n",
|
||||
"den = input(\"Enter your polynomial: \")\n",
|
||||
"k_in = input(\"Enter your K: \")\n",
|
||||
"\n",
|
||||
"rs = RouthStability(den)\n",
|
||||
"rs.set_k(k_in)\n",
|
||||
"rs.calc_routh()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"base_uri": "https://localhost:8080/"
|
||||
},
|
||||
"id": "jaif_A4_hfeJ",
|
||||
"outputId": "a231e081-6ff2-4e13-db18-759225fcd948"
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Enter your polynomial: 1.2 6.78 11.11\n",
|
||||
"Enter your K: 3.141\n",
|
||||
" 0 1\n",
|
||||
"0 1.200000 11.110\n",
|
||||
"1 6.780000 3.141\n",
|
||||
"2 10.554071 0.000\n",
|
||||
"3 3.141000 0.000\n",
|
||||
"SYSTEM IS STABLE\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Third Testing\n",
|
||||
"den = input(\"Enter your polynomial: \")\n",
|
||||
"k_in = input(\"Enter your K: \")\n",
|
||||
"\n",
|
||||
"rs = RouthStability(den)\n",
|
||||
"rs.set_k(k_in)\n",
|
||||
"rs.calc_routh()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "SjkQCR9chk6P"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"colab": {
|
||||
"collapsed_sections": [],
|
||||
"provenance": []
|
||||
},
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3.8.10 64-bit",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.8.10"
|
||||
},
|
||||
"vscode": {
|
||||
"interpreter": {
|
||||
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
|
||||
}
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
134
Assignment 3 - Routh Table/README.md
Normal file
134
Assignment 3 - Routh Table/README.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# routh_table
|
||||
This dir is belong to Control System class contains with Automated Routh Table Calculator based on Python. This code 100% original made by my hand :), please leave some notes if you're going to use it. Thanks!
|
||||
|
||||
## Libraries
|
||||
Libraries that used in this program is ```numpy``` and ```pandas```. ```numpy``` works to define and perform array while ```pandas``` is the final form after ```numpy.array``` to simplify the presentation. They imported by write..
|
||||
```
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
```
|
||||
|
||||
## RouthStability Class
|
||||
This class contains lots of procedures to simplify our Routh Stability Table Generator process.
|
||||
```
|
||||
def __init__(self, den):
|
||||
self.den = np.array([float(item) for item in den.split()])
|
||||
self.deg = len(self.den)
|
||||
```
|
||||
The constructor ```__init__``` takes string of coefficiens from polynomial, extract the number, and load into class variable. It also define ```self.deg``` variable to save array's length, reducing number to calling ```len()``` function
|
||||
|
||||
```
|
||||
def set_k(self, k):
|
||||
self.den = np.append(self.den, float(k))
|
||||
self.deg += 1
|
||||
```
|
||||
This function only takes one number from user and append it to ```self.den``` which defined as gain (constant). Also ```self.deg``` will increase by one
|
||||
|
||||
```
|
||||
def calc_routh(self):
|
||||
height = (self.deg+1)//2
|
||||
arr = np.zeros((height + 2,height))
|
||||
for index in range(self.deg):
|
||||
if index % 2 == 0:
|
||||
arr[0][index//2] = self.den[index]
|
||||
else:
|
||||
arr[1][(index-1)//2] = self.den[index]
|
||||
|
||||
for i in range(2, height+2):
|
||||
for j in range(height-1):
|
||||
arr[i][j] = (arr[i-1][0]*arr[i-2][j+1] - arr[i-2][0]*arr[i-1][j+1])/arr[i-1][0]
|
||||
arr[i][j] += 0
|
||||
|
||||
self.df = pd.DataFrame(arr)
|
||||
self.show_tab()
|
||||
if self.is_stable() == True:
|
||||
print("SYSTEM IS STABLE")
|
||||
else:
|
||||
print("SYSTEM IS UNSTABLE")
|
||||
```
|
||||
```calc_routh(self)``` as the core process of this class contains initialization and process about Routh Stability Process. Firstly, it define an empty zero (basically it filled with zeros) and iteratively being inserted by ```self.den``` (refering to Routh Table principle). After that, each cell will be updated by calculating Routh Table formula. Routh Table defined as ```numpy.ndarray``` and converted to ```pandas.DataFrame``` to simplify the presentation of table. ```self.show_tab()``` is called to print the Routh Table and ```self.is_stable()``` to check system's stability.
|
||||
|
||||
```
|
||||
def show_tab(self):
|
||||
print(self.df)
|
||||
```
|
||||
This function print the Routh Table, should be called only if Routh Table is generated by ```calc_routh```
|
||||
|
||||
```
|
||||
def get_table(self):
|
||||
return self.df
|
||||
```
|
||||
This function return ```pandas.DataFrame``` contained by Routh Table
|
||||
|
||||
```
|
||||
def is_stable(self):
|
||||
flag = True
|
||||
for item in self.df[0]:
|
||||
if item < 0: flag = False
|
||||
return flag
|
||||
```
|
||||
This function check the first column's value from Routh Table. The system is define as stable if and only if all the value is positive, else it's unstable
|
||||
|
||||
```
|
||||
def get_poly(self, x):
|
||||
total = 0
|
||||
for i in range(self.deg):
|
||||
total += self.den[self.deg-i-1]*(x**i)
|
||||
print(total)
|
||||
return total
|
||||
```
|
||||
This function initialize ```x``` value as variable on ```self.den``` polynomial and return the total
|
||||
|
||||
## Testing 1
|
||||
The testing can follow below example:
|
||||
```
|
||||
# First Testing
|
||||
den = input("Enter your polynomial: ")
|
||||
k_in = input("Enter your K: ")
|
||||
|
||||
rs = RouthStability(den)
|
||||
rs.set_k(k_in)
|
||||
rs.calc_routh()
|
||||
```
|
||||
It takes coefficient of polynomial and K from user, insert it into ```RouthStability``` class as constructor parameter, insert the K value, and generate Routh Table. The result can be found below:
|
||||
```
|
||||
Enter your polynomial: 1 3 5 7
|
||||
Enter your K: 9
|
||||
0 1 2
|
||||
0 1.000000 5.0 9.0
|
||||
1 3.000000 7.0 0.0
|
||||
2 2.666667 9.0 0.0
|
||||
3 -3.125000 0.0 0.0
|
||||
4 9.000000 -0.0 0.0
|
||||
SYSTEM IS UNSTABLE
|
||||
```
|
||||
|
||||
## Testing 2
|
||||
```
|
||||
Enter your polynomial: 11 15 19 21
|
||||
Enter your K: 29
|
||||
0 1 2
|
||||
0 11.000000 19.0 29.0
|
||||
1 15.000000 21.0 0.0
|
||||
2 3.600000 29.0 0.0
|
||||
3 -99.833333 0.0 0.0
|
||||
4 29.000000 -0.0 0.0
|
||||
SYSTEM IS UNSTABLE
|
||||
```
|
||||
|
||||
## Testing 3
|
||||
```
|
||||
Enter your polynomial: 1.2 6.78 11.11
|
||||
Enter your K: 3.141
|
||||
0 1
|
||||
0 1.200000 11.110
|
||||
1 6.780000 3.141
|
||||
2 10.554071 0.000
|
||||
3 3.141000 0.000
|
||||
SYSTEM IS STABLE
|
||||
```
|
||||
### Notes
|
||||
Contact nanda.r.d@mail.ugm.ac.id for more information
|
||||
### Links
|
||||
You can access the source code here
|
||||
[github.com/nandard/routh_table.git](https://github.com/nandard/routh_table.git)
|
Reference in New Issue
Block a user