# Adding algebraic functions to NDSolve

I want to define an integral function which depends on a system of ODE and is also used to NDSolve the ODEs.

Tc = Sum[c[i]];

D[c[i], t] == r[i] c[i] (1 - Tc/k) - d[i] c[i]


where i = 1,...,n

Basically, I want to evaluate Tc at a given t value, before solving the differential equation for c[i] so that the code uses this value for NDSolve.

r, k and d are known parameters. c[i] needs to be calculated and thus Tc which is a summation of all c[i]s, at a give time t, is also unknown.

Thanks in advance :)

• Your question is not clear enough for me. Could you try to define your goal more carefully and which elements are known / unknown? – Dr. belisarius Oct 27 '15 at 14:10
• r, k and d are known parameters. c[i] needs to be calculated and thus Tc which is a summation of all c[i]s, at a give time t, is also unknown. – VitalSigns Oct 27 '15 at 16:47
• Do only c[i] == c[i][t] and Tc == Tc[t] depend on on t? Looks a bit like the replicator equation. Just enter the equations as you normally would for NDSolve -- doesn't that work? Or define Tc[t_] = Sum[c[i][t], {i, n}] outside NDSolve and use Tc[t] inside NDSolve. – Michael E2 Oct 27 '15 at 16:57
• Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Oct 29 '15 at 15:15
• @MichaelE2 : Your solution worked too. Thanks! I defined Tc[t] == Sum of all c[i] within the NDSolve bracket and it worked. If i define it outside the bracket as Tc= Tc[t] == Sum of all C[i] and then use Tc in NDSolve bracket it doesn't work. weird ... I cant understand why? – VitalSigns Nov 3 '15 at 13:49

Perhaps

n = 5;
cc = Array[c[#] &, n];
Tc = Sum[c[i][t], {i, n}];
r = RandomInteger[{1, 10}, n];
d = RandomInteger[{1, 10}, n];
k = RandomInteger[{1, 100}];
initcon = Table[c[i][0] == RandomInteger[{0, 10}], {i, n}]

sol = NDSolveValue[
Join[Table[D[c[i][t], t] == r[[i]] c[i][t] (1 - Tc/k) - d[[i]] c[i][t], {i, n}],
initcon],
cc, {t, 0, 10}]

Plot[Through[sol[t]], {t, 0, 10}, Evaluated -> True]