iterative linear quadratic regulator for a cart pole system in matlab
main.m
% Define the system parametersm_c = 1; % Mass of the cartm_p = 0.1; % Mass of the poleg = 9.81; % Gravitational accelerationl = 0.5; % Length of the poled = 0.1; % Damping coefficient% Define the state-space modelA = [0100;
0 -d/m_c -m_p*g/m_c 0;
0001;
0 -l*d/(m_c*l^2) -l*(m_c+m_p)*g/(m_c*l^2) 0];
B = [0; 1/m_c; 0; l/(m_c*l^2)];
C = [1000; 0010];
D = [0; 0];
% Define the cost functionQ = diag([100.11000.1]); % State weightsR = 0.1; % Input weight% Iteratively solve the LQR problemK = zeros(1,4); % Start with zero gainsiter_max = 100; % Maximum number of iterationstol = 1e-6; % Tolerance for convergencefori = 1:iter_max
[K_new,~,~] = lqr(A,B,Q,R); % Solve the LQR problemifmax(abs(K_new - K)) < tol % Check for convergencebreak;
else K = K_new;
endend% Simulate the system with the LQR controllert = 0:0.01:5;
x0 = [00pi/1800]; % Initial stateu = -K*x0'; % Compute the control input[t,x] = ode45(@(t,x) cart_pole(t,x,u,m_c,m_p,g,l,d),t,x0); % Simulate the system with the control input% Plot the resultsfigure;
subplot(211); plot(t,x(:,1)); ylabel('Cart Position (m)'); grid on;
subplot(212); plot(t,x(:,3)*180/pi); ylabel('Pole Angle (deg)'); xlabel('Time (s)'); grid on;
% Define the nonlinear dynamics of the cart-pole systemfunction[dxdt] = cart_pole(t,x,u,m_c,m_p,g,l,d) s = sin(x(3));
c = cos(x(3));
dxdt = zeros(4,1);
dxdt(1) = x(2);
dxdt(2) = (u + m_p*l*(x(4)^2)*s - (m_c+m_p)*g*s - d*x(2))/(m_c + m_p*(1-c^2));
dxdt(3) = x(4);
dxdt(4) = ((m_c+m_p)*g*s - c*(u + m_p*l*(x(4)^2)*s - (m_c+m_p)*g*s - d*x(2)))/(l*(m_c + m_p*(1-c^2)));
end