文章目录
符号对象和表达式
基本符号对象和运算符
生成符号对象的规则
- 必须借助
sym syms symfun
定义 - 任何包含符号对象的表达式或方程也是符号对象
符号常数的定义
sym(Num) 如果Num是一个小数,则用此方法创建
sym(‘Num’) 如果Num是一个有理分数,则用此方法创建
对于sym(Num)
的说明:
- 如果
Num
是精准的浮点数,则能创建精准的符号常数 - 如果
Num
是诸如sin(0.3)
的数值表达式,那么sym(Num)
只能生成由表达式获得的16位精度的近似符号常数
>> a = sym(0.3)
a =
3/10
>> b = sym(sin(0.3))
b =
5323618770401843/18014398509481984
>> whos
Name Size Bytes Class Attributes
a 1x1 112 sym
b 1x1 112 sym
对sym(‘Num’)
的说明
- 如果
Num
是普通小数,如0.31,3.21e-1
,那么只能产生近似符号常数
基本符号变量
创建单个符号变量
syms para
syms para Flag Flag是给para划定范围用的,比如当Flag为real时,表示限定para为实域
创建多个符号变量
syms para1 para2 ...
syms para1 para2 para3 ... Flag 注意中间是空格,Flag的作用同上
>> syms a
>> syms b c 'real'
>> whos
Name Size Bytes Class Attributes
a 1x1 112 sym
b 1x1 112 sym
c 1x1 112 sym
>> assumptions %assumptions是用来查看符号变量做了什么限定性条件,这里b,c都为实域上的数
ans =
[ b in R_, c in R_]
符号表达式和符号函数
符号表达式和符号函数
符号表达式:由基本符号对象构成的衍生符号对象,可借助“=“赋义给某符号变量。
不建议使用sym('a*x^2+b*x+c')
生成串型符号表达式。
符号函数:syms f(x,y)
定义了一个以x,y
为变量的函数f
>> syms a b c x y;
>> y=a*x^2+b*x+c %将符号表达式赋给y
y =
a*x^2 + b*x + c
>> syms f(x,y) %定义一个符号函数f(x,y)
>> whos
Name Size Bytes Class Attributes
a 1x1 112 sym
b 1x1 112 sym
c 1x1 112 sym
f 1x1 112 symfun
x 1x1 112 sym
y 1x1 112 sym
自由符号变量
MATLAB
中有一个命令可以列出表达式中的基本符号变量
symvar(expression)
列出expression
中所有的符号变量symvar(expression,n)
列出n
个自由符号变量,这n
个的规则按离x
的距离列出(先右后左),比如前3个为x,y,w
(y
在x
的右边,w
在x
的左边)
>> syms x y w
>> f = x + y + w;
>> symvar(f,2)
ans =
[ x, y]
符号对象的识别
识别数据对象属性的命令
class(var)
给出var
的数据类别isa(var,’Obj’)
判断var
是否为Obj
代表的类,是返回1
whos
给出MATLAB
内存变量的属性
a = 1; b = 2; c = 3; d = 4;
Mn = [a,b;c,d]
Mc = '[a,b;c,d]'
Ms = sym(Mc)
Mn =
1 2
3 4
Mc =
[a,b;c,d]
Ms =
[ a, b]
[ c, d]
CMn = class(Mn)
CMc = class(Mc)
CMs = class(Ms)
CMn =
double
CMc =
char
CMs =
sym
whos Mn Mc Ms
Name Size Bytes Class Attributes
Mc 1x9 18 char
Mn 2x2 32 double
Ms 2x2 112 sym
符号变量假设
对符号变量的限定性假设
assume(assumption)
,假设限定在assumption
assume(expr,set)
表达式限定在集合set
,set
可以为’real’,’integer’’rational’
assumeAlso(assumption)
增加新假设assumption
a = sym(‘a’,res)
、syms a res
创建带res
限制的a
,res
只能为’real’
或’positive’
撤销限制
clear
,清除MATLAB
内存中的x
,但没有清除x的假设,如果再定义一个x
,那么x
的假设还在syms x clear
,sym(‘x’,’clear’)
以上两个都是撤销所有对x
的假设,没有删除x
assumptions(x)
显示对x
的所有假设reset(symengine)
重启MuPAD
引擎,清空MuPAD
中所有内容
在默认的复数域求根
%对x没有做任何假设,默认为复数域
clear all
syms x
f = x^3 + 475*x/100 +5/2;
r = solve(f,x)
r =
-1/2
(79^(1/2)*i)/4 + 1/4
1/4 - (79^(1/2)*i)/4
在实数域求根
assume(x,'real') %将x限制在real
r21 = solve(f,x)
r21 =
-1/2
%另一种限制x在实数域的方法
syms x clear
assume(imag(x) == 0)
r22 = solve(f,x)
r22 =
-1/2
求第一、第四象限的根
syms x clear
assume(real(x) > 0) %x的实部大于0即在一、四象限
r3 = solve(f,x)
r3 =
(79^(1/2)*i)/4 + 1/4
1/4 - (79^(1/2)*i)/4
求在第一象限的根
assumeAlso(imag(x) > 0) %同时追加条件x的虚部也大于0
r4 = solve(f,x)
r4 =
(79^(1/2)*i)/4 + 1/4
符号数字及表达式操作
符号数字转换成双精度数字
double(sym_Num)
符号数字的任意精度表达
digits
,显示当前环境下十进制符号数字的有效位数digits(n)
,设置有效位数为n
位vpa(x)
,根据digits
设置的有效数字显示x
vap(x,n)
,以n
位有效数字显示x
,该命令设置的有效位数只对该命令有效,其他情况的有效位数以digits
设置的为准。
符号化简
simplify(expr)
对expr
运用多种方法进行一轮简化
syms x
f = (1/x^3 + 6/x^2 + 12/x +8)^(1/3)
g1 = simplify(f)
f =
(12/x + 6/x^2 + 1/x^3 + 8)^(1/3)
g1 =
((2*x + 1)^3/x^3)^(1/3)
g2 = simplify(f,'Steps',10,'IgnoreAnalyticConstraints',true)
g2 =
1/x + 2
对simplify(f,'Steps',10)
的说明
‘Steps’
为化简的次数,这里选为10
表达式中的置换操作
公共子式简化
RS = subexpr(S)
,从S
中提取出公共的子式sigama
,并把用sigama
重写的S
赋给RS
RS = subexpr(S,’w’)
,从S
中提取出公共子式并把子式命名为w
,然后把用w
重写的S
赋值给RS
[Rs w] = subexpr(S,’w’)
,与上面一样
A = '[a11,a12,a13;a21,a22,a23;a31,a32,a33]';
SA = sym(A); %生成符号矩阵
det_A = det(SA) %求矩阵的行列式
inv_A = inv(SA) %求矩阵的逆
Sinv_A = subexpr(inv_A,'w') %提取矩阵逆中的公共子式并简化表达式
det_A =
a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31
inv_A =
[ (a22*a33 - a23*a32)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31), -(a12*a33 - a13*a32)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31), (a12*a23 - a13*a22)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31)]
[ -(a21*a33 - a23*a31)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31), (a11*a33 - a13*a31)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31), -(a11*a23 - a13*a21)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31)]
[ (a21*a32 - a22*a31)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31), -(a11*a32 - a12*a31)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31), (a11*a22 - a12*a21)/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31)]
w =
1/(a11*a22*a33 - a11*a23*a32 - a12*a21*a33 + a12*a23*a31 + a13*a21*a32 - a13*a22*a31)
Sinv_A =
[ w*(a22*a33 - a23*a32), -w*(a12*a33 - a13*a32), w*(a12*a23 - a13*a22)]
[ -w*(a21*a33 - a23*a31), w*(a11*a33 - a13*a31), -w*(a11*a23 - a13*a21)]
[ w*(a21*a32 - a22*a31), -w*(a11*a32 - a12*a31), w*(a11*a22 - a12*a21)]
>>
通用置换命令
subs(ES,old,new)
用new
置换ES
中的old
syms a b x
t = 0:pi/20:2*pi;
f1 = a * sin(x) + b
f2 = subs(f1,{a,x,b},{1,t,0});
plot(t,f2,'r')
符号微积分
极限和导数
函数 | 功能 |
---|---|
limit(f,v,a) |
求极限 |
limit(f,v,a,’right’) |
求右极限 |
limit(f,v,a,’left’) |
求左极限 |
diff(f,v,n) |
求导,n 缺省时,默认为1 |
jacobian(f,v) |
求多元函数f 的雅克比矩阵 |
taylor(g) |
麦克劳林5 阶展开 |
taylor(g,v,a,Name,Value) |
把g(v) 在v = a 展开,Value 与Name 必须成对出现。Value 一般为’Order’ ,Name 为一个整数n ,表示阶段误差阶次为n |
求两个重要极限
syms k t x
f1 = sin(k*t)/(k*t);
r1 = limit(f1,0) %求极限
f2 = (1-1/x)^(k*x);
r2 = limit(f2,x,inf) %求极限
r1 =
1
r2 =
exp(-k)
syms a t x;
f = [a,t^3;t*cos(x),log(x)];
df = diff(f) %f默认对x求一阶导
dfdt2 = diff(f,t,2) %f对t求二阶导
dfdxdt = diff(diff(f,x),t) %先对x求导,然后对t求导
df =
[ 0, 0]
[ -t*sin(x), 1/x]
dfdt2 =
[ 0, 6*t]
[ 0, 0]
dfdxdt =
[ 0, 0]
[ -sin(x), 0]
求Jacobian
矩阵 。
syms x1 x2
f = [x1 * exp(x2);x2;cos(x1)*sin(x2)];
v = [x1;x2]; %写成v = [x1, x2]也是一样的
Jf = jacobian(f,v)
Jf =
[ exp(x2), x1*exp(x2)]
[ 0, 1]
[ -sin(x1)*sin(x2), cos(x1)*cos(x2)]
隐函数求导。
设 ,求
syms x y
f(x,y) = sym('cos(x+sin(y(x))) == sin(y(x))'); %y必须写成y(x)
dfdx = diff(f,x,1)
dfdx(x, y) =
-sin(x + sin(y(x)))*(D(y)(x)*cos(y(x)) + 1) == D(y)(x)*cos(y(x))
dfdx1 = subs(dfdx,'D(y)(x)','dydx') %用dydx替代D(y)(x),这一步是必须的
dfdx1(x, y) =
-sin(x + sin(y(x)))*(dydx*cos(y(x)) + 1) == dydx*cos(y(x))
dydx = simplify(solve(dfdx1,'dydx')) 求解得隐函数导数
dydx =
-sin(x + sin(y(x)))/(cos(y(x))*(sin(x + sin(y(x))) + 1))
在x = 0
处展开的5
阶和8
阶Maclaurin
级数。
syms x
f(x) = x*exp(x);
r5 = taylor(f)
r8 = taylor(f,'Order',9)
pretty(r8)
r5(x) =
x^5/24 + x^4/6 + x^3/2 + x^2 + x
r8(x) =
x^8/5040 + x^7/720 + x^6/120 + x^5/24 + x^4/6 + x^3/2 + x^2 + x
8 7 6 5 4 3
x x x x x x 2
---- + --- + --- + -- + -- + -- + x + x
5040 720 120 24 6 2
pretty
可以把一个物理行表示扩展成多个物理行表示
级数符号求和
symsum(f,v,a,b)
,函数f
的变量v
取遍[a,b]
所有数之和
syms n k
f1 = 1/(k * (k+1));
s1 = symsum(f1,k,1,inf)
s1 =
1
syms x
f2 = x^(2*k - 1)/(2*k-1);
s2 = symsum(f2,k,1,inf)
s2 =
piecewise([abs(x) < 1, atanh(x)])
f3 = [1/(2*k-1)^2 , (-1)^k/k];
s3 = symsum(f3,k,1,inf)
s3 =
[ pi^2/8, -log(2)]
符号积分
int(f,v)
不定积分int(f,v,a,b)
[a,b]
上定积分
不定积分
syms x
intf = int(x*log(x),x)
intf =
(x^2*(log(x) - 1/2))/2
求三重积分
syms x y z
F2 = int(int(int(x^2+y^2+z^2,z,sqrt(x*y),x^2*y),y,sqrt(x),x^2),x,1,2)
VF2 = vpa(F2)
F2 =
(14912*2^(1/4))/4641 - (6072064*2^(1/2))/348075 + (64*2^(3/4))/225 + 1610027357/6563700
VF2 =
224.92153573331143159790710032805
微分方程的符号解法
命名格式常见的两种
dsolve(‘eq1, … , eqn’, ‘cond1, … , condn’, ‘v’)
dsolve(‘eq1’, … , ‘eqn’, ‘cond1’, … , ‘condn’, ‘v’)
输入的三部分为:微分方程,边值条件,指定的独立变量,若不指定独立变量,则默认为t,当y是因变量时,Dny表示y的n阶导数,边值条件必须写成y(a) = b, Dy(c) = d …
的形式
S = dsolve('Dx = y, Dy = -x')
disp(' ')
disp(['微分方程的解',blanks(2),'x',blanks(22),'y'])
disp([S.x,S.y])
S =
y: [1x1 sym]
x: [1x1 sym]
微分方程的解 x y
[ C2*cos(t) + C1*sin(t), C1*cos(t) - C2*sin(t)]
y = dsolve('x*D2y - 3*Dy = x^2','y(1) = 0,y(5) = 0','x')
y =
(31*x^4)/468 - x^3/3 + 125/468
符号变换和符号卷积
Fourier变换
fourier(f,t,w)
时域变频域ifourier(F,w,t)
频域变时域
单位阶跃函数的傅里叶变换。
syms t w
ut = heaviside(t);
UT = fourier(ut)
UT =
pi*dirac(w) - i/w
傅里叶反变换验证
Ut = ifourier(UT,w,t)
SUt = simplify(Ut)
SUt =
heaviside(t)
Laplace变换
Msyms t s a b
f1 = exp(-a*t)*sin(b*t);
F1 = laplace(f1,t,s);
pretty(F1)
b
-------------
2 2
(a + s) + b
Z变换及其反变换
ztrans(f,n,z)
iztrans(F,z,n)
syms n z clear
gn = 6*(1-(1/2)^n);
G = ztrans(gn,n,z);
pretty(G)
6 z 6 z
----- - -----
z - 1 1
z - -
2
符号卷积
方法一:定义实现
syms T t tao s
xt = exp(-t);
ht = exp(-t/T)/T;
uhtao = subs(xt,t,tao)*subs(ht,t,t-tao);
yt1 = int(uhtao,tao,0,t)
yt1 =
-(exp(-t) - exp(-t/T))/(T - 1)
方法二:Laplace
变换实现
yt21 = ilaplace(laplace(ut,t,s)*laplace(ht,t,s),s,t);
yt22 = collect(yt21,'(T-1)')
yt22 =
(exp(-t/T) - exp(-t))/(T - 1)
代数方程求解
线性方程组符号解
A = sym([1 1/2 1/2 -1; 1 1 -1 1; 1 -1/4 -1 1; -8 -1 1 1]);
b = sym([0;10;0;1]);
x1 = A\b
x1 =
1
8
8
9
一般代数方程的解
solve(‘eq1’, ‘eq2’, … , ‘eqn’, ‘v1’, ‘v2’, … , ‘vn’)
solve(expr1, expr2, … , exprn, ‘v1’, ‘v2’, … , ‘vn’)
S = solve('u*y^2 + v*z + w = 0', 'y + z + w = 0','y','z')
disp('S.y'),disp(S.y),disp('S.z'),disp(S.z)
S =
y: [2x1 sym]
z: [2x1 sym]
S.y
(v + 2*u*w + (v^2 + 4*u*w*v - 4*u*w)^(1/2))/(2*u) - w
(v + 2*u*w - (v^2 + 4*u*w*v - 4*u*w)^(1/2))/(2*u) - w
S.z
-(v + 2*u*w + (v^2 + 4*u*w*v - 4*u*w)^(1/2))/(2*u)
-(v + 2*u*w - (v^2 + 4*u*w*v - 4*u*w)^(1/2))/(2*u)
syms x
s = solve((x+2)^x == 2,x) %注意==号的使用
xs = (s+2)^s %验算
s =
0.69829942170241042826920133106081 %当没有解析解时,给出近似解
xs =
2.0