【MATLAB】符号数学计算(四):符号表达式操作

一、符号表达式合并

  • R=collect(S):将表达式S中相同次幂的项合并。S可以是一个表达式,也可以是一个符号矩阵。
  • R=collect(S,v):将表达式中S中v的相同次幂进行合并。如果v没有指定,则默认将含有x的相同次幂的项进行合并。
>> syms x y t;
>> f=sym('x*cos(t)+y*sin(t)+(x^2+2*x*y+3*y^2)*t')
 
f =
 
t*(x^2 + 2*x*y + 3*y^2) + x*cos(t) + y*sin(t)
 
>> collect(f,x)
 
ans =
 
t*x^2 + (cos(t) + 2*t*y)*x + 3*t*y^2 + sin(t)*y
 
>> collect(f,y)
 
ans =
 
(3*t)*y^2 + (sin(t) + 2*t*x)*y + t*x^2 + cos(t)*x

二、符号表达式展开

  • R=expand(S):将表达式S的各项进行展开。
>> syms x y;
>> f1=sym('(x-1)^2*(y-1)')
 
f1 =
 
(x - 1)^2*(y - 1)
 
>> expand(f1)
 
ans =
 
2*x + y - 2*x*y + x^2*y - x^2 - 1

>> f2=sym('exp((x+y)^2)')
 
f2 =
 
exp((x + y)^2)
 
>> expand(f2) 
 
ans =
 
exp(x^2)*exp(y^2)*exp(2*x*y)

三、符号表达式的嵌套

  • R=horner(S):其中S是符号多项式矩阵,函数horner将其中的每个多项式转换成它们的嵌套形式。
>> syms x y;
>> f1=sym('x^3-6*x^2+11*x-6');
>> f1
 
f1 =
 
x^3 - 6*x^2 + 11*x - 6
 
>> horner(f1)
 
ans =
 
x*(x*(x - 6) + 11) - 6
 
>> f2=sym('[x^2+x;y^3-2*y]')
 
f2 =
 
   x^2 + x
 y^3 - 2*y
 
>> horner(f2)
 
ans =
 
   x*(x + 1)
 y*(y^2 - 2)

 四、符号表达式的分解

  • factor(X):多项式或者多项式矩阵均可,系数是有理数,那么该函数将把X表示成系数为有理数的低价多项式相乘的形式;如果X不能分解成有理多项式乘积的形式,则返回X本身。
>> syms x y;
>> f1=sym('2*x^2-7*x*y-22*y^2-5*x+35*y-3')
 
f1 =
 
2*x^2 - 7*x*y - 5*x - 22*y^2 + 35*y - 3
 
>> factor(f1)
 
ans =
 
(2*x - 11*y + 1)*(x + 2*y - 3)
 
>> f2=sym('[2*x^2-5*x*y-3*y^2;x^3-y^3]')
 
f2 =
 
 2*x^2 - 5*x*y - 3*y^2
             x^3 - y^3
 
>> factor(f2)
 
ans =
 
       (x - 3*y)*(2*x + y)
 (x - y)*(x^2 + x*y + y^2)
 
>> f3=sym('12345678901234567890')
 
f3 =
 
12345678901234567890
 
>> factor(f3)
%factor对常数进行分解时,如果某个元素超过了16位,则必须先将其用函数sym定义成符号矩阵才能进行分解
 
ans =
 
2*3^2*5*101*3541*3607*3803*27961

 五、符号表达式的简化

  • R=simplify(S)
  • R=simple(S)
  • [R,how]=simple(S):不显示简化的中间结果,只显示寻找到最短形式及找到该形式的简化方法。
>> sym x;
>> f1=sym('(x^3-1)/(x-1)');
>> simplify(f1)
 
ans =
 
x^2 + x + 1
 
>> f2=sym('cos(x)+i*sin(x)')
 
f2 =
 
cos(x) + sin(x)*i
 
>> simple(f2)
警告: simple will be removed in a future release. Use simplify instead. 
> In sym.simple at 41 
 
simplify:
 
cos(x) + sin(x)*i
 
 
radsimp:
 
cos(x) + sin(x)*i
 
 
simplify(Steps = 100):
 
cos(x) + sin(x)*i
 
 
combine(sincos):
 
cos(x) + sin(x)*i
 
 
combine(sinhcosh):
 
cos(x) + sin(x)*i
 
 
combine(ln):
 
cos(x) + sin(x)*i
 
 
factor:
 
cos(x) + sin(x)*i
 
 
expand:
 
cos(x) + sin(x)*i
 
 
combine:
 
cos(x) + sin(x)*i
 
 
rewrite(exp):
 
exp(x*i)
 
 
rewrite(sincos):
 
cos(x) + sin(x)*i
 
 
rewrite(sinhcosh):
 
cosh(x*i) + sinh(x*i)
 
 
rewrite(tan):
 
(tan(x/2)*2*i)/(tan(x/2)^2 + 1) - (tan(x/2)^2 - 1)/(tan(x/2)^2 + 1)
 
 
mwcos2sin:
 
sin(x)*i - 2*sin(x/2)^2 + 1
 
 
collect(x):
 
cos(x) + sin(x)*i
 
 
ans =
 
exp(x*i)
 
>> [R,how]=simple(f2)
警告: simple will be removed in a future release. Use simplify instead. 
> In sym.simple at 41 
 
R =
 
exp(x*i)
 

how =

rewrite(exp)

猜你喜欢

转载自blog.csdn.net/csdn___csdn/article/details/81157660