DSolve: the right way of adding solutions

I'm trying to add the solution of two differential equations:

F[t] = (DSolve[.1*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t],
y[t], t] + DSolve[.2*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t], y[t], t])


But, this is clearly not the right way, this is what I get:

{{(y[t] ->
1. E^(-2. t) C[1] +
1. E^(-0.5 t)
C[2] + (5. + 1.66667 E^(-2. t) - 6.66667 E^(-0.5 t)) UnitStep[
t]) + (y[t] ->
1. E^(-4.56155 t) C[1] +
1. E^(-0.438447 t)
C[2] + (5. + 0.531695 E^(-4.56155 t) -
5.5317 E^(-0.438447 t)) UnitStep[t])}}


What is the correct way of doing this?

Thanks!

• If the code you show is how you would like to deal with DEs and their solutions, then I would suggest replacing DSolve with DSolveValue. (OTOH, the combined solution has four independent parameters, but the code produces only two, C[1] and C[2].) – Michael E2 Sep 22 '17 at 0:53
• Thanks! This is helpful. – Alt Sep 22 '17 at 2:16

One issue is that each equation has independent constants of integration (I suppose), C[1], C[2], etc. These parameters need to be regenerated and numbered independently. The function regenParam will do that.

ClearAll[regenParam];
(* regenerate parameters to be successive; default parameter is C[k] *)
regenParam[p_: C][s_List] := Module[{n = 0},
With[{params = DeleteDuplicates@Cases[#, _C, Infinity]},
n += Length@params;
# /. Thread[params -> Table[C[k], {k, n - Length@params + 1, n}]]
] & /@ s
];


We can then merge the solutions with Total:

Merge[Total]@ regenParam[]@
{DSolve[.1*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t], y[t], t],
DSolve[.2*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t], y[t], t]}
(*
<|y[t] ->
E^(-4.56155 t) C[1] + E^(-0.438447 t) C[2] + E^(-2. t) C[3] +
E^(-0.5 t) C[4] -
1.66667 E^(-2.5 t) (-1. E^(0.5 t) UnitStep[t] +
1. 2.71828^(2. t) E^(0.5 t) UnitStep[t] +
4. E^(2. t) UnitStep[t] -
4. 2.71828^(0.5 t) E^(2. t) UnitStep[t]) -
0.531695 E^(-5. t) (-1. E^(0.438447 t) UnitStep[t] +
1. 2.71828^(4.56155 t) E^(0.438447 t) UnitStep[t] +
10.4039 E^(4.56155 t) UnitStep[t] -
10.4039 2.71828^(0.438447 t) E^(4.56155 t) UnitStep[t])|>
*)


With a little work, one can extend this to Function solutions return by DSolve[system, y, t]. We have to write a function to total a list of functions.

functionTotal[s : {Verbatim[Function][{x_}, _] ..}] :=  (* Works on DSolve solutions *)
Replace[Total[s[[All, 2]]], e_ :> Function @@ {{x}, e}]

functionTotal[s : {Verbatim[Function][{x_}, _] ..}] :=  (* Holds the Function bodies *)
ReplacePart[
Replace[
Join @@ (Drop[#, 1] & /@ Hold @@@ s),
e_ :> Function @@ Hold[{x}, e]],
{2, 0} -> Plus];


It works in the same way with Merge as Total:

Merge[functionTotal]@ regenParam[]@
{DSolve[.1*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t], y, t],
DSolve[.2*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t], y, t]}
(*
<|y -> Function[{t}, (E^(-4.56155 t) C[1] + E^(-0.438447 t) C[2] -
0.531695 E^(-5. t) (-1. E^(0.438447 t) UnitStep[t] +
1. 2.71828^(4.56155 t) E^(0.438447 t) UnitStep[t] +
10.4039 E^(4.56155 t) UnitStep[t] -
10.4039 2.71828^(0.438447 t) E^(4.56155 t)
UnitStep[t])) + (E^(-2. t) C[3] + E^(-0.5 t) C[4] -
1.66667 E^(-2.5 t) (-1. E^(0.5 t) UnitStep[t] +
1. 2.71828^(2. t) E^(0.5 t) UnitStep[t] +
4. E^(2. t) UnitStep[t] -
4. 2.71828^(0.5 t) E^(2. t) UnitStep[t]))]|>
*)


I think the following solved my problem:

FullSimplify[(y[t] /.
First @ DSolve[.1*y''[t] + .5*y'[t] + .2*y[t] == UnitStep[t],
y[t], t]) + (g[t] /.
First @DSolve[.2*g''[t] + .5*g'[t] + .2*g[t] == UnitStep[t], g[t],
t])]


Ideas are welcome if there is a better solution, as I'm new to Mathematica.