ReplaceAll a list of rules to an equation

Given the following equation and rule, how would I Solve the equation for tr, k and c?

eq = T == tr – E^(-k * t)*c;
rule = {t -> {0, 5, 10}, T -> {2000, 1300, 1100}};

eq /. rule results in this, which is almost what I want:

{2000, 1300, 1100} == {tr – c, tr – c/E^(5k), tr – c/E^(10k)}

I would like to get a list of equations instead like so:

{2000 == tr – c, 1300 == tr – c/E^(5k), 1100 == tr – c/E^(10k)}

which could then be used to solve the equations (using And on the list).

=================

=================

2 Answers
2

=================

Pinguin Dirk’s solution is very neat for this problem. It is possible because, as you discovered, eq /. rule generates

{2000, 1300, 1100} == {tr – c, tr – c/E^(5*k), tr – c/E^(10*k)}

But this is often not the case! It works only because Power (^) has the attribute Listable. Let’s say instead of Power[E,t k] we had f[E,t k] where f is any function that is not Listable. eq /. rule yields:

{2000, 1300, 1100} == tr – c f[E, {0, -5 k, -10 k}]

If you run into this problem, a more general approach would be needed. The key to the general approach is to get your rules into this format:

rules = Thread[Thread /@ {t -> {0, 5, 10}, T -> {2000, 1300, 1100}}]

{{t -> 0, T -> 2000}, {t -> 5, T -> 1300}, {t -> 10, T -> 1100}}

Because in this format we can easily generate the required results, like this:

eq /. rules

{2000 == tr – c f[E, 0], 1300 == tr – c f[E, -5 k], 1100 == tr – c
f[E, -10 k]}

  

 

The # & /@ part is redundant as a list mapped to the identity function will return the same list. eq /. rules is enough.
– Tyilo
Oct 20 ’13 at 20:57

  

 

@Tyilo Very observant of you, I updated the answer.
– C. E.
Oct 20 ’13 at 21:01

  

 

@Tyilo Although I would maintain it’s not because of the reason you mention. {expr /. {a->b}, expr /. {b->c}} would not be the same as expr /. {{a->b},{b->c}} unless ReplaceAll had the seemingly undocumented property of threading over it’s second argument.
– C. E.
Oct 20 ’13 at 21:07

  

 

oh I didn’t know that what you wrote before is equal to (eq /. #)& /@ rules). I thought it was eq /. (# & /@ rules) or something else, sorry.
– Tyilo
Oct 20 ’13 at 21:25

Use for example:

Thread[eq/. rule]

and get:

{2000 == -c + tr, 1300 == -c E^(-5 k) + tr, 1100 == -c E^(-10 k) + tr}

  

 

@Tyilo: Thanks for the accept, but usually it pays to wait a bit… there are most certainly other people out there with other ideas, better ideas. And you don’t wanna scare them away, do you? Still, I am glad you like my approach!
– Pinguin Dirk
Oct 20 ’13 at 13:49

  

 

Fair enough, I’ll wait a little longer
– Tyilo
Oct 20 ’13 at 14:09