from numpy import* from scipy.linalg import* def lqr(A,B,Q,R): S =matrix(solve_continuous_are(A, B, Q, R)) K = matrix(inv(R)*(BT*S)) E= eig(AB*K)[0] return K, S, E P1=solve_lyapunov(NN,Ct*Q*C)
# -*- coding: utf8 -*- from numpy import* from control.matlab import * from matplotlib import pyplot as plt from scipy.linalg import* def lqr(A,B,Q,R): S =matrix(solve_continuous_are(A, B, Q, R)) K = matrix(inv(R)*(BT*S)) E= eig(AB*K)[0] return K, S, E # A=matrix([[-100,0,0],[143.678, -16.667,-195.402],[0,1.046,0]]); B=matrix([[2300], [0], [0]]); C=eye(3) D=zeros([3,1]) # R=[[84]] Q=matrix([[0.01, 0, 0],[0, 0.01, 0],[0, 0, 0.01]]); # LQR- [K,S,E]=lqr(A,B,Q,R) # N=AB*K; NN=NT; w=ss(N,B,C,D) x0=matrix([[220], [147], [162]]); Ct=CT; P1=solve_lyapunov(NN,Ct*Q*C) Kt=KT; x0t=x0.T; Jx=abs((x0t*P1*x0)[0,0]) P2=solve_lyapunov(NN,Kt*R*K); Ju=abs((x0t*P2*x0)[0,0]) fig = plt.figure() plt.suptitle(' R = %s,\n\ $J_{x}$= %s; $J_{u}$ = %s,Q=[[0.01,0,0],[0, 0.01,0],[0,0,0.01]]'%(R[0][0],round(Jx,1),round(Ju,1))) ax1 = fig.add_subplot(311) y,x=step(w[0,0]) ax1.plot(x,y,"b") plt.ylabel('$x_{1}$,B') ax1.grid(True) ax2 = fig.add_subplot(312) y,x=step(w[1,0]) ax2.plot(x,y,"b") plt.ylabel('$x_{2}$,A') ax2.grid(True) ax3 = fig.add_subplot(313) y,x=step(w[2,0]) ax3.plot(x,y,"b") plt.ylabel('$x_{3}$,$C^{-1}$') ax3.grid(True) plt.xlabel(', .') plt.show()
# -*- coding: utf8 -*- from numpy import* from control.matlab import * from matplotlib import pyplot as plt from scipy.linalg import* def lqr(A,B,Q,R): S =matrix(solve_continuous_are(A, B, Q, R)) K = matrix(inv(R)*(BT*S)) E= eig(AB*K)[0] return K, S, E # A=matrix([[-100,0,0],[143.678, -16.667,-195.402],[0,1.046,0]]); B=matrix([[2300], [0], [0]]); C=eye(3) D=zeros([3,1]) # R=[[840]] Q=matrix([[0.01, 0, 0],[0, 0.01, 0],[0, 0, 0.01]]); # LQR- [K,S,E]=lqr(A,B,Q,R) # N=AB*K; NN=NT; w=ss(N,B,C,D) x0=matrix([[220], [147], [162]]); Ct=CT; P1=solve_lyapunov(NN,Ct*Q*C) Kt=KT; x0t=x0.T; Jx=abs((x0t*P1*x0)[0,0]) P2=solve_lyapunov(NN,Kt*R*K); Ju=abs((x0t*P2*x0)[0,0]) fig = plt.figure() plt.suptitle(' R = %s,\n\ $J_{x}$= %s; $J_{u}$ = %s,Q=[[0.01,0,0],[0, 0.01,0],[0,0,0.01]]'%(R[0][0],round(Jx,1),round(Ju,1))) ax1 = fig.add_subplot(311) y,x=step(w[0,0]) ax1.plot(x,y,"b") plt.ylabel('$x_{1}$,B') ax1.grid(True) ax2 = fig.add_subplot(312) y,x=step(w[1,0]) ax2.plot(x,y,"b") plt.ylabel('$x_{2}$,A') ax2.grid(True) ax3 = fig.add_subplot(313) y,x=step(w[2,0]) ax3.plot(x,y,"b") plt.ylabel('$x_{3}$,$C^{-1}$') ax3.grid(True) plt.xlabel(', .') plt.show()
# -*- coding: utf8 -*- from numpy import* from control.matlab import * from matplotlib import pyplot as plt from scipy.linalg import* def lqr(A,B,Q,R): S =matrix(solve_continuous_are(A, B, Q, R)) K = matrix(inv(R)*(BT*S)) E= eig(AB*K)[0] return K, S, E # A=matrix([[-100,0,0],[143.678, -16.667,-195.402],[0,1.046,0]]); B=matrix([[2300], [0], [0]]); C=eye(3) D=zeros([3,1]) # R=[[840]] Q=matrix([[0.01,0,0],[0,0.88,0],[0,0,0.01]]); # LQR- [K,S,E]=lqr(A,B,Q,R) # N=AB*K; NN=NT; w=ss(N,B,C,D) x0=matrix([[220], [147], [162]]); Ct=CT; P1=solve_lyapunov(NN,Ct*Q*C) Kt=KT; x0t=x0.T; Jx=abs((x0t*P1*x0)[0,0]) P2=solve_lyapunov(NN,Kt*R*K); Ju=abs((x0t*P2*x0)[0,0]) fig = plt.figure() plt.suptitle(' R = %s,\n\ $J_{x}$= %s; $J_{u}$ = %s,Q=[[0.01,0,0],[0,0.88,0],[0,0,0.01]]'%(R[0][0],round(Jx,1),round(Ju,1))) ax1 = fig.add_subplot(311) y,x=step(w[0,0]) ax1.plot(x,y,"b") plt.ylabel('$x_{1}$,B') ax1.grid(True) ax2 = fig.add_subplot(312) y,x=step(w[1,0]) ax2.plot(x,y,"b") plt.ylabel('$x_{2}$,A') ax2.grid(True) ax3 = fig.add_subplot(313) y,x=step(w[2,0]) ax3.plot(x,y,"b") plt.ylabel('$x_{3}$,$C^{-1}$') ax3.grid(True) plt.xlabel(', .') plt.show()
# -*- coding: utf8 -*- from numpy import* from control.matlab import * from matplotlib import pyplot as plt from scipy.linalg import* def lqr(A,B,Q,R): S =matrix(solve_continuous_are(A, B, Q, R)) K = matrix(inv(R)*(BT*S)) E= eig(AB*K)[0] return K, S, E # A=matrix([[-100,0,0,0],[143.678, -16.667,-195.402,0],[0,1.046,0,0] ,[0,0,1,0]]); B=matrix([[2300], [0], [0],[0]]); C=matrix([[1, 0, 0,0],[0, 1, 0,0],[0, 0, 1,0],[0, 0, 0,1]]); D=matrix([[0],[0],[0],[0]]); # R=[[100]]; Q=matrix([[0.001, 0, 0,0],[0, 0.001, 0,0],[0, 0, 0.01,0],[0, 0,0,200]]); # LQR- (K,S,E)=lqr(A,B,Q,R) # N=AB*K; NN=NT; w=ss(N,B,C,D) x0=matrix([[220], [147], [162],[0]]); Ct=CT; P1=solve_lyapunov(NN,Ct*Q*C) Kt=KT; x0t=x0.T; Jx=abs((x0t*P1*x0)[0,0]) P2=solve_lyapunov(NN,Kt*R*K); Ju=abs((x0t*P2*x0)[0,0]) fig = plt.figure(num=None, figsize=(9, 7), dpi=120, facecolor='w', edgecolor='k') plt.suptitle(' LQR -',size=14) ax1 = fig.add_subplot(411) y,x=step(w[0,0]) ax1.plot(x,y,"b") plt.ylabel('out(1)') ax1.grid(True) ax2 = fig.add_subplot(412) y,x=step(w[1,0]) ax2.plot(x,y,"b") plt.ylabel('out(2)') ax2.grid(True) ax3 = fig.add_subplot(413) y,x=step(w[2,0]) ax3.plot(x,y,"b") plt.ylabel('out(3)') ax3.grid(True) ax4 = fig.add_subplot(414) y,x=step(w[3,0]) ax4.plot(x,y,"b") plt.ylabel('out(4)') ax4.grid(True) plt.xlabel(', .') plt.show()
# -*- coding: utf8 -*- from numpy import* from control.matlab import * from matplotlib import pyplot as plt i=(-1)**0.5 A=matrix([[-100,0,0],[143.678, -16.667,-195.402],[0,1.046,0]]); B=matrix([[2300], [0], [0]]); C=eye(3) D=zeros([3,1]) p=[[-9.71+14.97*i -9.71-14.97*i -15.39 -99.72]]; k=acker(A,B,p) H=AB*k; Wss=ss(H,B,C,D); step(Wss) w=tf(Wss) fig = plt.figure() plt.suptitle(' acker()') ax1 = fig.add_subplot(311) y,x=step(w[0,0]) ax1.plot(x,y,"b") ax1.grid(True) ax2 = fig.add_subplot(312) y,x=step(w[1,0]) ax2.plot(x,y,"b") ax2.grid(True) ax3 = fig.add_subplot(313) y,x=step(w[2,0]) ax3.plot(x,y,"b") ax3.grid(True) plt.xlabel(', .') plt.show()
from scipy.linalg import* # scipy lqr from numpy import * # numpy from matplotlib.pyplot import * # from control.matlab import * # control..ss() control..step() # lqr() m = 4; # . J = 0.0475; # ***2 #́- # . r = 0.25; # . g = 9.8; # /**2 c = 0.05; # () def lqr(A,B,Q,R): X =matrix(solve_continuous_are(A, B, Q, R)) K = matrix(inv(R)*(BT*X)) E= eig(AB*K)[0] return X, K, E
xe = [0, 0, 0, 0, 0, 0];# x () ue = [0, m*g]; # # A = matrix( [[ 0, 0, 0, 1, 0, 0], [ 0, 0, 0, 0, 1, 0], [ 0, 0, 0, 0, 0, 1], [ 0, 0, (-ue[0]*sin(xe[2]) - ue[1]*cos(xe[2]))/m, -c/m, 0, 0], [ 0, 0, (ue[0]*cos(xe[2]) - ue[1]*sin(xe[2]))/m, 0, -c/m, 0], [ 0, 0, 0, 0, 0, 0 ]]) # B = matrix( [[0, 0], [0, 0], [0, 0], [cos(xe[2])/m, -sin(xe[2])/m], [sin(xe[2])/m, cos(xe[2])/m], [r/J, 0]]) # C = matrix([[1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0]]) D = matrix([[0, 0], [0, 0]])
xd = matrix([[1], [0], [0], [0], [0], [0]]); yd = matrix([[0], [1], [0], [0], [0], [0]]);
lat = (0,2,3,5); alt = (1,4); # Ax = (A[lat, :])[:, lat]; Bx = B[lat, 0]; Cx = C[0, lat]; Dx = D[0, 0]; Ay = (A[alt, :])[:, alt]; By = B[alt, 1]; Cy = C[1, alt]; Dy = D[1, 1]; # K Qx1 = diag([1, 1, 1, 1, 1, 1]); Qu1a = diag([1, 1]); X,K,E =lqr(A, B, Qx1, Qu1a) K1a = matrix(K); # x H1ax = ss(Ax - Bx*K1a[0,lat], Bx*K1a[0,lat]*xd[lat,:], Cx, Dx); (Yx, Tx) = step(H1ax, T=linspace(0,10,100)); # y H1ay = ss(Ay - By*K1a[1,alt], By*K1a[1,alt]*yd[alt,:], Cy, Dy); (Yy, Ty) = step(H1ay, T=linspace(0,10,100));
figure(); title(" x y") plot(Tx.T, Yx.T, '-', Ty.T, Yy.T, '--'); plot([0, 10], [1, 1], 'k-'); axis([0, 10, -0.1, 1.4]); ylabel(' '); xlabel(''); legend(('x', 'y'), loc='lower right'); grid() show()
# Qu1a = diag([1, 1]); X,K,E =lqr(A, B, Qx1, Qu1a) K1a = matrix(K); H1ax = ss(Ax - Bx*K1a[0,lat], Bx*K1a[0,lat]*xd[lat,:], Cx, Dx); Qu1b = (40**2)*diag([1, 1]); X,K,E =lqr(A, B, Qx1, Qu1b) K1b = matrix(K); H1bx = ss(Ax - Bx*K1b[0,lat], Bx*K1b[0,lat]*xd[lat,:],Cx, Dx); Qu1c = (200**2)*diag([1, 1]); X,K,E =lqr(A, B, Qx1, Qu1c) K1c = matrix(K); H1cx = ss(Ax - Bx*K1c[0,lat], Bx*K1c[0,lat]*xd[lat,:],Cx, Dx); figure(); [Y1, T1] = step(H1ax, T=linspace(0,10,100)); [Y2, T2] = step(H1bx, T=linspace(0,10,100)); [Y3, T3] = step(H1cx, T=linspace(0,10,100)); plot(T1.T, Y1.T, 'r-'); plot(T2.T, Y2.T, 'b-'); plot(T3.T, Y3.T, 'g-'); plot([0 ,10], [1, 1], 'k-'); axis([0, 10, -0.1, 1.4]); title(" ") legend(('1', '$1,6\cdot 10^{3}$','$4\cdot 10^{4}$'), loc='lower right'); text(5.3, 0.4, 'rho'); ylabel(' '); xlabel(''); grid() show()
# Qx Qx2 = CT * C; Qu2 = 0.1 * diag([1, 1]); X,K,E =lqr(A, B, Qx2, Qu2) K2 = matrix(K); H2x = ss(Ax - Bx*K2[0,lat], Bx*K2[0,lat]*xd[lat,:], Cx, Dx); H2y = ss(Ay - By*K2[1,alt], By*K2[1,alt]*yd[alt,:], Cy, Dy); figure(); [Y2x, T2x] = step(H2x, T=linspace(0,10,100)); [Y2y, T2y] = step(H2y, T=linspace(0,10,100)); plot(T2x.T, Y2x.T, T2y.T, Y2y.T) plot([0, 10], [1, 1], 'k-'); axis([0, 10, -0.1, 1.4]); title(" ") ylabel(' '); xlabel(''); legend(('x', 'y'), loc='lower right'); grid() show()
# # # 1 . 10 . Qx3 = diag([100, 10, 2*pi/5, 0, 0, 0]); Qu3 = 0.1 * diag([1, 10]); X,K,E =lqr(A, B, Qx3, Qu3) K3 = matrix(K); H3x = ss(Ax - Bx*K3[0,lat], Bx*K3[0,lat]*xd[lat,:], Cx, Dx); H3y = ss(Ay - By*K3[1,alt], By*K3[1,alt]*yd[alt,:], Cy, Dy); figure(); [Y3x, T3x] = step(H3x, T=linspace(0,10,100)); [Y3y, T3y] = step(H3y, T=linspace(0,10,100)); plot(T3x.T, Y3x.T, T3y.T, Y3y.T) axis([0, 10, -0.1, 1.4]); title(" ") ylabel(' '); xlabel(''); legend(('x', 'y'), loc='lower right'); grid() show()
Source: https://habr.com/ru/post/441582/