MATLAB的代入命令:提取(subexpr)
在进行繁琐的数学运算中,经常会碰到类似这样的情况:在得到的方程的解中,由几个非常长的因子在解中出现很多遍,不管是在纸上还是在屏幕上,它不仅使式子过长变得难看,而且在转抄或粘贴时非常容易出错。MATLAB的subexpr命令可以解决这个问题。它能用一个语句完成筛选相同因子和整理式子的复杂工作。
在使用中,subexpr命令可以带一个或者两个参数。它的完整使用格式为:
[Y,SIGMA]=suberxpr(X,SIGMA)
或
[Y,SIGMA]=subexpr(X,’SIGMA’)
式子中各参数的含义如下。
X:待整理的代数式或代数式矩阵。
SIGMA:在整理过程中提出的各种因子将以矩阵的格式存在名为SIGMA的变量中。
Y:经提取各种因子后,整理完毕的代数式或其矩阵将被保留存在于Y矩阵中。
>> t=solve('a*x^3+b*x^2+c*x+d=0')
[r,s]=subexpr(t,'s')
t =
(((d/(2*a)+ b^3/(27*a^3)- (b*c)/(6*a^2))^2 + (c/(3*a)- b^2/(9*a^2))^3)^(1/2)- b^3/(27*a^3)- d/(2*a)+ (b*c)/(6*a^2))^(1/3)- b/(3*a)- (c/(3*a)- b^2/(9*a^2))/(((d/(2*a)+ b^3/(27*a^3)- (b*c)/(6*a^2))^2 + (c/(3*a)- b^2/(9*a^2))^3)^(1/2)- b^3/(27*a^3)- d/(2*a)+ (b*c)/(6*a^2))^(1/3)
(c/(3*a)- b^2/(9*a^2))/(2*(((d/(2*a)+ b^3/(27*a^3)- (b*c)/(6*a^2))^2 + (c/(3*a)- b^2/(9*a^2))^3)^(1/2)- b^3/(27*a^3)- d/(2*a)+ (b*c)/(6*a^2))^(1/3))- b/(3*a)- (((d/(2*a)+ b^3/(27*a^3)- (b*c)/(6*a^2))^2 + (c/(3*a)- b^2/(9*a^2))^3)^(1/2)- b^3/(27*a^3)- d/(2*a)+ (b*c)/(6*a^2))^(1/3)/2 - (3^(1/2)*((c/(3*a)- b^2/(9*a^2))/(((d/(2*a)+ b^3/(27*a^3)- (b*c)/(6*a^2))^2 + (c/(3*a)- b^2/(9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- d/(2*a)+ (b*c)/(6*a^2)
上例中,s为代数式,可试将a,b,c,d之一、二改为某一具体数字,再用subexpr命令观察一下s的变化。
subexpr的化简使用格式为:
Y=subexpr(X)
式中X,Y含义同前,此时X式中的相同因子将被保存在默认名为SIGMA的变量中。这个操作很简单,就不再举例了。