# MATLAB学习笔记（十）

- 符号变量的创建与运算
- 数学表达式的分解与化简
- 符号表达式求解
- 方程式求解
- 表达式的符号微分运算
- 表达式的积分运算

## 一.符号代数

1.syms申明变量

>> syms x

>> x = sym('x')
x =
x

2.创建表达式

>> D = D0 * exp(-Q/(R+T))
D =
D0*exp(-Q/(R + T))

>> E=sym('m*c^2')
E =
c^2*m

3.提取分子和分母

>> y = 2 * (x + 3) ^ 2 / (x ^ 2 + 6 * x + 9)
y =
(2*(x + 3)^2)/(x^2 + 6*x + 9)

>> [num,den] = numden(y)
num =
2
den =
1
>>

>> w = (x + 3)^2/(x+4)^2
w =
(x + 3)^2/(x + 4)^2
>> [num,den]=numden(w)
num =
(x + 3)^2
den =
(x + 4)^2

>> num * den
ans =
(x + 3)^2*(x + 4)^2
>> num/den
ans =
(x + 3)^2/(x + 4)^2
>> num+den
ans =
(x + 3)^2 + (x + 4)^2

4.表达式展开，分解和合并同类项

>> num
num =
(x + 3)^2
>> expand(num)
ans =
x^2 + 6*x + 9

>> s = x ^ 2 + 4 + 4 * x
s =
x^2 + 4*x + 4
>> factor(s)
ans =
(x + 2)^2

>> collect(num)
ans =
x^2 + 6*x + 9

5.化简函数

>> w = sym('x^3-1=(x-3)*(x+3)')
w =
x^3 - 1 == (x - 3)*(x + 3)
>> simplify(w)
ans =
x^3 + 8 == x^2

>> simple(w)

> In sym.simple at 41
simplify:
x^3 + 8 == x^2
x^3 - 1 == (x - 3)*(x + 3)
simplify(Steps = 100):
x in RootOf(z^3 - z^2 + 8, z)
combine(sincos):
x^3 - 1 == (x - 3)*(x + 3)
combine(sinhcosh):
x^3 - 1 == (x - 3)*(x + 3)
combine(ln):
x^3 - 1 == (x - 3)*(x + 3)
factor:
(x - 1)*(x^2 + x + 1) == (x - 3)*(x + 3)
expand:
x^3 - 1 == x^2 - 9
combine:
x^3 - 1 == (x - 3)*(x + 3)
rewrite(exp):
x^3 - 1 == (x - 3)*(x + 3)
rewrite(sincos):
x^3 - 1 == (x - 3)*(x + 3)
rewrite(sinhcosh):
x^3 - 1 == (x - 3)*(x + 3)
rewrite(tan):
x^3 - 1 == (x - 3)*(x + 3)
mwcos2sin:
x^3 - 1 == (x - 3)*(x + 3)
collect(x):
x^3 - 1 == x^2 - 9
ans =
x^3 + 8 == x^2

## 二.求解表达式和方程

1.求解表达式或表达式方程

>> E1 = x - 3
E1 =
x - 3
>> solve(E1)
ans =
3
>> solve('x^2-9')
ans =
3
-3
>>

>> solve('a*x^2+b*x+c')
ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)

MATLAB默认求解x，即当没有指定哪个变量已知，哪个变量是要求解的变量的情况下，默认将x设置为要求的变量。我们也可以显式的指定要求解哪个变量：

>> solve('a*x^2+b*x+c','a')
ans =
-(c + b*x)/x^2

>> solve('a*x^2+b*x+c','b')
ans =
-(a*x^2 + c)/x

2.方程的求解

>> E2 = sym('5*x^2+6*x+3=10')
E2 =
5*x^2 + 6*x + 3 == 10
>> solve(E2)
ans =
(2*11^(1/2))/5 - 3/5
- (2*11^(1/2))/5 - 3/5
>> double(ans)
ans =
0.7266
-1.9266
>>

>> E3 = sym('P = P0 * exp(r * t)')
E3 =
P == P0*exp(r*t)
>> solve(E3,'t')
ans =
log(P/P0)/r

3.方程组的求解

