def UnstructuredDelta(M, DeltaStructure): """ Function to calculate the unstructured delta stracture for a pertubation. Parameters ---------- M : matrix (2 by 2), TODO: extend for n by n M-delta structure DeltaStructure : string ['Full','Diagonal'] Type of delta structure Returns ------- delta : matrix unstructured delta matrix """ if DeltaStructure == "Full": [U, s, V] = utils.SVD(M) S = np.diag(s) delta = 1/s[0] * V[:,0] * U[:,0].H elif DeltaStructure == 'Diagonal': # TODO: complete delta = 'NA' else: delta = 0 return delta
def main(): # first read in data. tar == class label. dat == data print print "Reading Data..." dat, tar = utils.ReadDat("train.csv", 1) print "Done Reading.\n" print 'Generated Sample Image\n' utils.sampleImage(dat[11, :]) # preliminary statistics print "How many of each digit in training set?" print np.bincount(np.int32(tar)), '\n' # break up training data into training, cross validation, and test set (60,20,20) xTrain, xTmp, yTrain, yTmp = cross_validation.train_test_split( dat, tar, test_size=0.4) xVal, xTest, yVal, yTest = cross_validation.train_test_split(xTmp, yTmp, test_size=0.5) # now perform preprocessing (mean normalization and SVD to reduce feature space) print 'Preprocessing...' xTrain = utils.meanNorm(xTrain) xVal = utils.meanNorm(xVal) xTest = utils.meanNorm(xTest) Ur = utils.SVD(xTrain, 0.9) print np.shape(xTrain), np.shape(xVal), np.shape(xTest) xTrain = utils.Project(xTrain, Ur) xVal = utils.Project(xVal, Ur) xTest = utils.Project(xTest, Ur) print np.shape(xTrain), np.shape(xVal), np.shape(xTest) print 'Done Preprocessing.\n' # now train SVM on training set; choose optimal SVM parameters based on validation set print 'Now training SVM...\n' opt = TrainSVM(xTrain, xVal, xTest, yTrain, yVal, yTest) print # now read in test set print "Reading Test Set..." test, tmp = utils.ReadDat("test.csv", 0) print "Done Reading.\n" # Preprocess test set test = utils.meanNorm(test) test = utils.Project(test, Ur) # now make prediction print 'Making Prediction' pred = MakePred(xTrain, yTrain, opt, test, xTest, yTest) print print 'Outputting prediction\n' utils.OutputPred(pred, ['ImageId', 'Label'], 0) print 'Done'
def input_acceptable_const_plot(G, Gd, w_start=-2, w_end=2, axlim=None, points=1000, modified=False): """ Subplots for input constraints for acceptable control. Applies equation 6.55 (p241). Parameters ---------- G : numpy matrix Plant model. Gd : numpy matrix Plant disturbance model. modified : boolean If true, the arguments in the equation are changed to :math:`\sigma_1 (G) + 1 \geq |u_i^H g_d|`. This is to avoid a negative log scale. Returns ------- Plot : matplotlib figure Note ---- This condition only holds for :math:`|u_i^H g_d|>1`. """ s, w, axlim = df.frequency_plot_setup(axlim, w_start, w_end, points) freqresp = [G(si) for si in s] sig = numpy.array([utils.sigmas(Gfr) for Gfr in freqresp]) one = numpy.ones(points) plot_No = 1 dimGd = numpy.shape(Gd(0))[1] dimG = numpy.shape(G(0))[0] acceptable_control = numpy.zeros((dimGd, dimG, points)) for j in range(dimGd): for i in range(dimG): for k in range(points): U, _, _ = utils.SVD(G(s[k])) acceptable_control[j, i, k] = numpy.abs(U[:, i].H * Gd(s[k])[:, j]) plt.subplot(dimG, dimGd, plot_No) if not modified: plt.loglog(w, sig[:, i], label=('$\sigma_%s$' % (i + 1))) plt.plot(w, acceptable_control[j, i] - one, label=('$|u_%s^H.g_{d%s}|-1$' % (i + 1, j + 1))) else: plt.loglog(w, sig[:, i] + one, label=('$\sigma_%s+1$' % (i + 1))) plt.plot(w, acceptable_control[j, i], label=('$|u_%s^H.g_{d%s}|$' % (i + 1, j + 1))) plt.loglog([w[0], w[-1]], [1, 1], 'r', ls=':', label='Applicable') plt.xlabel('Frequency [rad/unit time]') plt.grid(True) plt.axis(axlim) plt.legend() plot_No += 1
def perf_Wp_plot(S, wB_req, maxSSerror, w_start, w_end, axlim=None, points=1000): """ MIMO sensitivity S and performance weight Wp plotting funtion. Parameters ---------- S : numpy array Sensitivity transfer function matrix as function of s => S(s) wB_req : float The design or require bandwidth of the plant in rad/time. 1/time eg: wB_req = 1/20sec = 0.05rad/s maxSSerror : float The maximum stead state tracking error required of the plant. wStart : float Minimum power of w for the frequency range in rad/time. eg: for w starting at 10e-3, wStart = -3. wEnd : float Maximum value of w for the frequency range in rad/time. eg: for w ending at 10e3, wStart = 3. Returns ------- wB : float The actually plant bandwidth in rad/time given the specified controller used to generate the sensitivity matrix S(s). Plot : matplotlib figure Example ------- >>> K = numpy.array([[1., 2.], ... [3., 4.]]) >>> t1 = numpy.array([[5., 5.], ... [5., 5.]]) >>> t2 = numpy.array([[5., 6.], ... [7., 8.]]) >>> Kc = numpy.array([[0.1, 0.], ... [0., 0.1]])*10 >>> >>> def G(s): ... return K*numpy.exp(-t1*s)/(t2*s + 1) ... >>> def L(s): ... return Kc*G(s) ... >>> def S(s): ... # SVD of S = 1/(I + L) ... return numpy.linalg.inv((numpy.eye(2) + L(s))) >>> perf_Wp(S, 0.05, 0.2, -3, 1) """ s, w, axlim = df.frequency_plot_setup(axlim, w_start, w_end, points) magPlotS1 = numpy.zeros((len(w))) magPlotS3 = numpy.zeros((len(w))) Wpi = numpy.zeros((len(w))) f = 0 # f for flag for i in range(len(w)): _, Sv, _ = utils.SVD(S(s[i])) magPlotS1[i] = Sv[0] magPlotS3[i] = Sv[-1] if f < 1 and magPlotS1[i] > 0.707: wB = w[i] f = 1 for i in range(len(w)): Wpi[i] = utils.Wp(wB_req, maxSSerror, s[i]) plt.subplot(2, 1, 1) plt.loglog(w, magPlotS1, 'r-', label='Max $\sigma$(S)') plt.loglog(w, 1. / Wpi, 'k:', label='|1/W$_P$|', lw=2.) plt.axhline(0.707, color='green', ls=':', lw=2, label='|S| = 0.707') plt.axvline(wB_req, color='blue', ls=':', lw=2) plt.text(wB_req * 1.1, 7, 'req wB', color='blue', fontsize=10) plt.axvline(wB, color='green') plt.text(wB * 1.1, 0.12, 'wB = %0.3f rad/s' % wB, color='green', fontsize=10) plt.axis(axlim) plt.grid(True) plt.xlabel('Frequency [rad/unit time]') plt.ylabel('Magnitude') plt.legend(loc='upper left', fontsize=10, ncol=1) plt.subplot(2, 1, 2) plt.semilogx(w, magPlotS1 * Wpi, 'r-', label='|W$_P$S|') plt.axhline(1, color='blue', ls=':', lw=2) plt.axvline(wB_req, color='blue', ls=':', lw=2, label='|W$_P$S| = 1') plt.text(wB_req * 1.1, numpy.max(magPlotS1 * Wpi) * 0.95, 'req wB', color='blue', fontsize=10) plt.axvline(wB, color='green') plt.text(wB * 1.1, 0.12, 'wB = %0.3f rad/s' % wB, color='green', fontsize=10) plt.axis(axlim) plt.xlabel('Frequency [rad/unit time]') plt.ylabel('Magnitude') plt.legend(loc='upper right', fontsize=10, ncol=1) return wB