Matlab学习记录19

书籍:Matlab实用教程

工具:Matlab2021a

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第3章MATLAB的符号计算

3.3 符号表达式的操作和转换

3.3.1 符号表达式中自由变量的确定

跟之前的老版本不同,不支持findsym,而是symvar

cpp 复制代码
>> f=str2sym('a*x^2+b*x+c')
 
f =
 
a*x^2 + b*x + c
 
>> findsym(f)
函数或变量 'findsym' 无法识别。
>> symvar(f)
 
ans =
 
[a, b, c, x]
 
>> g=str2sym('sinx(z)+cos(v)')
 
g =
 
cos(v) + sinx(z)
 
>> symvar(g)
 
ans =
 
[v, z]

3.3.2 符号表达式的化简

1、pretty函数

cpp 复制代码
>> f=str2sym('x^3-6*x^2+11*x-6')
 
f =
 
x^3 - 6*x^2 + 11*x - 6
 
>> g=str2sym('(x-1)*(x-2)*(x-3)')
 
g =
 
(x - 1)*(x - 2)*(x - 3)
 
>> h=str2sym('x*(x*(x-6)+11)-6')
 
h =
 
x*(x*(x - 6) + 11) - 6
 
>> pretty(f)
 3      2
x  - 6 x  + 11 x - 6

2、collect函数

cpp 复制代码
>> collect(g)
 
ans =
 
x^3 - 6*x^2 + 11*x - 6
 
>> f1=str2sym('X^3+2*x^2*y+4*x*y+6')
 
f1 =
 
X^3 + 2*y*x^2 + 4*y*x + 6
 
>> collect(f1,'y')
 
ans =
 
X^3 + y*(2*x^2 + 4*x) + 6

3、expand函数

cpp 复制代码
>> expand(g)
 
ans =
 
x^3 - 6*x^2 + 11*x - 6

4、horner函数

cpp 复制代码
>> horner(f)
 
ans =
 
x*(x*(x - 6) + 11) - 6

5、factor函数

cpp 复制代码
>> factor(f)
 
ans =
 
[x - 3, x - 1, x - 2]

6、simplify函数

cpp 复制代码
>> y=str2sym('cos(x)^2-sin(x)^2')
 
y =
 
cos(x)^2 - sin(x)^2
 
>> simplify(y)
 
ans =
 
cos(2*x)

7、simple函数

不支持该函数

cpp 复制代码
>>  syms x
>> y = cos(x)^2+sin(x)^2;
>> R = simple(y)
函数或变量 'simple' 无法识别。

3.3.3 符号表达式的替换

1、subexpr函数

cpp 复制代码
>> syms a b c d x
>> a=eig([a b;c d])
 
a =
 
a/2 + d/2 - (a^2 - 2*a*d + d^2 + 4*b*c)^(1/2)/2
a/2 + d/2 + (a^2 - 2*a*d + d^2 + 4*b*c)^(1/2)/2
 
>> subexpr(a,x)
 
x = 
 
(a^2 - 2*a*d + d^2 + 4*b*c)^(1/2)/2
 
 
 
ans =
 
a/2 + d/2 - x
a/2 + d/2 + x

2、subs函数

cpp 复制代码
>> f=str2sym('(x+y)^2+3*(x+y)+5')
 
f =
 
3*x + 3*y + (x + y)^2 + 5
 
>> x=5;
>> f1=subs(f)
 
f1 =
 
3*cos(x)^2 - 3*sin(x)^2 + (cos(x)^2 - sin(x)^2 + 5)^2 + 20
 
>> f2=subs(f,str2sym('x+y'),'s')
 
f2 =
 
s^2 + 3*s + 5
 
>> f3=subs(f,str2sym('x+y'),5)
 
f3 =
 
45
 
>> f4=subs(f,'x','z')
 
f4 =
 
3*y + 3*z + (y + z)^2 + 5

3.3.4 求反函数和复合函数

1、求反函数

cpp 复制代码
>> f=str2sym('t*e^x')
 
f =
 
e^x*t
 
>> g=finverse(f)
 
g =
 
log(x/t)/log(e)
 