>> one = sym('3*x+2*y-z=10')
one =
3*x + 2*y - z == 10
>> two=sym('-x+3*y+2*z=5')
two =
3*y - x + 2*z == 5
>> three=sym('x-y-z=-1')
three =
x - y - z == -1
>>
x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]

x: [1x1 sym]
y: [1x1 sym]
z: [1x1 sym]
ans =
-2
ans =
5
ans =
-6

>> [x,y,z] = solve(one,two,three)
x =
-2
y =
5
z =
-6

4.替换

>> E4 = sym('a*x^2+b*x+c')
E4 =
a*x^2 + b*x + c
>> subs(E4,'x','y')
ans =
a*y^2 + b*y + c

>> subs(E4,'x',3)
ans =
9*a + 3*b + c

>> syms a b c x
>> subs(E4,{a,b,c,x},{1,2,3,4})
ans =
27

>> E6 = subs(E4,{a,b,c},{1,2,3})
E6 =
x^2 + 2*x + 3

>> numbers = 1:5
numbers =
1     2     3     4     5
>> subs(E6,x,numbers)
ans =
[ 6, 11, 18, 27, 38]

## 三.符号绘图

1.绘图函数ezplot

>> y = sym('x^2-2')
y =
x^2 - 2
>> ezplot(y)

>> ezplot(y,[-10,10]),title('二次函数'),xlabel('x'),ylebel('y')

2.ezplot绘制隐函数

>> ezplot('x^2+y^2-1',[-1.5,1.5])

>> ezplot('x^2+y^2=1',[-1.5,1.5])

x = sin(t)
y = cos(t)

>> ezplot('sin(t)','cos(t)')

>> y1 = sym('sin(x)');
>> y2 = sym('sin(2*x)');
>> y3 = sym('sin(3*x)');
>> ezplot(y1)
>> hold on
>> ezplot(y2)
>> ezplot(y3)
>> hold off

3.绘制三维图像

>> z1 = sym('3*(1-x)^2*exp(-(x^2)) - (y+1)^2')
z1 =
3*exp(-x^2)*(x - 1)^2 - (y + 1)^2
>> z2 = sym('- 10*(x/5 - x^3 - y^5) * exp(-x^2-y^2)')
z2 =
10*exp(- x^2 - y^2)*(x^3 - x/5 + y^5)
>> z3 = sym('- 1/3*exp(-(x+1)^2 - y^2)')
z3 =
-exp(- (x + 1)^2 - y^2)/3
>> z = z1 + z2 + z3;

>> subplot(2,2,1)
>> ezmesh(z)
>> title('ezmesh')

>> subplot(2,2,2)
>> ezmeshc(z)
>> title('ezmeshc')

>> subplot(2,2,3)
>> ezsurf(z)
>> title('ezsurf')

>> subplot(2,2,4)
>> ezsurfc(z)
>> title('ezsurfc')

4.绘制等高图，极坐标图

>> subplot(2,2,1)
>> ezcontour(z)
>> title('ezcontour')
>>
>> subplot(2,2,2)
>> ezcontourf(z)
>> title('ezcontourf')
>>
>> subplot(2,2,3)
>> z = sym('sin(x)');
>> ezpolar(z)
>> title('ezpolar')
>>
>> subplot(2,2,4)
>> ezplot(z)
>> title('ezplot')
>>

## 四.微积分运算

1.微分和导数

>> dist = sym('20+20*sin(pi * (t-10)/20)')
dist =
20*sin((pi*(t - 10))/20) + 20
>> ezplot(dist,[0:20])
>> title('车辆行驶距离与时间的函数')
>> xlabel('时间, s')
>> ylabel('距离, m')

MATLAB提供了求导的函数diff，可以求出符号表达式的导数，速度是位移的导数：

>> velocity = diff(dist)
velocity =
pi*cos((pi*(t - 10))/20)
>> ezplot(velocity,[0,20])
>> title('车辆速度与时间的函数')
>> xlabel('时间, s')
>> ylabel('速度，m/s')

>> acceleration = diff(velocity)
acceleration =
-(pi^2*sin((pi*(t - 10))/20))/20
>> ezplot(acceleration,[0,20])
>> title('加速度与时间的函数')
>> xlabel('时间，s')
>> ylabel('加速度，m/s²')

• 本文已收录于以下专栏：

举报原因： 您举报文章：MATLAB学习笔记（十） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)