Matlab符号计算求导与化简

依托于Maple的符号计算引擎,Matlab可以完成高等数学中几乎一切符号计算。这里简单记录一下蔡自兴《机器人学》第三版中第四章的双连杆的动能推导过程。

在书本上我们已知的信息是:

x2=d1sinθ1+d2sin(θ1+θ2)
y2=d1cosθ1d2cos(θ1+θ2)
v22=x˙2+y˙2

其中 θi(i=1,2) 是时间的函数,我们通过演算可以得到与书中一样的不错的表达式:

v22=d21θ1˙2+d22(θ2˙2+2θ1˙θ2˙+θ2˙2)+2d1d2cosθ2(θ1˙2+θ1˙θ2˙)

下面就以这个表达式为目标,探索如何利用Matlab进行带有导数的表达式的运算和化简

clc,clear,close all
syms th1(t) th2(t) d1 d2 Dth1 Dth2

x2 = d1*sin(th1) + d2*sin(th1 + th2);
y2 = -d1*cos(th1) - d2*cos(th1 + th2);
v22 = diff(x2)^2 + diff(y2)^2

v22 指的是 v22 ,上述代码的输出结果为:

v22(t) =

(d2*cos(th1(t) + th2(t))*(diff(th1(t), t) + diff(th2(t), t)) + d1*cos(th1(t))*diff(th1(t), t))^2 + (d2*sin(th1(t) + th2(t))*(diff(th1(t), t) + diff(th2(t), t)) + d1*sin(th1(t))*diff(th1(t), t))^2

简化表达式

这个结果显然不是我们想要的,下面逐步进行变换化简

  1. 用变量 Dth1Dth2 替换 diff(th1(t), t)diff(th2(t), t) ,简化表达式

    v22_pretty = subs(v22,[diff(th1(t), t) diff(th2(t), t)],[Dth1 Dth2])
    
    v22_pretty(t) =
    
    (d2*cos(th1(t) + th2(t))*(Dth1 + Dth2) + Dth1*d1*cos(th1(t)))^2 + (d2*sin(th1(t) + th2(t))*(Dth1 + Dth2) + Dth1*d1*sin(th1(t)))^2
  2. collect 函数按照 d1 的降幂次合并同类项

    v22_collect1 = collect(v22_pretty,d1)
    
    v22_collect1(t) =
    
    (Dth1^2*cos(th1(t))^2 + Dth1^2*sin(th1(t))^2)*d1^2 + (2*Dth1*d2*cos(th1(t))*cos(th1(t) + th2(t))*(Dth1 + Dth2) + 2*Dth1*d2*sin(th1(t))*sin(th1(t) + th2(t))*(Dth1 + Dth2))*d1 + d2^2*cos(th1(t) + th2(t))^2*(Dth1 + Dth2)^2 + d2^2*sin(th1(t) + th2(t))^2*(Dth1 + Dth2)^2
  3. 表达式中出现了 cos2θ+sin2θ 的表达式没有进行化简,下面用 simplify 函数进行化简

    v22_simplify = simplify(v22_collect1)
    
    v22_simplify(t) =
    
    Dth1^2*d1^2 + 2*cos(th2(t))*Dth1^2*d1*d2 + Dth1^2*d2^2 + 2*cos(th2(t))*Dth1*Dth2*d1*d2 + 2*Dth1*Dth2*d2^2 + Dth2^2*d2^2
  4. collect 依照 [d1 d2] 降幂次合并同类项

    v22_collect2 = collect(v22_simplify,[d1 d2])
    
    v22_collect2(t) =
    
    Dth1^2*d1^2 + (2*cos(th2(t))*Dth1^2 + 2*Dth2*cos(th2(t))*Dth1)*d1*d2 + (Dth1^2 + 2*Dth1*Dth2 + Dth2^2)*d2^2
  5. 上面的表达式中,还有 cosθ2 没有化简

    v22_collect3 = collect(v22_collect2,cos(th2(t)))
    
    v22_collect3(t) =
    
    d1*d2*(2*Dth1^2 + 2*Dth2*Dth1)*cos(th2(t)) + (Dth1^2 + 2*Dth1*Dth2 + Dth2^2)*d2^2 + Dth1^2*d1^2

4 和5与下面的语句等价

collect(expr,[d1 d2 cos(th2(t))])

最后得到了文章伊始的表达式

后记

在已知结果情况下,省去探索的步骤,1-5的语句可以直接化简为:

v22_pretty = subs(v22,[diff(th1(t), t) diff(th2(t), t)],[Dth1 Dth2]);
v22_simplify = simplify(v22_pretty);
v22_collcet = collect(v22_simplify,[d1 d2 cos(th2(t))]);

依照一般的习惯,表达式的化简多依赖于 simplifycollect ,像类似 expand (展开) horner (嵌套) 等函数通常是我们不希望遇到的。

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值