Search code examples
matlabsymbolic-mathexp

Factor symbolic expression involving exp()


I have a symbolic function exp(a+b), and would like to factor out A=exp(a) to produce exp(a+b) = A*exp(b), but I cannot figure out how to do this in MATLAB. Below is my attempt:

syms a b A
X = exp(a+b);
Y = subs(X,exp(a),A) % = A*exp(b)

however, Y = exp(a+b). For some reason, MATLAB cannot determine: exp(a+b) = exp(a) * exp(b) = A*exp(b).

Any help is greatly appreciated.


Solution

  • First, expand the expression so that the exponents are separated then do the substitution. By default, when writing out an expression for the first time (before running it through any functions), MATLAB will try and simplify your expression and so exp(a)*exp(b) can be much better expressed using exp(a+b). This is why your substitution had no effect. However, if you explicitly want to replace a part of the expression that is encompassed by an exponent with a base, expand the function first, then do your substitution:

    >> syms a b A;
    >> X = exp(a+b);
    >> Xexpand = expand(X)
    
    Xexpand =
    
    exp(a)*exp(b)
    
    >> Y = subs(Xexpand, exp(a), A)
    
    Y =
    
    A*exp(b)