def step_response_plot(Y, U, t_end=50, initial_val=0, timedim='sec', axlim=None, points=1000, constraint=None, method='numeric'): ''' A plot of the step response of a transfer function Parameters ---------- Y : tf Output transfer function. U : tf Input transfer function. t_end : integer Time period which the step response should occur (optional). initial_val : integer Starting value to evalaute step response (optional). constraint : float The upper limit the step response cannot exceed. is only calculated if a value is specified (optional). method : ['numeric','analytic'] The method that is used to calculate a constrainted response. A constraint value is required (optional). Returns ------- Plot : matplotlib figure ''' if axlim is None: axlim = [None, None, None, None] plt.gcf().set_facecolor('white') [t, y] = utils.tf_step(Y, t_end, initial_val) plt.plot(t, y) [t, y] = utils.tf_step(U, t_end, initial_val) plt.plot(t, y) if constraint is None: plt.legend(['$y(t)$', '$u(t)$']) else: [t, y] = utils.tf_step(U, t_end, initial_val, points, constraint, Y, method) plt.plot(t, y[0]) plt.plot(t, y[1]) plt.legend(['$y(t)$', '$u(t)$', '$u(t) const$', '$y(t) const$']) #con = constraint plt.plot([0, t_end], numpy.ones(2), '--') plt.axis(axlim) plt.xlabel('Time [' + timedim + ']')
def step_response_plot(Y, U, t_end=50, initial_val=0, timedim='sec', axlim=None, points=1000, constraint=None, method='numeric'): """ A plot of the step response of a transfer function Parameters ---------- Y : tf Output transfer function. U : tf Input transfer function. t_end : integer Time period which the step response should occur (optional). initial_val : integer Starting value to evaluate step response (optional). constraint : float The upper limit the step response cannot exceed. is only calculated if a value is specified (optional). method : ['numeric','analytic'] The method that is used to calculate a constrained response. A constraint value is required (optional). Returns ------- Plot : matplotlib figure """ axlim = df.frequency_plot_setup(axlim) [t, y] = utils.tf_step(Y, t_end, initial_val) plt.plot(t, y) [t, y] = utils.tf_step(U, t_end, initial_val) plt.plot(t, y) if constraint is None: plt.legend(['$y(t)$', '$u(t)$']) else: [t, y] = utils.tf_step(U, t_end, initial_val, points, constraint, Y, method) plt.plot(t, y[0]) plt.plot(t, y[1]) # con = constraint plt.legend(['$y(t)$', '$u(t)$', '$u(t) const$', '$y(t) const$']) plt.plot([0, t_end], numpy.ones(2), '--') plt.axis(axlim) plt.xlabel('Time [' + timedim + ']')
# First example in reference to demonstrate working of tf object # Define s as tf to enable its use as a variable s = tf([1, 0]) # Define the transfer functions F = tf(1, [1, 1]) G = tf(100, [1, 5, 100]) C = 20*(s**2 + s + 60) / s / (s**2 + 40*s + 400) S = tf(10, [1, 10]) T = F * feedback(G*C, S) # This is the same figure as in the reference tf_step(T, 6) # Assign names to lines as in second example F.u = 'r'; F.y = 'uF' C.u = 'e'; C.y = 'uC' G.u = 'u'; G.y = 'ym' S.u = 'ym'; S.y = 'y' # There must be a better way to call the name of an object if the object has # already been assigned a name.... F.name = 'F' C.name = 'C'
S = 1/(1+L) mT = maxpeak(T) mS = maxpeak(S) GM, PM, wc, wb, wbt, valid = marginsclosedloop(L) GM, PM, wc, w_180 = margins(L) print('GM = ', np.round(GM, 1)) print('PM = ', np.round(PM, 1), 'rad') print('wB = ', np.round(wb, 3)) print('wC = ', np.round(wc, 3)) print('wBT = ', wbt) print('w180 = ', np.round(w_180, 3)) print('Ms = ', np.round(mS, 2)) print('Mt = ', np.round(mT, 1)) [t, y] = tf_step(T, 50) plt.figure('Figure 2.17') plt.plot(t, y) plt.xlabel('time (s)') plt.ylabel('y(t)') plt.show() w = np.logspace(-2, 2, 1000) s = 1j*w gain_T = np.abs(T(s)) gain_S = np.abs(S(s)) plt.figure('Figure 2.18') plt.loglog(w, gain_T) plt.loglog(w, gain_S) yrange = np.logspace(-2, 2)
import matplotlib.pyplot as plt import numpy as np s = tf([1,0], 1) G = 200/((10*s + 1)*(0.05*s + 1)**2) Gd = 100/(10*s + 1) wc = 10 K = wc*(10*s + 1)*(0.1*s + 1)/(200*s*(0.01*s + 1)) L = G*K t = np.linspace(0, 3) Sd = (1/(1 + G*K))*Gd T = feedback(L, 1) [t,y] = tf_step(T, 3) plt.figure('Figure 2.22') plt.subplot(1, 2, 1) plt.plot(t, y) plt.title('Tracking Response') plt.ylabel('y(t)') plt.xlabel('Time (s)') plt.ylim([0, 1.5]) [t,yd] = Sd.step(0, t) plt.subplot(1, 2, 2) plt.plot(t, yd) plt.ylabel('y(t)') plt.xlabel('Time (s)') plt.title('Disturbance Response') plt.ylim([0, 1.5])
""" # First example in reference to demonstrate working of tf object # Define s as tf to enable its use as a variable s = tf([1, 0]) # Define the transfer functions F = tf(1, [1, 1]) G = tf(100, [1, 5, 100]) C = 20*(s**2 + s + 60) / s / (s**2 + 40*s + 400) S = tf(10, [1, 10]) T = F * feedback(G*C, S) # This is NOT the same figure as in the reference t, y = tf_step(T, 6) plt.plot(t, y) plt.xlabel('Time') plt.ylabel('y(t)') plt.show() #utilsplot.step(T, t_end = 6) # Assign names to lines as in second example F.u = 'r'; F.y = 'uF' C.u = 'e'; C.y = 'uC' G.u = 'u'; G.y = 'ym' S.u = 'ym'; S.y = 'y' # There must be a better way to call the name of an object if the object has # already been assigned a name.... F.name = 'F'
""" # First example in reference to demonstrate working of tf object # Define s as tf to enable its use as a variable s = tf([1, 0]) # Define the transfer functions F = tf(1, [1, 1]) G = tf(100, [1, 5, 100]) C = 20 * (s**2 + s + 60) / s / (s**2 + 40 * s + 400) S = tf(10, [1, 10]) T = F * feedback(G * C, S) # This is NOT the same figure as in the reference t, y = tf_step(T, 6) plt.plot(t, y) plt.xlabel('Time') plt.ylabel('y(t)') plt.show() #utilsplot.step(T, t_end = 6) # Assign names to lines as in second example F.u = 'r' F.y = 'uF' C.u = 'e' C.y = 'uC' G.u = 'u' G.y = 'ym' S.u = 'ym' S.y = 'y'
S = 1/(1 + L) mT = maxpeak(T) mS = maxpeak(S) GM, PM, wc, wb, wbt, valid = marginsclosedloop(L) GM, PM, wc, w_180 = margins(L) print('GM = ', np.round(GM, 1)) print('PM = ', np.round(PM, 1), 'rad') print('wB = ', np.round(wb, 3)) print('wC = ', np.round(wc, 3)) print('wBT = ', wbt) print('w180 = ', np.round(w_180, 3)) print('Ms = ', np.round(mS, 2)) print('Mt = ', np.round(mT, 1)) [t, y] = tf_step(T, 50) plt.figure('Figure 2.17') plt.title('Step response for system T') plt.plot(t, y) plt.xlabel('time (s)') plt.ylabel('y(t)') # Calculate rise time tr y90 = y[y<=0.9] tr = t[len(y90)] print('rise time = ',np.round(tr, 1) ,'s') plt.axhline(0.9, color='black', linestyle='--',linewidth=0.5) plt.axvline(tr, color='black', linestyle='--',linewidth=0.5) plt.show() w = np.logspace(-2, 2, 1000) s = 1j*w
# First example in reference to demonstrate working of tf object # Define s as tf to enable its use as a variable s = tf([1, 0]) # Define the transfer functions F = tf(1, [1, 1]) G = tf(100, [1, 5, 100]) C = 20 * (s**2 + s + 60) / s / (s**2 + 40 * s + 400) S = tf(10, [1, 10]) T = F * feedback(G * C, S) # This is the same figure as in the reference tf_step(T, 6) # Assign names to lines as in second example F.u = 'r' F.y = 'uF' C.u = 'e' C.y = 'uC' G.u = 'u' G.y = 'ym' S.u = 'ym' S.y = 'y' # There must be a better way to call the name of an object if the object has # already been assigned a name.... F.name = 'F'