# Replacing exponential pattern in equation

I have an equation of the form

f=a0+a1*Exp[-b1*x]+a2*Exp[-b2*x]+a3*Exp[-b3*x]+...+an*Exp[-bn*x]


with $n$ exponential terms. $a_i,b_i$ are real numbers (a result of some previous computation). My intention is to apply integral operator $\int_p^qdx$ to the function above. When number of terms in above equation becomes very large (in fact on my PC about greater than 30), the evaluation becomes very slow. So I thought why not bypass the integration step and use replacement rules because I know what the integral is going to look like. That is I want to do the following replacement, which I hope will speed up evaluation:

Exp[-b*x] -> (Exp[-b*q]-Exp[-b*p])/b
a0 -> a0(q-p)


I want to repeat that $a_i,b_i$, are real numbers which are output of some previous computation, and not just symbols, even though for generality I had to represent them as symbols here rather than as numbers.

I wish I could tell you what I tried, but honestly I have no clue how to proceed with this problem. How do I match patterns and do replacement in this case? Thanks in advance for any help.

P.S. I saw this post: Replace pattern for exponentials but wasn't helpful to me.

• You say the $a_i$ are real numbers. How do these numbers relate to the $a$ in your rule a -> p - q? Nov 23, 2016 at 8:22
• Why do you want a->p-q? Integrate[a Exp[-b x], {x, p, q}] yields $\frac{a \left(e^{-b p}-e^{-b q}\right)}{b}$. Nov 23, 2016 at 8:28
• @m_goldberg, corey979 Sorry $a$ should be replaced by the quantity you have shown.
– Deep
Nov 23, 2016 at 9:27
• @corey979, m_goldberg The operator is $\int_p^q dx$, so the replacement for constant term should be $a0\to a0(q-p)$. Edited.
– Deep
Nov 23, 2016 at 9:38

f = 3. + 2.6 Exp[-7.1 x] - 2.2 Exp[2 x]

rule = {Exp[b_*x] -> (Exp[b*q] - Exp[b*p])/b}

f /. rule /. {First[f] -> First[f] (p - q)}


The First[f] -> First[f] (p - q) is not very robust, though; works for this particular type of f.

• +1 Beautiful. I guess I can always use ReplacePart for dealing with the constant term.
– Deep
Nov 23, 2016 at 11:08

Working with version 10.1 this seems to works perfectly

f = a0 + a1*Exp[-Subscript[b, 1]*x] + a2*Exp[-Subscript[b, 2]*x] +
a3*Exp[-Subscript[b, 3]*x]
Exp[-b*x] -> (Exp[-b*q] - Exp[-b*p])/b

• I would add one element to the rule and apply it:  rule = {Exp[-b_x] -> (Exp[-bq] - Exp[-b*p])/b, a0 -> a0 (p - q)}; f /. rule Nov 23, 2016 at 7:50
• That's not an answer to this question, it doesn'r work on f = 3. + 2.6 Exp[-3.7 x]. The OP emphasized that a, b are some known particular numbers. @AlexeiBoulbitch's comment works, but partially, as it does not alter the as. Nov 23, 2016 at 7:54
• @corey979 Right. This rule is better: rule = {Exp[b_*x] -> (Exp[b*q] - Exp[b*p])/b, a0 -> a0 (p - q)};. Nov 23, 2016 at 8:17
• I'm not fully awaken yet, but I think the OP wants sth like rule = {a_ Exp[b_*x] -> (p - q) (Exp[b*q] - Exp[b*p])/b}. Nov 23, 2016 at 8:24
• @corey979 You are right. Is {a_Exp[b_*x]} a valid pattern?
– Deep
Nov 23, 2016 at 9:31