4
$\begingroup$

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.

$\endgroup$

2 Answers 2

3
$\begingroup$

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) *)
$\endgroup$
2
  • $\begingroup$ +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. $\endgroup$ Commented Jan 26, 2016 at 0:59
  • $\begingroup$ 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. $\endgroup$ Commented Jan 26, 2016 at 4:06
4
$\begingroup$

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

Addendum

A bit more compact is

Total[MapIndexed[C[First@#2] Exp[Last@#1 t] &, Solve[s^2 + a*s + b == 0, s], {2}], 2]
$\endgroup$
3
  • $\begingroup$ 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! $\endgroup$ Commented Jan 26, 2016 at 4:02
  • $\begingroup$ I have decided to choose the answer by march because it addresses the challenge of creating a replacement list as I requested. However, I prefer this approach by bbgodfrey. $\endgroup$ Commented Jan 26, 2016 at 4:07
  • $\begingroup$ You made a good choice. The first solution by @march is compact and creative. $\endgroup$
    – bbgodfrey
    Commented Jan 26, 2016 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.