control-system/Assignment 3 - Routh Table/442385_Nanda_Sisken_Assign_3.ipynb
2022-09-12 23:39:09 +07:00

230 lines
6.4 KiB
Plaintext

{
"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
}