control-system/442385_Nanda_Sisken_Assign_3.ipynb
2022-09-08 03:07:03 +07:00

210 lines
5.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 132,
"metadata": {
"id": "aDtFkeqAvLX2"
},
"outputs": [],
"source": [
"# Import Libraries\n",
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 133,
"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])//2\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": 134,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Je19fGrvtOu4",
"outputId": "c99b1159-f745-4c3f-c012-c2f464b8edb0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your polynomial: 1 2 3 4 5\n",
"Enter your K: 6\n",
" 0 1 2\n",
"0 1.0 3.0 5.0\n",
"1 2.0 4.0 6.0\n",
"2 1.0 2.0 0.0\n",
"3 0.0 3.0 0.0\n",
"4 -2.0 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": 136,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kFgcxNcscafI",
"outputId": "25aadcc4-6a29-4c16-c992-46d95a56cbfb"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your polynomial: 12 56 37 80\n",
"Enter your K: 17\n",
" 0 1 2\n",
"0 12.0 37.0 17.0\n",
"1 56.0 80.0 0.0\n",
"2 556.0 476.0 0.0\n",
"3 8912.0 0.0 0.0\n",
"4 2121056.0 0.0 0.0\n",
"SYSTEM IS STABLE\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": 137,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jaif_A4_hfeJ",
"outputId": "0b387e7e-efbf-4f5c-fb45-d98489bbba3e"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter your polynomial: 1.2 6.81 7.31\n",
"Enter your K: 3.141\n",
" 0 1\n",
"0 1.20 7.310\n",
"1 6.81 3.141\n",
"2 23.00 0.000\n",
"3 36.00 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()"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}