>> syms t
>> g=finverse(f,t)
 
g =
 
t/e^x

2、求复合函数

cpp 复制代码
>> f=str2sym('t*e^x')
 
f =
 
e^x*t
 
>> g=str2sym('a*x^2+b*y+c')
 
g =
 
a*x^2 + c + b*y
 
>> h1=compose(g,f)
 
h1 =
 
c + b*y + a*e^(2*x)*t^2
 
>> h2=compose(f,g)
 
h2 =
 
e^(a*x^2 + c + b*y)*t
 
>> h3=compose(f,g,'z')
 
h3 =
 
e^(a*z^2 + c + b*y)*t
cpp 复制代码
>> f1=str2sym('t*e^x')
 
f1 =
 
e^x*t
 
>> g1=str2sym('y^2')
 
g1 =
 
y^2
 
>> h1=compose(f1,g1)
 
h1 =
 
e^(y^2)*t
 
>> h2=compose(f1,g1,'z')
 
h2 =
 
e^(z^2)*t
 
>> h3=compose(f1,g1,'t','y')
 
h3 =
 
e^x*y^2
 
>> h4=compose(f1,g1,'t','y','z')
 
h4 =
 
e^x*z^2
 
>> h5=subs(h3,'y','z')
 
h5 =
 
e^x*z^2

3.3.5 符号表达式的转换

1、符号表达式与多项式的转换

cpp 复制代码
>> f=str2sym('2*x+3*x^2+1')
 
f =
 
3*x^2 + 2*x + 1
 
>> sym2poly(f)

ans =

     3     2     1

>> f1=str2sym('a*x^2+b*x+c')
 
f1 =
 
a*x^2 + b*x + c
 
>> sym2poly(f1)
错误使用 sym/sym2poly (第 32 行)
Polynomial has more than one symbolic variable.
cpp 复制代码
>> g=poly2sym([1 3 2])
 
g =
 
x^2 + 3*x + 2
 
>> g1=poly2sym([1 3 2],str2sym('y'))
 
g1 =
 
y^2 + 3*y + 2

2、提取分子和分母

cpp 复制代码
>> f1=str2sym('1/(s^2+3*s+2)')
 
f1 =
 
1/(s^2 + 3*s + 2)
 
>> f2=str2sym('1/s^2+3*s+2')
 
f2 =
 
3*s + 1/s^2 + 2
 
>> [n1,d1]=numden(f1)
 
n1 =
 
1
 
 
d1 =
 
s^2 + 3*s + 2
 
>> [n2,d2]=numden(f2)
 
n2 =
 
3*s^3 + 2*s^2 + 1
 
 
d2 =
 
s^2

3.4 符号极限、微积分和级数求和

3.4.1 符号极限

cpp 复制代码
>> f=str2sym('1/x')
 
f =
 
1/x
 
>> limit(f)
 
ans =
 
NaN
 
>> limit(f,'x',0)
 
ans =
 
NaN
 
>> limit(f,'x',0,'left')
 
ans =
 
-Inf
 
>> limit(f,'x',0,'right')
 
ans =
 
Inf
 
>> syms t x
>> limit((cos(x+t)-cos(x))/t,t,0)
 
ans =
 
-sin(x)

3.4.2 符号微分

cpp 复制代码
>> f=str2sym('a*x^2+b*x+c')
 
f =
 
a*x^2 + b*x + c
 
>> diff(f)
 
ans =
 
b + 2*a*x
 
>> diff(f,'a')
 
ans =
 
x^2
 
>> diff(f,'x',2)
 
ans =
 
2*a
 
>> diff(f,3)
 
ans =
 
0
 
>> syms t x
>> g=[2*x t^2;t*sin(x) exp(x)]
 
g =
 
[     2*x,    t^2]
[t*sin(x), exp(x)]
 
>> diff(g)
 
ans =
 
[       2,      0]
[t*cos(x), exp(x)]
 
>> diff(g,'t')
 
ans =
 
[     0, 2*t]
[sin(x),   0]
 
>> diff(g,2)
 
ans =
 
[        0,      0]
[-t*sin(x), exp(x)]

