根据双摆系统的拉格朗日动力模型建立仿真。
计算拉格朗日方程:
当计算出拉格朗日量之后,需要求出L对phi1,phi2,dphi1,dphi2的偏导。
Matlab中,当所有参数为sym变量时,我们可以用jacobian这一命令实现:
L_phi1 = jacobian(L,phi1);
L_dphi1 = jacobian(L,dphi1);
L_phi2 = jacobian(L,phi2);
L_dphi2 = jacobian(L,dphi2);
2.求出偏导后,需要计算dL_ddphi1和dL_ddphi2对时间的导数,但是此时角度为sym变量,因此需将两式中的角度,角速度变量替换为与时间有关的变量:
可以使用subs来实现这一操作:
L_dphi1_t = subs(L_dphi1,{phi1,dphi1,phi2,dphi2}, ...
str2sym({'phi1(t)','dphi1(t)','phi2(t)','dphi2(t)'}));
L_dphi2_t = subs(L_dphi2,{phi1,dphi1,phi2,dphi2}, ...
str2sym({'phi1(t)','dphi1(t)','phi2(t)','dphi2(t)'}));
然后使用diff函数对时间求导:
dL_dphi1_t = diff(L_dphi1_t,t);
dL