control-system/442385_Nanda_Sisken_Assign_3.ipynb

230 lines
6.4 KiB
Plaintext
Raw Normal View History

2022-09-08 02:59:13 +07:00
{
"cells": [
{
"cell_type": "code",
2022-09-10 15:53:36 +07:00
"execution_count": 1,
2022-09-08 03:07:03 +07:00
"metadata": {
"id": "aDtFkeqAvLX2"
},
"outputs": [],
2022-09-08 02:59:13 +07:00
"source": [
"# Import Libraries\n",
"import numpy as np\n",
"import pandas as pd"
2022-09-08 03:07:03 +07:00
]
2022-09-08 02:59:13 +07:00
},
{
"cell_type": "code",
2022-09-10 15:53:36 +07:00
"execution_count": 4,
2022-09-08 02:59:13 +07:00
"metadata": {
"id": "Letb52yp47Gb"
},
"outputs": [],
"source": [
"# Define RouthStability Class \n",
"class RouthStability():\n",
2022-09-08 03:07:03 +07:00
"\n",
2022-09-08 02:59:13 +07:00
" ## 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",
2022-09-08 03:07:03 +07:00
"\n",
2022-09-08 02:59:13 +07:00
" ## 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",
2022-09-08 03:07:03 +07:00
"\n",
2022-09-08 02:59:13 +07:00
" ## 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",
2022-09-10 15:53:36 +07:00
" arr[i][j] = (arr[i-1][0]*arr[i-2][j+1] - arr[i-2][0]*arr[i-1][j+1])\n",
2022-09-08 02:59:13 +07:00
" arr[i][j] += 0\n",
2022-09-10 15:53:36 +07:00
" try:\n",
" arr[i][j] /= arr[i-1][0]\n",
" except ZeroDivisionError:\n",
" arr[i][j] = 0\n",
2022-09-08 02:59:13 +07:00
" \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",
2022-09-08 03:07:03 +07:00
"\n",
2022-09-08 02:59:13 +07:00
" ## print generated route table\n",
" def show_tab(self):\n",
" print(self.df)\n",
2022-09-08 03:07:03 +07:00
"\n",
2022-09-08 02:59:13 +07:00
" ## return generated route DataFrame\n",
" def get_table(self):\n",
" return self.df\n",
2022-09-08 03:07:03 +07:00
"\n",
2022-09-08 02:59:13 +07:00
" ## 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",
2022-09-08 03:07:03 +07:00
" \n",
2022-09-08 02:59:13 +07:00
" ## 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",
2022-09-10 15:53:36 +07:00
"execution_count": 5,
2022-09-08 02:59:13 +07:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
2022-09-08 03:07:03 +07:00
"id": "Je19fGrvtOu4",
2022-09-10 15:53:36 +07:00
"outputId": "bcf1f997-2fa1-4838-eac7-3aa5156d4a43"
2022-09-08 02:59:13 +07:00
},
"outputs": [
{
"name": "stdout",
2022-09-08 03:07:03 +07:00
"output_type": "stream",
2022-09-08 02:59:13 +07:00
"text": [
2022-09-10 15:53:36 +07:00
"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",
2022-09-08 02:59:13 +07:00
"SYSTEM IS UNSTABLE\n"
]
}
2022-09-08 03:07:03 +07:00
],
2022-09-08 02:59:13 +07:00
"source": [
2022-09-08 03:07:03 +07:00
"# First Testing\n",
2022-09-08 02:59:13 +07:00
"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()"
2022-09-08 03:07:03 +07:00
]
},
{
"cell_type": "code",
2022-09-10 15:53:36 +07:00
"execution_count": 6,
2022-09-08 02:59:13 +07:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kFgcxNcscafI",
2022-09-10 15:53:36 +07:00
"outputId": "e67322ca-5a85-4434-a690-655b07c9404a"
2022-09-08 02:59:13 +07:00
},
"outputs": [
{
"name": "stdout",
2022-09-08 03:07:03 +07:00
"output_type": "stream",
2022-09-08 02:59:13 +07:00
"text": [
2022-09-10 15:53:36 +07:00
"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"
2022-09-08 02:59:13 +07:00
]
}
2022-09-08 03:07:03 +07:00
],
2022-09-08 02:59:13 +07:00
"source": [
2022-09-08 03:07:03 +07:00
"# Second Testing\n",
2022-09-08 02:59:13 +07:00
"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()"
2022-09-08 03:07:03 +07:00
]
},
{
"cell_type": "code",
2022-09-10 15:53:36 +07:00
"execution_count": 7,
2022-09-08 02:59:13 +07:00
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "jaif_A4_hfeJ",
2022-09-10 15:53:36 +07:00
"outputId": "a231e081-6ff2-4e13-db18-759225fcd948"
2022-09-08 02:59:13 +07:00
},
"outputs": [
{
"name": "stdout",
2022-09-08 03:07:03 +07:00
"output_type": "stream",
2022-09-08 02:59:13 +07:00
"text": [
2022-09-10 15:53:36 +07:00
"Enter your polynomial: 1.2 6.78 11.11\n",
2022-09-08 02:59:13 +07:00
"Enter your K: 3.141\n",
2022-09-10 15:53:36 +07:00
" 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",
2022-09-08 02:59:13 +07:00
"SYSTEM IS STABLE\n"
]
}
2022-09-08 03:07:03 +07:00
],
"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()"
2022-09-08 02:59:13 +07:00
]
2022-09-10 15:53:36 +07:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SjkQCR9chk6P"
},
"outputs": [],
"source": []
2022-09-08 03:07:03 +07:00
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"provenance": []
2022-09-08 02:59:13 +07:00
},
2022-09-08 03:07:03 +07:00
"kernelspec": {
2022-09-10 15:53:36 +07:00
"display_name": "Python 3.8.10 64-bit",
"language": "python",
2022-09-08 03:07:03 +07:00
"name": "python3"
},
"language_info": {
2022-09-10 15:53:36 +07:00
"name": "python",
"version": "3.8.10"
},
"vscode": {
"interpreter": {
"hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
}
2022-09-08 02:59:13 +07:00
}
2022-09-08 03:07:03 +07:00
},
"nbformat": 4,
"nbformat_minor": 0
}