# Assigning multiple solutions different replacement names

Let's say we are solving a homogeneous differential equation, such as $$y^{\prime\prime} + a y^{\prime} + b y = 0$$ with characteristic equation $$s^2 + a s+ b = 0.$$ Using the Solve function,

sols = Solve[s^2 + a*s + b == 0, s]


we get

{{s -> 1/2 (-a - Sqrt[a^2 - 4 b])}, {s -> 1/2 (-a + Sqrt[a^2 - 4 b])}}.


Now, the family of solutions is given by $$y(t) = C_1 e^{s_1 t} + C_2 e^{s_2 t}$$ where $s_1$ and $s_2$ are the roots of the characteristic equation.

How best to automatically substitute sols, which has two assignments for s into an expression like

C1*Exp[s1*t] + C2*Exp[s2*t]


or something more general (that adapts to the number of solutions)? All I can come up with are pretty inelegant hacks.

It would nice to give Solve an option to return something like

{{s[1] -> 1/2 (-a - Sqrt[a^2 - 4 b])}, {s[2] -> 1/2 (-a + Sqrt[a^2 - 4 b])}}


but I couldn't find an option like this in the documentation.

There are many possibilities. You could do something like

sols = Flatten@Module[{i=1}, Solve[s^2 + a*s + b == 0, s] /. s :> s[i++]];
(* {s[1] -> 1/2 (-a - Sqrt[a^2 - 4 b]), s[2] -> 1/2 (-a + Sqrt[a^2 - 4 b])} *)


Alternatively,

sols = Solve[s^2 + a*s + b == 0, s];
exprs = {c1 Exp[s t], c2 Exp[s t]};
Plus @@ MapThread[#1 /. #2 &, {exprs, sols}]
(* c1 E^(1/2 (-a - Sqrt[a^2 - 4 b]) t) + c2 E^(1/2 (-a + Sqrt[a^2 - 4 b]) t) *)

• +1 These are both nice! I'll wait a bit to see if anyone else wants to take a crack at it before I accept it as the answer. – Rico Picone Jan 26 '16 at 0:59
• I'm going to choose this as the correct answer because it technically addresses the issue of creating a list of replacement rules for different variables. However, I slightly prefer the approach by bbgodfrey. – Rico Picone Jan 26 '16 at 4:06

Another possibility is

sol = Solve[s^2 + a*s + b == 0, s]//Flatten;
Sum[C[i] Exp[sol[[i, 2]] t], {i, Length[sol]}]


But, why not just

DSolve[y''[t] + a y'[t] + b y[t] == 0, y[t], t][[1, 1, 2]]


both of which give

(* E^(1/2 (-a - Sqrt[a^2 - 4 b]) t) C[1] + E^(1/2 (-a + Sqrt[a^2 - 4 b]) t) C[2] *)


Total[MapIndexed[C[First@#2] Exp[Last@#1 t] &, Solve[s^2 + a*s + b == 0, s], {2}], 2]

• I like really like your solution, especially in its first form. It's intuitive for a student. Since I'm teaching, I want to show the steps in addition to the DSolve approach. Thanks! – Rico Picone Jan 26 '16 at 4:02