# routh_table This repo 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])//2 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 2 3 4 5 Enter your K: 6 0 1 2 0 1.0 3.0 5.0 1 2.0 4.0 6.0 2 1.0 2.0 0.0 3 0.0 3.0 0.0 4 -2.0 0.0 0.0 SYSTEM IS UNSTABLE ``` ## Testing 2 ``` Enter your polynomial: 12 56 37 80 Enter your K: 17 0 1 2 0 12.0 37.0 17.0 1 56.0 80.0 0.0 2 556.0 476.0 0.0 3 8912.0 0.0 0.0 4 2121056.0 0.0 0.0 SYSTEM IS STABLE ``` ## Testing 3 ``` Enter your polynomial: 1.2 6.81 7.31 Enter your K: 3.141 0 1 0 1.20 7.310 1 6.81 3.141 2 23.00 0.000 3 36.00 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)