《MATLAB基础篇》(五) 符号计算
作者:互联网
目录
- (1) 符号常量创建
- (2) 符号变量创建
- (3) 符号函数和符号方程的创建
- (4) 符号矩阵的创建
- (5) 符号表达式的基本操作(一)
- (6) 符号表达式的基本操作(二)
- (7) 符号运算精度
- (8) 符号函数绘图
- (9) 图形化函数符号计算器
(1) 符号常量创建
用
sym()
函数创建符号常量:
clc;clear;
a1 = sqrt(5) %用默认double创建根号5。
a2 = sqrt(sym(5)) %创建符号变量根号5。
a3 = double(a2) %将符号变量转换成double类型。
a4 = sym(3)/sym(8) %符号型八分之三。
a5 = 3*a4
ca1 = class(a1)
ca2 = class(a2)
ca3 = class(a3)
ca4 = class(a4)
ca5 = class(a5)
>>a1 =
2.2361
a2 =
5^(1/2)
a3 =
2.2361
a4 =
3/8
a5 =
9/8
ca1 =
'double'
ca2 =
'sym'
ca3 =
'double'
ca4 =
'sym'
ca5 =
'sym'
clc;clear;
t = 0.13
y1 = sym(t)
y2 = sym(t,'r') %rational
y3 = sym(t,'f') %fraction
y4 = sym(t,'e')
y5 = sym(t,'d') %double
>>t =
0.1300
y1 =
13/100
y2 =
13/100
y3 =
1170935903116329/9007199254740992
y4 =
eps/50 + 13/100
y5 =
0.13000000000000000444089209850063
(2) 符号变量创建
(2.1) 用sym函数创建单个符号变量
clc;clear;
a = sym('a');
b = sym('b');
c = sym('c');
x = sym('x');
f1 = a * x^2 + b * x + c
g = f1^2 + 3 * f1 + 5
>>f1 =
a*x^2 + b*x + c
g =
3*c + 3*b*x + 3*a*x^2 + (a*x^2 + b*x + c)^2 + 5
(2.2) 用syms函数创建多个符号变量
clc;clear;
syms a b c x;
f1 = a * x^2+b * x + c
g = f1^2 + 3 * f1 + 5
>>f1 =
a*x^2+b*x+c
g =
3*c + 3*b*x + 3*a*x^2 + (a*x^2 + b*x + c)^2 + 5
(3) 符号函数和符号方程的创建
符号函数是一个代数式,符号方程是一个等式。
clc;clear;
syms x y;
f = 4 * (x - 2)^2 + sin(x + y) + 5^x %建立符号函数。
e = 4 * (x - 2)^2 + 5^y == 0 %建立符号方程。
>>f =
sin(x + y) + 4*(x - 2)^2 + 5^x
e =
4*(x - 2)^2 + 5^y == 0
(4) 符号矩阵的创建
clc;clear;
syms x;
A1=[1.5 x sin(x);cos(3)*2/4 4*x exp(x)]
size(A1)
A2=[1/4,2/6,3/5;1/3,8/9,8/4;2/3,3/5,5/7]
size(A2)
>>A1 =
[ 3/2, x, sin(x)]
[ -4458529838789353/9007199254740992, 4*x, exp(x)]
ans =
2 3
A2 =
0.2500 0.3333 0.6000
0.3333 0.8889 2.0000
0.6667 0.6000 0.7143
ans =
3 3
clc;clear;
syms x;
A1=[1.5 x sin(x);cos(3)*2/4 4*x exp(x)]
a=double(5);
A2=A1+a
M1=magic(4)
A3=sym(M1)
M2=[0.4 0.5 0.6;3.12 pi sin(1);log(2) cos(1) 1.333]
A4=sym(M2)
>>A1 =
[ 3/2, x, sin(x)]
[ -4458529838789353/9007199254740992, 4*x, exp(x)]
A2 =
[ 13/2, x + 5, sin(x) + 5]
[ 40577466434915607/9007199254740992, 4*x + 5, exp(x) + 5]
M1 =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
A3 =
[ 16, 2, 3, 13]
[ 5, 11, 10, 8]
[ 9, 7, 6, 12]
[ 4, 14, 15, 1]
M2 =
0.4000 0.5000 0.6000
3.1200 3.1416 0.8415
0.6931 0.5403 1.3330
A4 =
[ 2/5, 1/2, 3/5]
[ 78/25, pi, 3789648413623927/4503599627370496]
[ 6243314768165359/9007199254740992, 1216652631687587/2251799813685248, 1333/1000]
(5) 符号表达式的基本操作(一)
(5.1) 符号变量的查找
clc;clear;
syms m n x y;
f=m^2+5*n+sin(x+5)+exp(5*y)+4
symvar(f) %查找符号表达式中所有的符号变量。
symvar(f,2) %查找符号表达式中2个与x最接近的符号变量。
symvar(f,3) %查找符号表达式中3个与x最接近的符号变量。
>>f =
m^2 + 5*n + exp(5*y) + sin(x + 5) + 4
ans =
[ m, n, x, y]
ans =
[ x, y]
ans =
[ n, x, y]
(5.2) 符号变量的替代
clc;clear;
syms x y;
f=x^2+3*x+5*x*y+6*y+2*y^2;
g1=subs(f,x,3)
g2=subs(f,3) %默认替代x。
g3=subs(f,y,5)
>>g1 =
2*y^2 + 21*y + 18
g2 =
2*y^2 + 21*y + 18
g3 =
x^2 + 28*x + 80
clc;clear;
syms x y;
f=x^2+3*x+5*x*y+6*y+2*y^2
g1=subs(f,{x,y},{3,5})
g2=subs(exp(x*y),x,magic(4))
>>f =
x^2 + 5*x*y + 3*x + 2*y^2 + 6*y
g1 =
173
g2 =
[ exp(16*y), exp(2*y), exp(3*y), exp(13*y)]
[ exp(5*y), exp(11*y), exp(10*y), exp(8*y)]
[ exp(9*y), exp(7*y), exp(6*y), exp(12*y)]
[ exp(4*y), exp(14*y), exp(15*y), exp(y)]
(5.3) 符号表达式的展开
clc;clear;
syms x y;
f1=(x-y)^2+(x+y)^3 %多项式展开。
g1=expand(f1)
f2=sin(x-3*y) %三角函数展开。
g2=expand(f2)
f3=exp(2*x^2+4*y) %指数函数展开。
g3=expand(f3)
>>f1 =
(x - y)^2 + (x + y)^3
g1 =
x^3 + 3*x^2*y + x^2 + 3*x*y^2 - 2*x*y + y^3 + y^2
f2 =
sin(x - 3*y)
g2 =
cos(x)*sin(y) - 3*cos(y)*sin(x) + 4*cos(y)^3*sin(x) - 4*cos(x)*cos(y)^2*sin(y)
f3 =
exp(2*x^2 + 4*y)
g3 =
exp(4*y)*exp(2*x^2)
(5.4) 符号表达式的化简
clc;clear;
syms x y;
f1=(2*x^4-2*x)/(2*x^2+4*x+2);
sf1=simplify(f1)
f2=2*cos(x)^2-3*sin(x)^2-2;
sf2=simplify(f2)
sf3=simplify(sin(x)^2+cos(x)^2)
>>sf1 =
-(- x^4 + x)/(x + 1)^2
sf2 =
-5*sin(x)^2
sf3 =
1
(5.5) 符号表达式的算数运算
符号表达式可以进行加减乘除四则运算,但是只能进行等于(
==
)和不等于(~=
)的比较运算。
(5.6) 符号多项式的因式分解
clc;clear;
syms x y;
factor(x^4-y^4+x^2-y^2)
for i=1:8
disp(factor(x^i-1))
end
>>ans =
[ x + y, x - y, x^2 + y^2 + 1]
x - 1
[ x - 1, x + 1]
[ x - 1, x^2 + x + 1]
[ x - 1, x + 1, x^2 + 1]
[ x - 1, x^4 + x^3 + x^2 + x + 1]
[ x - 1, x + 1, x^2 + x + 1, x^2 - x + 1]
[ x - 1, x^6 + x^5 + x^4 + x^3 + x^2 + x + 1]
[ x - 1, x + 1, x^2 + 1, x^4 + 1]
clc;clear;
y1=factor(1234567890)
y2=factor(sym('12345678901234567890'))
>>y1 =
2 3 3 5 3607 3803
y2 =
[ 2, 3, 3, 5, 101, 3541, 3607, 3803, 27961]
(5.7) 合并符号表达式的同类项
clc;clear;
syms x y;
f=-1/4*y*exp(-2*x)+2/5*x*exp(-2*x)+3*exp(-2*x)
y1=collect(f,exp(-2*x))
y2=collect(x^2*y+y*x+3*y^2*x^3-2*x^2-2*x)
y3=collect(x^2*y+y*x+3*y^2*x^3-2*x^2-2*x,'y')
>>f =
3*exp(-2*x) + (2*x*exp(-2*x))/5 - (y*exp(-2*x))/4
y1 =
((2*x)/5 - y/4 + 3)*exp(-2*x)
y2 =
3*y^2*x^3 + (y - 2)*x^2 + (y - 2)*x
y3 =
3*x^3*y^2 + (x^2 + x)*y - 2*x^2 - 2*x
(5.8) 将符号表达式转换为嵌套形式
clc;clear;
syms x y;
f1=x^4+6*x^3+4*x^2-4
g1=horner(f1)
f2=[x^2-2*x+4 x^3-3*x^2+5;x^2-4*x+6 4*x^2+6*x-8]
g2=horner(f2)
>>f1 =
x^4 + 6*x^3 + 4*x^2 - 4
g1 =
x^2*(x*(x + 6) + 4) - 4
f2 =
[ x^2 - 2*x + 4, x^3 - 3*x^2 + 5]
[ x^2 - 4*x + 6, 4*x^2 + 6*x - 8]
g2 =
[ x*(x - 2) + 4, x^2*(x - 3) + 5]
[ x*(x - 4) + 6, x*(4*x + 6) - 8]
(5.9) 获取符号分式的分子和分母
clc;clear;
syms x y;
[n1,d1]=numden(sym(3/4))
f=(x/y+3*y/x);
[n2,d2]=numden(f)
>>n1 =
3
d1 =
4
n2 =
x^2 + 3*y^2
d2 =
x*y
(6) 符号表达式的基本操作(二)
(6.1) 符号反函数运算
clc;clear;
syms x y;
f=sin(x)+cos(y)+5
g1=finverse(f) %默认以x为自变量。
g2=finverse(f,x)
g3=finverse(f,y)
>>f =
cos(y) + sin(x) + 5
g1 =
-asin(cos(y) - x + 5)
g2 =
-asin(cos(y) - x + 5)
g3 =
acos(y - sin(x) - 5)
(6.2) 符号函数的复合
clc;clear;
syms x y z t u;
f=cos(x/t);
g=sin(y/u);
c1=compose(f,g)
c2=compose(f,g,z)
c3=compose(f,g,x,z)
c4=compose(f,g,t,z)
c5=compose(f,g,x,y,z)
c6=compose(f,g,x,u,z)
c7=compose(f,g,t,u,z)
>>c1 =
cos(sin(y/u)/t)
c2 =
cos(sin(z/u)/t)
c3 =
cos(sin(z/u)/t)
c4 =
cos(x/sin(z/u))
c5 =
cos(sin(z/u)/t)
c6 =
cos(sin(y/z)/t)
c7 =
cos(x/sin(y/z))
(6.3) 求符号表达式的极限
clc;clear;
syms x h;
y1=limit((cos(x+h)-cos(x))/h,h,0)
y2=limit(((x+h)^3-x^3)/h,h,0)
>>y1 =
-sin(x)
y2 =
3*x^2
(6.4) 求符号函数的微分
用
diff()
进行一元函数的微分和求导运算,用jacobian()
对多元符号函数求导。
clc;clear;
syms x y;
f=5*x^4+y*sin(x)+x*cos(y)+6
g1=diff(f)
g2=diff(f,4)
g3=diff(f,x,4)
g4=diff(f,y,4)
>>f =
x*cos(y) + y*sin(x) + 5*x^4 + 6
g1 =
cos(y) + y*cos(x) + 20*x^3
g2 =
y*sin(x) + 120
g3 =
y*sin(x) + 120
g4 =
x*cos(y)
clc;clear;
syms x y;
f=[5*x^4 y*sin(x) 4*x x*cos(y)]
g2=jacobian(f,[x,y])
>>f =
[ 5*x^4, y*sin(x), 4*x, x*cos(y)]
g2 =
[ 20*x^3, 0]
[ y*cos(x), sin(x)]
[ 4, 0]
[ cos(y), -x*sin(y)]
(6.5) 求符号函数的积分
clc;clear;
syms x;
y=int(exp(sin(x))*cos(x),'x') %不定积分。
>>y =
exp(sin(x))
clc;clear;
syms x;
y=int(sin(x),'x',0,pi) %定积分。
>>y =
2
(6.6) 符号表达式的级数
(6.6.1) 级数求和
clc;clear;
syms x k;
y1=symsum(sin(x)+cos(k),x,1,5) %对x求1~5项的和。
y2=symsum(1/k^2,1,Inf) %对k求1到正无穷项的和。
>>y1 =
sin(1) + sin(2) + sin(3) + sin(4) + sin(5) + 5*cos(k)
y2 =
pi^2/6
(6.6.2) 级数展开
clc;clear;
syms x;
f1=taylor(exp(x))
f2=taylor(exp(x),x,0)
f3=taylor(exp(x),x,0,'order',8)
>>f1 =
x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
f2 =
x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
f3 =
x^7/5040 + x^6/720 + x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
(6.7) 符号代数方程求解
clc;clear;
syms a b c x;
y1=solve(a*x^2+b*x+c==0)
y2=solve(3*x^2+8*x+5==0)
y3=solve(x^2-2*x+6==0)
>>y1 =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
y2 =
-5/3
-1
y3 =
1 - 5^(1/2)*1i
1 + 5^(1/2)*1i
clc;clear;
syms x y;
e1=y*sin(x)==5
g1=solve(e1)
g2=solve(e1,y)
>>e1 =
y*sin(x) == 5
g1 =
asin(5/y)
pi - asin(5/y)
g2 =
5/sin(x)
(6.8) 符号代数方程组求解
clc;clear;
syms x y;
e1=x^2+4*x+y^2+5*y-6==0
e2=x^2+2*x+y^2-4==0
g=solve(e1,e2) %默认解x、y。
g.x
g.y
>>e1 =
x^2 + 4*x + y^2 + 5*y - 6 == 0
e2 =
x^2 + 2*x + y^2 - 4 == 0
g =
包含以下字段的 struct:
x: [2×1 sym]
y: [2×1 sym]
ans =
(5*129^(1/2))/29 - 21/29
- (5*129^(1/2))/29 - 21/29
ans =
20/29 - (2*129^(1/2))/29
(2*129^(1/2))/29 + 20/29
clc;clear;
syms x y a b;
e1=a*x+b*y-6==0
e2=b*x+a*y-4==0
g1=solve(e1,e2) %默认解x、y。
g1.x
g1.y
g2=solve(e1,e2,a,b) %解a、b。
g2.a
g2.b
>>e1 =
a*x + b*y - 6 == 0
e2 =
a*y + b*x - 4 == 0
g1 =
包含以下字段的 struct:
x: [1×1 sym]
y: [1×1 sym]
ans =
(2*(3*a - 2*b))/(a^2 - b^2)
ans =
(2*(2*a - 3*b))/(a^2 - b^2)
g2 =
包含以下字段的 struct:
a: [1×1 sym]
b: [1×1 sym]
ans =
(2*(3*x - 2*y))/(x^2 - y^2)
ans =
(2*(2*x - 3*y))/(x^2 - y^2)
(6.9) 符号微分方程求解
clc;clear;
syms a y;
y=dsolve('Dy==a*y')
y=dsolve('Dy==a*y','y(0)=2')
>> y =
C4*exp(a*t)
y =
2*exp(a*t)
clc;clear;
dsolve('Dx=-a*x')
dsolve('Dx=-a*x','x(0)=1')
dsolve('Dx=-a*x','x(0)=1','s')
>>ans =
C6*exp(-a*t)
ans =
exp(-a*t)
ans =
exp(-a*s)
clc;clear;
g1=dsolve('D2y=-a^2*y')
g2=dsolve('D2y=-a^2*y','y(0)=1')
g3=dsolve('D2y=-a^2*y','y(0)=1','y(pi)=1')
>>g1 =
C9*exp(-a*t*1i) + C10*exp(a*t*1i)
g2 =
C13*exp(a*t*1i) - exp(-a*t*1i)*(C13 - 1)
g3 =
- (exp(-a*t*1i)*(exp(pi*a*1i) - 1))/(exp(-pi*a*1i) - exp(pi*a*1i)) +(exp(a*t*1i)*(exp(-pi*a*1i) - 1))/(exp(-pi*a*1i) - exp(pi*a*1i))
(6.10) 符号微分方程组求解
clc;clear;
[x1,y1]=dsolve('Dx=y','Dy=-x','t')
[x2,y2]=dsolve('Dx=y','Dy=-x','x(0)=3','y(0)=4','t')
>>x1 =
C17*cos(t) + C16*sin(t)
y1 =
C16*cos(t) - C17*sin(t)
x2 =
5*cos(t - atan(4/3))
y2 =
5*cos(t + atan(3/4))
clc;clear;
S=dsolve('Du=v,Dv=w,Dw=-u','u(0)=0,v(0)=0,w(0)=1')
[u,v,w]=dsolve('Du=v,Dv=w,Dw=-u','u(0)=0,v(0)=0,w(0)=1')
>>S =
包含以下字段的 struct:
v: [1×1 sym]
u: [1×1 sym]
w: [1×1 sym]
u =
exp(-t)/3 - (cos((3^(1/2)*t)/2)*exp(t)^(1/2))/3 + (3^(1/2)*sin((3^(1/2)*t)/2)*exp(t)^(1/2))/3
v =
(cos((3^(1/2)*t)/2)*exp(t)^(1/2))/3 - exp(-t)/3 + (3^(1/2)*sin((3^(1/2)*t)/2)*exp(t)^(1/2))/3
w =
exp(-t)/3 + (2*cos((3^(1/2)*t)/2)*exp(t)^(1/2))/3
(7) 符号运算精度
digits(d)
:调用该函数后,符号对象的近似解的精度变成d位有效数字,参数d的默认值为32位。D = digits
:调用该函数后,得到当前采用的数值计算的精度。
(8) 符号函数绘图
(8.1) 用符号函数绘制曲线
(8.1.1) fplot
clc;clear;
x=-2:0.1:4;
subplot(2,2,1)
plot(x,humps(x));
title('plot');
subplot(2,2,2)
fplot(@humps,[-2 4])
title('fplot');
subplot(2,2,3)
syms x;
fplot([1/x,sin(x),cos(x)]);
legend('1/x','sin(x)','cos(x)');
(8.1.2) ezplot
clc;clear;
figure;
subplot(2,2,1)
ezplot('x^2-6*x+9*sin(x)+4',[-2*pi,2*pi]);
subplot(2,2,2)
ezplot('x^2-5*y+3*x*y+4*y^2',[-3,1,-3,3]);
subplot(2,2,3)
ezplot('sin(4*t)*cos(2*t)','cos(4*t)*sin(2*t)',[0,pi]);
(8.1.3) ezpolar
clc;clear;
figure;
subplot(2,2,1)
ezpolar('sin(2*t).*cos(3*t)',[0,pi]);
subplot(2,2,2)
ezpolar('1+cos(3*t)',[0,2*pi]);
subplot(2,2,3)
ezpolar(@cos,[0,pi]);
(8.1.4) ezplot3
clc;clear;
figure;
subplot(1,2,1)
ezplot3('cos(t)','t*sin(t)','sqrt(t)',[0,2*pi]);
subplot(1,2,2)
ezplot3(@cos,'t*sin(t)',@sqrt,[0,5*pi],'animate');
(8.2) 用符号函数绘制曲面
(8.2.1) ezmesh
clc;clear;
figure;
subplot(2,2,1)
ezmesh('exp(-x)*cos(y)');
subplot(2,2,2)
ezmesh('x.*exp(-x.^2-y.^2)');
subplot(2,2,3)
ezmesh('-x.^2-y.^2','circ');
subplot(2,2,4)
ezmesh('x*y-y',[-3,3,-4,4]);
(8.2.2) ezmeshc
clc;clear;
figure;
subplot(1,2,1)
ezmeshc('x.*exp(-5*x.^2-8*y.^2)');
subplot(1,2,2)
ezmeshc('x.^2+y.^2',[-3 3 -3 3]);
(8.2.3) ezcontour
clc;clear;
figure;
subplot(2,2,1)
ezcontour('x*sin(y)',[-5 5 -5 5]);
subplot(2,2,2)
ezcontourf('x*exp(-x.^2-y.^2)');
subplot(2,2,3)
ezcontourf('x^2-y^2-1',[-4 4 -5 5]);
(8.2.4) ezsurf
clc;clear;
figure;
subplot(1,2,1)
ezsurf('x.*y.^2/(x.^2+y.^4)');
subplot(1,2,2)
ezsurf('s*sin(t)','s*cos(t)','t',[0,4*pi]);
(8.2.5) ezsurfc
clc;clear;
figure;
subplot(1,2,1)
ezsurfc('4*x.*exp(-4*x.^2 - 6*y.^2)');
subplot(1,2,2)
ezsurfc('x.*y.^2/(x.^2+y.^4)');
(9) 图形化函数符号计算器
funtool
:打开单变量函数符号计算器。taylortool
:打开泰勒级数逼近计算器。
参考资料来源:
- B站Up主.古德谓尔.
- 中国大学MOOC《科学计算与MATLAB语言》.刘卫国 、蔡旭晖 、吕格莉 、何小贤.中南大学.
- 《MATLAB软件与基础数学实验》.李换琴、朱旭、王勇茂、籍万新.西安交通大学出版社.
- 《MATLAB R2018a完全自学一本通》.刘浩、韩晶.电子工业出版社.
本文作者:
Aiden Lee
特别声明:
文章仅供学习参考,转载请注明出处,严禁盗用!
标签:cos,符号,clear,基础,clc,MATLAB,exp,符号计算,sin 来源: https://blog.csdn.net/dasdkhaksd/article/details/110927204