# Finding the coefficients of an expression

I have a set of formulas of the following form:

{5.1011 + E^(-6876.32 t) (-5.1011 + 1. x),
5.28335 + E^(-4868.36 t) (-5.28335 + 1. x),
6.45616 + E^(-1847.14 t) (-6.45616 + 1. x),
144.165 + E^(-34.6815 t) (-144.165 + 1. x),
320. + E^(-15.6055 t) (-320. + 1. x)}


I would like to get the coefficients {a, b, c} where each of the formulas is expressed as

a + e^(b*t)*(c + 1. vStart)


I tried using rules, "Normal", and Solve to see if I could figure this out. I will note that if I take any of these expressions and for example do this:

5.1011 + E^(-6876.32 t) (-5.1011 + 1. x) //TreeForm


I get a picture like this:

so Mathematica clearly understands the pieces. I just cannot figure out how to turn this into the list shown with TreeForm so I can grab the pieces. As an example, in this tree (simplifying each coefficient), I'd get a=5.1, b=-6876.3, c=-5.1.

I suspect this is something simple but am at a loss. Any help appreciated.

expr = {
5.1011 + E^(-6876.32 t) (-5.1011 + 1. x),
5.28335 + E^(-4868.36 t) (-5.28335 + 1. x),
6.45616 + E^(-1847.14 t) (-6.45616 + 1. x),
144.165 + E^(-34.6815 t) (-144.165 + 1. x),
320. + E^(-15.6055 t) (-320. + 1. x)};

((expr // Rationalize) /. r_Rational :> N[r]) /.
a_ + E^(b_*t) (c_ + x) :> {a, b, c}

(* {{5.1011, -6876.32, -5.1011}, {5.28335, -4868.36, -5.28335}, \
{6.45616, -1847.14, -6.45616}, {144.165, -34.6815, -144.165}, {320, \
-15.6055, -320}} *)

• Brilliant! This is what I was hoping to do and didn't know how. Thank you! Apr 5, 2021 at 7:17
• May I know why the first part of this is needed? I know that it is since after seeing your answer I tried without that and it didn't work. I just don't understand the purpose of the first set of expressions before getting to the last rule replacement. Apr 5, 2021 at 7:25
• Is Rationalize needed because of an issue with pattern matching and floating point numbers? Apr 5, 2021 at 8:18
• I used Rationalize to eliminate the 1. coefficient in 1. x. Alternatively, you could include that in the pattern, i.e., expr /. a_ + E^(b_*t) (c_ + 1. x) :> {a, b, c} I prefer using Rationalize since that approach will work whether or not the 1.  is present. Apr 5, 2021 at 14:14
• Thanks for the explanation and solution! Apr 5, 2021 at 17:03

I think I may have figured this out. Looking up TreeForm, I found FullForm.

anExpression =
5.101102275075902 +
E^(-6876.316769642943 t) (-5.101102275075902 + 1. vStart);
depth = Depth[FullForm[anExpression]];
expressionAsList = Level[FullForm[anExpression], depth];
expressionAsList[[{1, 3, 7}]]
(*{5.1011, E, -6876.32, t, -6876.32 t, E^(-6876.32 t), -5.1011, 1., \
vStart, 1. vStart, -5.1011 + 1. vStart,
E^(-6876.32 t) (-5.1011 + 1. vStart),
5.1011 + E^(-6876.32 t) (-5.1011 + 1. vStart)}
{5.1011, -6876.32, -5.1011}
*)


From this I can grab what I want from positions 1,3,7.

Update: looks like FullForm is unnecessary. Instead

Level[anExpression,Infinity]
`

gives the same answers without the intermediate step. Also, for these equations, it turns out that a==-c so I only need to extract two values (but this is a peculiarity of the types of expressions - for the general solution I'd want to pick the values).