cart-pole 建模

使用matlab symbolic toolbox进行拉格朗日建模,并转为LTI模型

Matlab 复制代码
%% Cart-Pole Lagrangian modeling + symbolic linearization (upright, theta=0)
clear; clc;
syms x dx ddx real
syms th dth ddth real
syms M m l g u real

% --- Generalized coordinates
q   = [x;  th];
dq  = [dx; dth];
ddq = [ddx; ddth];

% --- Kinetic & Potential energy
% Cart velocity: dx
% Pole mass position: px = x + l*sin(th), pz = l*cos(th)
% Velocities:
vpx = dx + l*cos(th)*dth;
vpz = -l*sin(th)*dth;
v2  = vpx^2 + vpz^2;

T_cart = 0.5*M*dx^2;
T_pole = 0.5*m*v2;
T = T_cart + T_pole;

% Potential energy (zero at upright th=0 ⇒ V = m*g*l*(1 - cos(th)))
V = m*g*l*(1 - cos(th));

% Lagrangian
L = T - V;

% --- Generalized forces (only x is actuated by force u)
Q = [u; 0];

% --- Euler-Lagrange: d/dt(dL/ddq) - dL/dq = Q
dLd_dq  = jacobian(L, q).';         % ∂L/∂q
dLd_ddq = jacobian(L, dq).';        % ∂L/∂dq

% Time derivative of dLd_ddq (treat q,dq as functions of time)
% Use total derivative: d/dt = (∂/∂q)*dq + (∂/∂dq)*ddq
d_dt_dLd_ddq = jacobian(dLd_ddq, q)*dq + jacobian(dLd_ddq, dq)*ddq;

EL = d_dt_dLd_ddq - dLd_dq - Q;     % = 0 gives equations of motion

% Solve for accelerations ddq = [ddx; ddth]
sol = solve(EL == 0, ddq);
ddx_expr  = simplify(sol.ddx);
ddth_expr = simplify(sol.ddth);

% --- Build nonlinear state-space: X=[x;dx;th;dth]
f = [
    dx;
    ddx_expr;
    dth;
    ddth_expr
];
g_u = jacobian(f, u);   % input channel (should be a 4x1)
f0  = subs(f, u, 0);    % drift when u=0

% --- Linearize around upright equilibrium: x=0, dx=0, th=0, dth=0, u=0
xeq = [0; 0; 0; 0];
ueq = 0;