diff可以计算向量间元素的差值

cpp 复制代码
>> x1=0:0.5:2;
>> y1=sin(x1)

y1 =

         0    0.4794    0.8415    0.9975    0.9093

>> diff(y1)

ans =

    0.4794    0.3620    0.1560   -0.0882

3.4.3 符号积分

cpp 复制代码
>> f=str2sym('cos(x)')
 
f =
 
cos(x)
 
>> int(f)
 
ans =
 
sin(x)
 
>> int(f,0,pi/3)
 
ans =
 
3^(1/2)/2
 
>> int(f,'a','b')
 
ans =
 
sin(b) - sin(a)
 
>> int(int(f))
 
ans =
 
-cos(x)
 
>> syms t x
>> g=[2*x t^2;t*sin(x) exp(x)]
 
g =
 
[     2*x,    t^2]
[t*sin(x), exp(x)]
 
>> int(g)
 
ans =
 
[      x^2,  t^2*x]
[-t*cos(x), exp(x)]
 
>> int(g,'t')
 
ans =
 
[         2*t*x,    t^3/3]
[(t^2*sin(x))/2, t*exp(x)]
 
>> int(g,'a','b')
 
ans =
 
[          b^2 - a^2,    -t^2*(a - b)]
[t*(cos(a) - cos(b)), exp(b) - exp(a)]

3.4.4 符号级数

1、symsum函数

cpp 复制代码
>> syms x k
>> s1=symsum(1/k^2,1,10)
 
s1 =
 
1968329/1270080
 
>> 
>> s2=symsum(1/k^2,1,inf)
 
s2 =
 
pi^2/6
 
>> s3=symsum(x^k,k,0,inf)
 
s3 =
 
piecewise(1 <= x, Inf, abs(x) < 1, -1/(x - 1))

2、taylor函数

cpp 复制代码
>> syms x

>> s2=taylor(exp(x))
 
s2 =
 
x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
 
>> s1=taylor(exp(x),x,8)
 
s1 =
 
exp(8) + exp(8)*(x - 8) + (exp(8)*(x - 8)^2)/2 + (exp(8)*(x - 8)^3)/6 + (exp(8)*(x - 8)^4)/24 + (exp(8)*(x - 8)^5)/120

3.5 符号积分变换

3.5.1 傅里叶变换及其反变换

cpp 复制代码
>> syms t w
>> F=fourier(1/t,t,w)
 
F =
 
-pi*sign(w)*1i
 
>> f=ifourier(F,t)
 
f =
 
1/t
 
>> f=ifourier(F)
 
f =
 
1/x
 
>> fourier(str2sym('Heaviside(t)'))
 
ans =
 
fourier(Heaviside(t), t, w)
 
>> fourier(str2sym('Heaviside(t)'),t,w)
 
ans =
 
fourier(Heaviside(t), t, w)
 
>> g=str2sym('Heaviside(t)')
 
g =
 
Heaviside(t)
 
>> G=fourier(g)
 
G =
 
fourier(Heaviside(t), t, w)
 
>> G=fourier(g,t,w)
 
G =
 
fourier(Heaviside(t), t, w)

3.5.2 拉普拉斯变换及其反变换

cpp 复制代码
>> syms a t s
>> F1=laplace(sin(a*t),t,s)
 
F1 =
 
a/(a^2 + s^2)
 
>> F2=laplace(str2sym('Heaviside(t)'))
 
F2 =
 
laplace(Heaviside(t), t, s)
 
>> f1=ilaplace(1/(s+a),s,t)
 
f1 =
 
exp(-a*t)
 
>> f2=ilaplace(1,s,t)
 
f2 =
 
dirac(t)

3.5.3 Z变换及其反变换

cpp 复制代码
>> syms a n z t
>> Fz1=ztrans(str2sym('Heaviside(t)'),n,z)
 
Fz1 =
 
(z*Heaviside(t))/(z - 1)
 
>> Fz2=ztrans(str2sym('Dirac(t)'),n,z)
 
Fz2 =
 
(z*Dirac(t))/(z - 1)
 
