TransWikia.com

Finding the coefficients of an expression

Mathematica Asked by Mark R on April 10, 2021

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:
TreeForm of equation

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.

2 Answers

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}} *)

Correct answer by Bob Hanlon on April 10, 2021

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).

Answered by Mark R on April 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP