collect、expand、horner、factor、simplify和pretty函数分别实现符号表达式的化简。 subexpr 和subs分别用来实现变量替换。
1.collect
syms x
coeffs = collect((exp(x) + x)*(x + 2)) %未指定符号变量
symvar((exp(x) + x)*(x + 2), 1) %查找表达式中的符号变量
%收集特定变量的幂系数
syms x y
coeffs_x = collect(x^2*y + y*x - x^2 - 2*x, x)
coeffs_y = collect(x^2*y + y*x - x^2 - 2*x, y)
syms a b
%指定对x、y收集
coeffs_xy = collect(a^2*x*y + a*b*x^2 + a*x*y +
x^2, [x y])
%根据i和pi收集系数
coeffs_i = collect(2*x*i - 3*i*y, i)
coeffs_pi = collect(x*pi*(pi - y) + x*(pi + i) + 3*pi*y,
pi)
%符号表达式和函数的系数集合
syms x y
fh = expand(sin(x + 3*y));
coeffs_cosy = collect(fh, cos(y))
coeffs_sinxsiny = collect(fh, [sin(x) sin(y)])
syms y(x)
fh2 = y^2*x + y*x^2 + y*sin(x) + x*y;
coeffs_y = collect(fh2, y)
%为矩阵的每个元素收集系数
syms x y
A = collect([(x + 1)*(y + 1), x^2 + x*(x -y); 2*x*y - x, x*y +
x/y], x)
%收集函数调用的系数
syms