>> Fz3=ztrans(exp(-a*t),n,z)
 
Fz3 =
 
(z*exp(-a*t))/(z - 1)
 
>> f1=iztrans(Fz1,z,n)
 
f1 =
 
Heaviside(t)*kroneckerDelta(n, 0) - Heaviside(t)*(kroneckerDelta(n, 0) - 1)
 
>> f2=iztrans(Fz2,z,n)
 
f2 =
 
Dirac(t)*kroneckerDelta(n, 0) - Dirac(t)*(kroneckerDelta(n, 0) - 1)
 
>> f3=iztrans(Fz3,z,n)
 
f3 =
 
exp(-a*t)*kroneckerDelta(n, 0) - exp(-a*t)*(kroneckerDelta(n, 0) - 1)

3.6 符号方程的求解

3.6.1 代数方程

cpp 复制代码
>> f1=str2sym('a*x^2+b*x+c')
 
f1 =
 
a*x^2 + b*x + c
 
>> solve(f1)
 
ans =
 
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
 
>> f2=str2sym('sin(x)')
 
f2 =
 
sin(x)
 
>> solve(f2,x)
 
ans =
 
0

>> eq1=str2sym('x^2+2*x+1')
 
eq1 =
 
x^2 + 2*x + 1
 
>> eq2=str2sym('x+3*z=4')
 
eq2 =
 
x + 3*z == 4
 
>> eq3=str2sym('y*z=-1')
 
eq3 =
 
y*z == -1
 
>> [x,y,z]=solve(eq1,eq2,eq3)
 
x =
 
-1
 
 
y =
 
-3/5
 
 
z =
 
5/3

3.6.2 符号常微分方程

cpp 复制代码
>> y=dsolve('x*D2y-3*Dy=x^2','x')
警告: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead. 
> 位置:dsolve (第 126 行) 
 
y =
 
C2*x^4 - x^3/3 + C1
 
>> syms x y
>> y=dsolve('x*D2y-3*Dy=x^2','x')
警告: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead. 
> 位置:dsolve (第 126 行) 
 
y =
 
C2*x^4 - x^3/3 + C1
 
>> y=dsolve('x*D2y-3*Dy=x^2',x)
警告: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead. 
> 位置:dsolve (第 126 行) 
 
y =
 
C2*x^4 - x^3/3 + C1
 
>> y=dsolve('x*D2y-3*Dy=x^2','y(1)=0,y(5)=0','x')
警告: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead. 
> 位置:dsolve (第 126 行) 
 
y =
 
(31*x^4)/468 - x^3/3 + 125/468
 
>> [x,y]=dsolve('Dx=y,Dy=-x',t)
警告: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead. 
> 位置:dsolve (第 126 行) 
 
x =
 
C1*cos(t) + C2*sin(t)
 
 
y =
 
C2*cos(t) - C1*sin(t)
 
>> [x,y]=dsolve('Dx=y,Dy=-x')
警告: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations instead. 
> 位置:dsolve (第 126 行) 
 
x =
 
C1*cos(t) + C2*sin(t)
 
 
y =
 
C2*cos(t) - C1*sin(t)


 
相关推荐
jllllyuz2 小时前
基于帧差法与ViBe算法的MATLAB前景提取
开发语言·算法·matlab
.鸣2 小时前
CSDN Java反射
java·学习
wen__xvn2 小时前
代码随想录算法训练营DAY1第一章 数组part01
数据结构·算法·leetcode
MarkHD2 小时前
智能体在车联网中的应用:第37天 CARLA仿真中的端到端车道保持:基于CNN模仿学习的自动驾驶实践
学习·cnn·自动驾驶
一 乐2 小时前
景区管理|基于springboot + vue景区管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习
爱编码的傅同学2 小时前
【程序地址空间】页表的映射方式
c语言·数据结构·c++·算法
week_泽2 小时前
网安基础_1_补充_网络安全渗透测试学习笔记
笔记·学习·web安全
·present·2 小时前
射频网课学习第八章(低噪声放大器)
学习
t198751282 小时前
基于射线理论的水声信道仿真MATLAB程序
开发语言·matlab