{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# EE16B Section 13B: Stability, Steady-State Error, and Integrators" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#Run these to import all of the necessary files\n", "import numpy as np\n", "import scipy as sp\n", "import scipy.io as spio\n", "import scipy.signal as sig\n", "import scipy.io.wavfile as wf\n", "import matplotlib as plt\n", "from pylab import *\n", "import os\n", "import re\n", "\n", "# Plots graphs in the browser window\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook implements the examples from the lecture on 11/24." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Define some functions to iterate the state updates\n", "\n", "def step_P_CL(A_CL,B_CL,u,x0):\n", " return A_CL*x0+B_CL*u\n", " \n", "def sim_P_CL(A_CL,B_CL,u,x0,n):\n", " x = [x0]\n", " x_cur = x0\n", " for i in range(n):\n", " x_next = step_P_CL(A_CL,B_CL,u[i],x_cur)\n", " x.append(x_next)\n", " x_cur=x_next\n", " return x\n", "\n", "\n", "def step_PI_CL(A_CL,B_CL,u,x0):\n", " return np.dot(A_CL,x0)+np.dot(B_CL,u)\n", " \n", "def sim_PI_CL(A_CL,B_CL,u,x0,n):\n", " x = x0\n", " x_cur = x0\n", " for i in range(n):\n", " x_next = step_PI_CL(A_CL,B_CL,u[i],x_cur)\n", " x=np.concatenate((x,x_next),axis=1)\n", " x_cur=x_next\n", " return x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Example 1: Proportional Control" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Just one state variable (A is 1x1)\n", "\n", "init_state = 0\n", "desired_state = 1\n", "\n", "init=init_state\n", "iter=100\n", "yd=iter*[desired_state]\n", "\n", "plt.figure(1,figsize=(16,8))\n", "t = np.linspace(0,1,num=iter+1)\n", "plt.plot(t[0:iter],yd,label='Desired Output',linewidth=4)\n", "\n", "for i in range(-5,10):\n", " K=i/10+0.5\n", " A_CL=0.5-K\n", " B_CL=K\n", " x = sim_P_CL(A_CL,B_CL,yd,init,iter)\n", " plt.plot(t,x,label=\"%0.1f\"%K)\n", " sse = abs(desired_state-x[-1])\n", " print('K:',\"%0.1f\"%K,'\\t\\tSSE:',\"%0.2f\"%sse)\n", "\n", "plt.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **What happens to the steady-state error as we increase K?**\n", "* **What is the disadvantage of increasing K? What would happen if we increased it further?**\n", "* **Is it possible for this system to achieve zero steady-state error?**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Example 2: Proportional-Integral (PI) Control" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Add a second state variable that is the time integral of the first - now A is 2x2\n", "\n", "init_state = 0\n", "desired_state = 1\n", "\n", "init = np.array([[0],[0]])\n", "iter=100\n", "yd = np.full(iter,desired_state)\n", "Ts=1\n", "\n", "plt.figure(1,figsize=(16,8))\n", "t = np.linspace(0,1,num=iter+1)\n", "plt.plot(t[0:iter],yd,label='Desired Output',linewidth=4)\n", "\n", "for i in range(1,9):\n", " K=i/20\n", " A_CL = np.array([[0.5,1],[-K*Ts,1]])\n", " B_CL = np.array([[0],[K*Ts]])\n", " x = sim_PI_CL(A_CL,B_CL,yd,init,iter)\n", " plt.plot(t,x[0,:],label=\"%0.2f\"%K)\n", " sse = abs(desired_state-x[0,-1])\n", " print('K:',\"%0.2f\"%K,'\\t\\tSSE:',\"%0.2f\"%sse)\n", "\n", "plt.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **What is the steady-state error in each case?**\n", "* **Are there any disadvantages of PI control?**" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }