?3?【例5.2.1-4】写出矩阵?2?4??x2(1)
x2?33x???2x?1x?1?各元素的分子、分母多项式
?3x?4??syms x;A=[3/2,(x^2+3)/(2*x-1)+3*x/(x-1);4/x^2,3*x+4];
[n,d]=numden(A)
pretty(simplify(A)) %<3> n =
[ 3, x^3+5*x^2-3] [ 4, 3*x+4] d =
[ 2, (2*x-1)*(x-1)] [ x^2, 1]
[ 3 2 ] [ x + 5 x - 3 ] [3/2 -----------------] [ (2 x - 1) (x - 1)] [ ] [ 4 ] [---- 3 x + 4 ] [ 2 ]
[ x ]
(2)
pretty(simplify(n./d))
[ 3 2 ]
[ x + 5 x - 3 ] [3/2 -----------------] [ (2 x - 1) (x - 1)] [ ] [ 4 ] [---- 3 x + 4 ] [ 2 ] [ x ] 【例5.2.1-5】简化f?3(1)
syms x;f=(1/x^3+6/x^2+12/x+8)^(1/3); sfy1=simplify(f),sfy2=simplify(sfy1)
1612?2??8 3xxx sfy1 =
((2*x+1)^3/x^3)^(1/3) sfy2 =
((2*x+1)^3/x^3)^(1/3)
(2)
g1=simple(f),g2=simple(g1)
g1 =
(2*x+1)/x g2 = 2+1/x
cosx??sinx【例5.2.1-6】简化ff?
5 / 19
2syms x;ff=cos(x)+sqrt(-sin(x)^2);
ssfy1=simplify(ff),ssfy2=simplify(ssfy1)
ssfy1 =
cos(x)+(-1+cos(x)^2)^(1/2) ssfy2 =
cos(x)+(-1+cos(x)^2)^(1/2)
gg1=simple(ff),gg2=simple(gg1)
gg1 =
cos(x)+i*sin(x) gg2 = exp(i*x)
5.2.2 符号函数的求反和复合
【例5.2.2-1】求f?x2的反函数
syms x;f=x^2;g=finverse(f)
g = x^(1/2)
fg=simple(compose(g,f)) %验算g(f(x))是否等于x
fg = x
【例5.2.2-2】求f?x1?u2,g?cos(y?fai)的复合函数 (1)
syms x y u fai t;f=x/(1+u^2);g=cos(y+fai);fg1=compose(f,g) fg1 =
cos(y+fai)/(1+u^2)
(2)
fg2=compose(f,g,u,fai,t) fg2 =
x/(1+cos(y+t)^2)
5.2.3 置换及其应用
5.2.3.1 自动执行的子表达式置换指令
【例5.2.3.1-1】演示子表达式的置换表示。
clear all,syms a b c d W;[V,D]=eig([a b;c d]);
[RVD,W]=subexpr([V;D],W) RVD =
[ -(1/2*d-1/2*a-1/2*W)/c, -(1/2*d-1/2*a+1/2*W)/c] [ 1, 1]
[ 1/2*d+1/2*a+1/2*W, 0] [ 0, 1/2*d+1/2*a-1/2*W] W =
(d^2-2*a*d+a^2+4*b*c)^(1/2)
5.2.3.2 通用置换指令
6 / 19
%<2>
【例5.2.3.2-1】用简单算例演示subs的置换规则。 (1)
syms a x;f=a*sin(x)+5; f =
a*sin(x)+5
(2)
f1=subs(f,'sin(x)',sym('y')) %<2>
f1 = a*y+5
(3)
f2=subs(f,{a,x},{2,sym(pi/3)}) f2 =
3^(1/2)+5
(4)
f3=subs(f,{a,x},{2,pi/3}) f3 = 6.7321
(5)
f4=subs(subs(f,a,2),x,0:pi/6:pi) f4 =
5.0000 6.0000 6.7321 7.0000 6.7321 6.0000 5.0000
(6)
f5=subs(f,{a,x},{0:6,0:pi/6:pi}) f5 =
5.0000 5.5000 6.7321 8.0000 8.4641 7.5000 5.0000
5.2.4 符号数值精度控制和任意精度计算 5.2.4.1 向双精度数值转换的doblue指令 5.2.4.2 任意精度的符号数值
【例5.2.4.2-1】指令使用演示。 digits Digits = 32
p0=sym('(1+sqrt(5))/2'); p1=sym((1+sqrt(5))/2) e01=vpa(abs(p0-p1)) p1 =
7286977268806824*2^(-52) e01 =
.543211520368251e-16
p2=vpa(p0)
e02=vpa(abs(p0-p2),64)
p2 =
1.6180339887498948482045868343656 e02 =
7 / 19
%<3>
%<4>
%<5> %<6>
.38117720309179805762862135448622e-31
digits Digits = 32
5.2.5 符号对象与其它数据对象间的转换
【例5.2.5-1】符号、数值间的转换。
phi=sym((1+sqrt(5))/2) double(phi)
phi =
7286977268806824*2^(-52) ans = 1.6180
【例5.2.5-2】各种多项式表示形式之间的转换
syms x;f=x^3+2*x^2-3*x+5; sy2p=sym2poly(f)
p2st=poly2str(sy2p,'x') p2sy=poly2sym(sy2p) pretty(f,'x')
sy2p =
1 2 -3 5 p2st =
x^3 + 2 x^2 - 3 x + 5 p2sy =
x^3+2*x^2-3*x+5
5.3 符号微积分
5.3.1 符号序列的求和
t?1【例5.3.1-1】求
??tk3??,(?1)k?t?0??k?1?1?(2k?1)2k?? syms k t;f1=[t k^3];f2=[1/(2*k-1)^2,(-1)^k/k]; s1=simple(symsum(f1))
s2=simple(symsum(f2,1,inf))
s1 =
[ 1/2*t*(t-1), k^3*t] s2 =
[ 1/8*pi^2, -log(2)]
5.3.2 符号微分和
jacobian矩阵
2【例5.3.2-1】求d?at3?d2?2?at3?d?dx??tcosxlnx?、
?dttcosxlnx?和
??a?dxdt?tcosxsyms a t x;f=[a,t^3;t*cos(x), log(x)];
8 / 19
t3?lnx??