# Simplify complex answer given by DSolve[]

I tried the technique in this question, but that did not work for the solution given by the MWE:

DSolve[{f'[t] + f[t]*g[t] == h[t], f[0] == f0}, {f[t]}, t]


How to teach Mathematica to simplify the solution to $\int_0^t h(s) \exp \left(-\int_s^t g(\tau ) \, d\tau \right) \, ds+f0\exp \left(-\int_0^t g(s) \, ds\right)$

Clear[xf];
xf[e_] :=
e /. {Integrate[int_, {x_, a_, b_}] - Integrate[int_, {x_, a_, c_}] :>
Integrate[int, {x, c, b}],
coeff_ Integrate[int_, {x_, a_, b_}] :>
Integrate[coeff int, {x, a, b}]};

sol = DSolve[{f'[t] + f[t]*g[t] == h[t], f[0] == f0}, {f[t]}, t];
Simplify[sol,
TransformationFunctions -> {xf, Expand, Collect[#, Power[E, _]] &, Automatic}]
(*
{{f[t] -> E^Integrate[-g[K[1]], {K[1], 0, t}] * f0 +
Integrate[E^Integrate[-g[K[1]], {K[1], K[2], t}]*h[K[2]], {K[2], 0, t}]}}
*)


To get the answer in the form requested (it's not "simpler" according to the default ComplexityFunction of Simplify:

% /. {Integrate[int_, {x_, t, b_}] :> -Integrate[int, {x, b, t}]} /.
{K[1] -> s, K[2] -> τ}

(*
{{f[t] -> E^Integrate[-g[s], {s, 0, t}] * f0 +
Integrate[E^Integrate[-g[s], {s, τ, t}] * h[τ], {τ, 0, t}]}}
*)

• Using the code in your first block does not give a very good result because strictly speaking E^Integrate[g[K[1]], {K[1], t, K[2]}] includes the integrate index K[2] and should be included in the last Integrate[h[K[2]], {K[2], 0, t}]. Following your idea, the second replace rule in xf function should be coeff_*Integrate[int_,{x_, a_, b_}] :> Integrate[coeff*int,{x, a, b}] Commented Mar 10, 2015 at 20:07
• @egwenesedai Of course -- and thanks! (Fixed now, I hope.) Commented Mar 10, 2015 at 22:27