diff --git a/442385_Nanda_Sisken_Assign_3.ipynb b/442385_Nanda_Sisken_Assign_3.ipynb new file mode 100644 index 0000000..78a5228 --- /dev/null +++ b/442385_Nanda_Sisken_Assign_3.ipynb @@ -0,0 +1,211 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "# Import Libraries\n", + "import numpy as np\n", + "import pandas as pd" + ], + "metadata": { + "id": "aDtFkeqAvLX2" + }, + "execution_count": 132, + "outputs": [] + }, + { + "cell_type": "code", + "execution_count": 133, + "metadata": { + "id": "Letb52yp47Gb" + }, + "outputs": [], + "source": [ + "# Define RouthStability Class \n", + "class RouthStability():\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", + " ## 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", + " ## 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", + " ## print generated route table\n", + " def show_tab(self):\n", + " print(self.df)\n", + " ## return generated route DataFrame\n", + " def get_table(self):\n", + " return self.df\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", + " ## 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", + "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()" + ], + "metadata": { + "id": "Je19fGrvtOu4", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "c99b1159-f745-4c3f-c012-c2f464b8edb0" + }, + "execution_count": 134, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "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" + ] + } + ] + }, + { + "cell_type": "code", + "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()" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "kFgcxNcscafI", + "outputId": "25aadcc4-6a29-4c16-c992-46d95a56cbfb" + }, + "execution_count": 136, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "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" + ] + } + ] + }, + { + "cell_type": "code", + "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": { + "base_uri": "https://localhost:8080/" + }, + "id": "jaif_A4_hfeJ", + "outputId": "0b387e7e-efbf-4f5c-fb45-d98489bbba3e" + }, + "execution_count": 137, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "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" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "SjkQCR9chk6P" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file