# Solve the n ODE in the loop, add the solutions to create one function

I need to solve the n ODE and create a function which is a sum of all the solutions. I wanted to use the code below, but only part (solve) and (create solution) works well. How can I store the solutions from each ODE and add them together to obtain one function ? Thank you for any help

sxf={}; (* list for solutions of each ODE *)

For [i = 1; i < 10, i++;
xusol = DSolve[{dx[[i]] == a[[i]], x0ui[[i]] == x0u[[i]]}, x[[i]], t] (*solve*)
sx[t_]:= Evaluate[x[[i]] /. xusol]; (* create solution ??? *)
AppendTo[xlist,sx]; (* store *)]

sxf = Function[t, Sum[sx,{i, 1, Ns}]]; (* sum all solutions ??? *)

Plot[sxf[t], {t, 0, tmax}] (* plot x[t] *)

• Begin by replacing your For loop with a Table command. – Searke Mar 26 '16 at 15:43
• xusol =Table[ DSolve[{dx[[i]] == a[[i]], x0ui[[i]] == x0u[[i]]}, x[[i]], t] , {i,1,10}] – Searke Mar 26 '16 at 16:02
• Without more information, it is impossible to help you. What is dx? a? x0u? x0ui? x? (The notation is kind of strange.) Furthermore, is your question about how to take $n$ pure functions and make out of them the pure function which is a sum? Do the different solutions depend on each other or can you solve them all at once? If so, there is a very nice thing that can be done with DSolveValue: it can return the sum of the functions with one call to the function, but I cannot show you this without more knowledge about your problem. – march Mar 27 '16 at 4:10

This is more of a comment than an answer until such time as you post more information. (I could not fit all of this into a comment.)

Let's take a sample set of equations:

eqns = y[#]'[t] == # - y[#][t] & /@ Range[0, 3]
inits = y[#][0] == 4 & /@ Range[0, 3]
(* {y[0]'[t] == -y[0][t], y[1]'[t] == -y[1][t], y[2]'[t] == -y[2][t], y[3]'[t] == -y[3][t]}
(* {y[0][0] == 4, y[1][0] == 4, y[2][0] == 4, y[3][0] == 4} *)


Then, let's use DSolveValue to return the functions all at once:

sols = DSolveValue[Join[eqns, inits], Array[y, 4, 0], t]
(* {Function[{t}, 4 E^-t], Function[{t}, E^-t (3 + E^t)], Function[{t}, 2 E^-t (1 + E^t)], Function[{t}, E^-t (1 + 3 E^t)]} *)


Then, plot:

Plot[Evaluate@Through[sols[t]], {t, 0, 5}]


Alternatively, plot the sum of these functions:

Plot[Plus @@ Through[sols[t]], {t, 0, 5}, PlotRange -> {0, 16}]


But here's the thing: you can use DSolveValue to return the sum to begin with!

sol = DSolveValue[Join[eqns, inits], Plus @@ Array[y[#][t] &, 4, 0], t]
(* 4 E^-t + 2 E^-t (1 + E^t) + E^-t (3 + E^t) + E^-t (1 + 3 E^t) *)


and

Plot[sol, {t, 0, 5}, PlotRange -> {0, 16}]