A = simplify( subs( jacobian(f, [x, dx, th, dth]), ...
    [x, dx, th, dth, u], [xeq.', ueq] ) );
B = simplify( subs( g_u, [x, dx, th, dth, u], [xeq.', ueq] ) );

% --- Optional: pretty-print A, B
disp('A ='); pretty(A); disp('B ='); pretty(B);

% --- 验证:将A,B化简到常见形式(象征参数不数值化)
A_simplified = simplify(A);
B_simplified = simplify(B);

% --- 如果想代入具体参数,取消注释以下行:
% Mv = 1.0; mv = 0.1; lv = 0.5; gv = 9.81;
% A_num = double(subs(A_simplified, {M,m,l,g}, {Mv,mv,lv,gv}));
% B_num = double(subs(B_simplified, {M,m,l}, {Mv,mv,lv}));

latex代码

复制代码
% ==========================================================
% Cart-Pole (Simplified) Lagrangian Modeling + Linearization
% ==========================================================

\section{Simplified Cart--Pole Model and Symbolic Linearization}
\label{sec:cartpole_model}

To validate the modeling and control pipeline in a minimal setting, a classical cart--pole
system is considered. The model is derived using the Euler--Lagrange method and then
symbolically linearized around the upright equilibrium.

\subsection{Generalized coordinates and parameters}
\label{subsec:cartpole_coordinates}

The generalized coordinates are chosen as
\begin{equation}
\mathbf{q}=
\begin{bmatrix}
x\\ \theta
\end{bmatrix},\qquad
\dot{\mathbf{q}}=
\begin{bmatrix}
\dot{x}\\ \dot{\theta}
\end{bmatrix},\qquad
\ddot{\mathbf{q}}=
\begin{bmatrix}
\ddot{x}\\ \ddot{\theta}
\end{bmatrix},
\end{equation}
where $x$ is the cart displacement and $\theta$ is the pole angle measured from the upright
direction ($\theta=0$ corresponds to the upright posture).
The system parameters are: cart mass $M$, pole mass $m$, pole length $l$ (from the cart pivot to the pole CoM),
and gravitational acceleration $g$. The cart is actuated by a horizontal input force $u$.

\subsection{Kinematics}
\label{subsec:cartpole_kinematics}

The cart velocity is $\dot{x}$. The pole CoM position is
\begin{equation}
p_x = x + l\sin\theta,\qquad
p_z = l\cos\theta,
\end{equation}
and the corresponding velocities are
\begin{equation}
\dot{p}_x=\dot{x}+l\cos\theta\,\dot{\theta},\qquad
\dot{p}_z=-l\sin\theta\,\dot{\theta}.
\end{equation}
Thus, the squared speed of the pole CoM is
\begin{equation}
v^2=\dot{p}_x^{\,2}+\dot{p}_z^{\,2}
=\left(\dot{x}+l\cos\theta\,\dot{\theta}\right)^2+\left(l\sin\theta\,\dot{\theta}\right)^2.
\label{eq:cartpole_v2}
\end{equation}

\subsection{Energy and Lagrangian}
\label{subsec:cartpole_energy}

The kinetic energy is the sum of the cart and pole kinetic energies:
\begin{equation}
T
=\frac{1}{2}M\dot{x}^2+\frac{1}{2}m v^2,
\label{eq:cartpole_T}
\end{equation}
where $v^2$ is given by \eqref{eq:cartpole_v2}.
The potential energy is chosen to be zero at the upright configuration $\theta=0$:
\begin{equation}
V=mgl\left(1-\cos\theta\right).
\label{eq:cartpole_V}
\end{equation}
Hence, the Lagrangian is
\begin{equation}
\mathcal{L}=T-V.
\label{eq:cartpole_L}
\end{equation}

\subsection{Euler--Lagrange equations of motion}
\label{subsec:cartpole_EL}

The generalized input is applied only along the cart coordinate, therefore
\begin{equation}
\mathbf{Q}=
\begin{bmatrix}
u\\ 0
\end{bmatrix}.
\label{eq:cartpole_Q}
\end{equation}
The Euler--Lagrange equations are
\begin{equation}
\frac{d}{dt}\left(\frac{\partial \mathcal{L}}{\partial \dot{\mathbf{q}}}\right)
-\frac{\partial \mathcal{L}}{\partial \mathbf{q}}
=\mathbf{Q}.
\label{eq:cartpole_EL_eq}
\end{equation}

Solving \eqref{eq:cartpole_EL_eq} for accelerations yields the nonlinear dynamics
\begin{equation}
\ddot{x} = \frac{u + ml\dot{\theta}^{2}\sin\theta - mg\sin\theta\cos\theta}{M+m\sin^{2}\theta},
\label{eq:cartpole_ddx}
\end{equation}
\begin{equation}
\ddot{\theta}
=
\frac{(M+m)g\sin\theta - \cos\theta\left(u+ml\dot{\theta}^{2}\sin\theta\right)}
{l\left(M+m\sin^{2}\theta\right)}.
\label{eq:cartpole_ddth}
\end{equation}

\subsection{Nonlinear state-space form}
\label{subsec:cartpole_nonlinear_ss}

Define the state vector
\begin{equation}
\mathbf{x}=
\begin{bmatrix}
x & \dot{x} & \theta & \dot{\theta}
\end{bmatrix}^{\!\top}.
\label{eq:cartpole_state}
\end{equation}
Using \eqref{eq:cartpole_ddx}--\eqref{eq:cartpole_ddth}, the nonlinear state-space model is
\begin{equation}
\dot{\mathbf{x}}=\mathbf{f}(\mathbf{x},u)=
\begin{bmatrix}
\dot{x}\\
\ddot{x}(x,\dot{x},\theta,\dot{\theta},u)\\
\dot{\theta}\\
\ddot{\theta}(x,\dot{x},\theta,\dot{\theta},u)
\end{bmatrix}.
\label{eq:cartpole_ss}
\end{equation}

\subsection{Symbolic linearization at the upright equilibrium}
\label{subsec:cartpole_linearization}

Consider the upright equilibrium (zero motion and zero input):
\begin{equation}
\mathbf{x}_0=
\begin{bmatrix}
0 & 0 & 0 & 0
\end{bmatrix}^{\!\top},\qquad
u_0=0.
\label{eq:cartpole_eq}
\end{equation}
A first-order Taylor expansion gives the local LTI model
\begin{equation}
\delta\dot{\mathbf{x}}=\mathbf{A}\,\delta\mathbf{x}+\mathbf{B}\,\delta u,
\label{eq:cartpole_lti}
\end{equation}
where
\begin{equation}
\mathbf{A}=\left.\frac{\partial \mathbf{f}}{\partial \mathbf{x}}\right|_{(\mathbf{x}_0,u_0)},
\qquad
\mathbf{B}=\left.\frac{\partial \mathbf{f}}{\partial u}\right|_{(\mathbf{x}_0,u_0)}.
\label{eq:cartpole_AB_def}
\end{equation}

Using symbolic differentiation (implemented in MATLAB), the Jacobians simplify to
\begin{equation}
\mathbf{A}=
\begin{bmatrix}
0 & 1 & 0 & 0\\
0 & 0 & -\dfrac{mg}{M} & 0\\
0 & 0 & 0 & 1\\
0 & 0 & \dfrac{(M+m)g}{lM} & 0
\end{bmatrix},
\qquad
\mathbf{B}=
\begin{bmatrix}
0\\[1mm]
\dfrac{1}{M}\\[1mm]
0\\[1mm]
-\dfrac{1}{lM}
\end{bmatrix}.
\label{eq:cartpole_AB_upright}
\end{equation}

The resulting LTI model \eqref{eq:cartpole_lti} is used for designing linear state-feedback controllers
(e.g., pole placement and LQR) around the upright balancing configuration.
相关推荐
IT猿手8 小时前
基于控制障碍函数的多无人机编队动态避障控制方法研究,MATLAB代码
开发语言·matlab·无人机·openclaw·多无人机动态避障路径规划·无人机编队
Evand J12 小时前
【MATLAB复现RRT(快速随机树)算法】用于二维平面上的无人车路径规划与避障,含性能分析与可视化
算法·matlab·平面·无人车·rrt·避障
s090713612 小时前
【声纳成像】基于滑动子孔径与加权拼接的条带式多子阵SAS连续成像(MATLAB仿真)
开发语言·算法·matlab·合成孔径声呐·后向投影算法·条带拼接
IT猿手15 小时前
基于 ZOH 离散化与增量 PID 的四旋翼无人机轨迹跟踪控制研究,MATLAB代码
开发语言·算法·matlab·无人机·动态路径规划·openclaw
IT猿手15 小时前
基于控制障碍函数(Control Barrier Function, CBF)的无人机编队三维动态避障路径规划,MATLAB代码
开发语言·matlab·无人机·动态路径规划·无人机编队
SugarFreeOixi15 小时前
MATLAB绘图风格记录NP类型
python·matlab·numpy
IT猿手15 小时前
基于 CBF 的多无人机编队动态避障路径规划研究,无人机及障碍物数量可以自定义修改,MATLAB代码
开发语言·matlab·无人机·动态路径规划
IT猿手18 小时前
基于强化学习Q-learning算法的无人机三维路径规划算法原理与实现,MATLAB代码
算法·matlab·无人机·路径规划·动态路径规划
ghie909020 小时前
拉普拉斯金字塔图像融合MATLAB仿真程序
人工智能·计算机视觉·matlab
IT猿手1 天前
基于动态三维环境下的Q-Learning算法无人机自主避障路径规划研究,MATLAB代码
算法·matlab·无人机·动态路径规划·多无人机动态避障路径规划