matlab符号计算方法02

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 a b c d e f x
F = a*sin(2*x) + b*sin(2*x) + c*cos(x) + d*cos(x) + e*sin(3*x) 
+f*sin(3*x);
collect(F, 'sin')

collect(F, {'sin' 'cos'})

2. expand

syms x
sf = (sin(3*x) – 1)^2;
fep = expand(sf)

%通过将“ArithmeticOnly”设置为true,抑制函数(如sin(3*x))的扩展。
fep2 = expand(sf, 'ArithmeticOnly', true)

syms a b c
fl = log((a*b/c)^2);
flg = expand(fl)

%通过将“IgnoreAnalyticConstraints”设置为true,应用
标识来简化对数的输入。
flg2 = expand(fl,'IgnoreAnalyticConstraints',true)

3. horner


syms x y;
fun = expand((x-2)^3)

funh = horner(fun)

fun1 = x^3+3*x+1;
fun2 = 3*y^2+4*y+7;
horfun = horner([fun1,fun2])

fx = (x^2+x+1)*(x^3+1);
fxh = horner(fx)

4. factor

F = factor(823429252)

%大数字用单引号括起来转化为符号数值
F = factor(sym('82342925225632328'))

F = factor(sym(-92465)) %负数

F = factor(sym(112/81)) %分数形式

sym x;
fun = 4*x^3+x^4+8*x+5*x^2+6;
hfun = factor(fun)

syms a b c d
y = -a*b^5*c*d*(a^2 - 1)*(a*d - b*c);
F = factor(y,[b c])

5. simplify

syms x c a b;
s1 = simplify(sin(x)^2 + cos(x)^2)

s2 = simplify(exp(c*log(sqrt(a+b))))

s3 = simplify([(x^2+5*x+6)/(x+2),sqrt(16)])

6.subexpr

syms x a
sol=solve(x^3+a*x+1,x,'MaxDegree',3)
r = subexpr(sol)

pretty(r) %pretty 符号表达式

syms a b c x
solutions = solve(a*x^2 + b*x + c == 0, x)

syms s
[abbrSolutions,s] = subexpr(solutions,s)

7.subs

%替换
sola = subs(sol,a,1)
solv = vpa(sola,15)

%指定替换为数值矩阵
syms a t
SA = subs(exp(a*t) + 1, a, -magic(3))

syms x y a b
fh = a*b*x*y;
%指定替换为函数
fh1 = subs(subs(fh,x,sin(x)),y,exp(y))

%嵌套替换为对应数值
fh2 = subs(subs(fh1,a,5),b,3)

%绘制二维曲面图
ezmesh(fh2)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值