2
$\begingroup$
ClearAll[sus];
D[sus[i, t], t] /. i -> 1

generates

   (0,1)
sus     [1, t]

And

ClearAll[sus];
sus = {sus1[t], sus2[t], sus3[t]};
D[sus, t][[1]]

generates

  sus1'[t]

Apparently, these two derivatives perform different operations because I applied these derivative operations in a simple model, and obtain very different outcomes.

Can someone explain to me what exactly these two derivate operations do?

EDIT

Model 1

ClearAll["Global`*"];

Manipulate[
ip = {ip1, ip2, ip3};
rr = {rr1, rr2, rr3};

sus0 = {sus01, sus02, sus03};
inf0 = {inf01, inf02, inf03};
rec0 = {rec01, rec02, rec03};

sus = {sus1[t], sus2[t], sus3[t]};
inf = {inf1[t], inf2[t], inf3[t]};
rec = {rec1[t], rec2[t], rec3[t]};

sol = NDSolve[
Union[
 Table[D[sus, t][[i]] == -ip[[i]]*sus[[i]]*inf[[i]], {i, 3}],
 Table[
  D[inf, t][[i]] == 
   ip[[i]]*sus[[i]]*inf[[i]] - rr[[i]]*inf[[i]], {i, 3}],Table[D[rec, t][[i]] == rr[[i]]* inf[[i]], {i, 3}], Table[(sus[[i]] /. t -> 0) == sus0[[i]], {i, 3}], Table[(inf[[i]] /. t -> 0) == inf0[[i]], {i, 3}], Table[(rec[[i]] /. t -> 0) == rec0[[i]], {i, 3}]
],
vars = 
 Flatten@Table[#[[0]] & /@ variable, {variable, {sus, inf, rec}}], {t, 0, days}
] // First;

Row[
 Table[
  Plot[Evaluate[Take[#[t] & /@ vars, {i, 9, 3}] /. sol], {t, 0, days},  PlotRange -> All,
Frame -> True,
FrameLabel -> {"time (days)", "proportion of percentage"}, PlotLabel ->Style["Continous-time Sectoral SIR Model", Bold, Medium], PlotLegends -> Placed[Take[vars, {i, 9, 3}], Below], PlotStyle -> Thick, ImageSize -> 250], {i, 3}]
],

Style["disease information", Bold], 
{{ip1, .5, "Infection rate (sector 1): "}, .0,.9},
{{rr1, .2, "Recovery rate (sector 1): "}, .0, .9},  
{{ip2, .5, "Infection rate (sector 2): "}, .0, .9}, 
{{rr2, .2, "Recovery rate (sector 2): "}, .0, .9},
{{ip3, .5, "Infection rate (sector 3): "}, .0, .9},
{{rr3, .2, "Recovery rate (sector 3): "}, .0, .9},
Delimiter,
Style["population information", Bold], 
{{sus01, .999, "initially susceptible (%) (sector 1): "}, 0, 1},
{{inf01, .001, "initially infected (%) (sector 1): "}, 0, 1 - sus01},
{{rec01, .0, "initially recovered (%) (sector 1): "}, 0, 1 - sus01 - inf01},
{{sus02, .999, "initially susceptible (%) (sector 2): "}, 0, 1},
{{inf02, .001, "initially infected (%) (sector 2): "}, 0, 1 - sus02},
{{rec02, .0, "initially recovered (%) (sector 2): "}, 0, 1 - sus02 - inf02},
{{sus03, .999, "initially susceptible (%) (sector 3): "}, 0, 1}, 
{{inf03, .001, "initially infected (%) (sector 3): "}, 0, 1 - sus03},
{{rec03, .0, "initially recovered (%) (sector 3): "}, 0,  1 - sus03 - inf03}, 
Delimiter,
Style["time scale", Bold],
{{days, 100, "Duration of the pandemic (days)"}, 1, 365, 5}
]

Model 2

ClearAll[initials, eqns, vars, sol, sus, inf, rec, sus0, inf0, rec0];
initials = 
Table[Thread[{sus0[i], inf0[i], rec0[i]} = {.999, .001, 0}], {i, 3}];
eqns = Flatten[
Table[{
 D[sus[i, t], t] == -0.5*sus[i, t]*inf[i, t],
 D[inf[i, t], t] == 0.5*sus[i, t]*inf[i, t] - 0.2*inf[i, t],
 D[rec[i, t], t] == 0.2*inf[i, t],
 (sus[i, t] /. t -> 0) == sus0[i],
 (inf[i, t] /. t -> 0) == inf0[i],
 (rec[i, t] /. t -> 0) == rec0[i]
 }, {i, 3}]
 ]; 
vars = Flatten@Table[{sus[i, t], inf[i, t], rec[i, t]}, {i, 3}];
sol = NDSolve[eqns, vars, {t, 0, 20}] // First;

Legended[ Grid[
Partition[Plot[{##}, {t, 0, 365},
  PlotStyle -> {Blue, Red, Green},
  PlotRange -> All,
  Frame -> True,
  FrameLabel -> {"time (days)", "proportion of percentage"},
  PlotLabel -> 
   Style["Continous-time Sectoral SIR Model", Bold, Medium],
  PlotStyle -> Thick,
  ImageSize -> 250
  ] & /@ {
 Evaluate[Take[vars, {1, 3}] /. sol],
 Evaluate[Take[vars, {4, 6}] /. sol],
 Evaluate[Take[vars, {7, 9}] /. sol]
 }, 3]
 ],
 LineLegend[{Blue, Red, Green}, {"sus[t]", "inf[t]", "rec[t]"}]
 ]
$\endgroup$
4
  • 1
    $\begingroup$ Well the first one is a partial derivative of a function of two variables, the second a derivative of a function of one variable. Why should they be the same? $\endgroup$ – MarcoB Dec 5 '20 at 18:58
  • $\begingroup$ @MarcoB: One of the two variables is nothing more than an index (subscript). The actual function is a function of t' in both cases. To me, there should be any difference between Model 1 and Model 2 outcomes because the first argument is not evaluated in the system at all. $\endgroup$ – Tugrul Temel Dec 5 '20 at 21:11
  • 1
    $\begingroup$ I understand that, but Mathematica does not know your intentions :-) It just sees a function of two variables in the first case and of one variable in the second. $\endgroup$ – MarcoB Dec 5 '20 at 22:46
  • $\begingroup$ @MarcoB: You are absolutely right. My question was badly formulated on my side in that I did not mean that the two functions are the same but meant that the outcomes from those derivatives should be the same. In the two models above, the outcomes are the same, which is what I was aiming to obtain. Thank you for your reaction. $\endgroup$ – Tugrul Temel Dec 5 '20 at 22:59
4
$\begingroup$
ClearAll[sus];

D[sus[i, t], t] /. i -> 1 // InputForm

(* Derivative[0, 1][sus][1, t] *)

This is the first derivative with respect to the second argument, and with the first argument equal to 1. This is equivalent to

D[sus[1, t], t] // InputForm

(* Derivative[0, 1][sus][1, t] *)

ClearAll[sus];

sus = {sus1[t], sus2[t], sus3[t]};

The derivative of a list is the list of the derivatives

D[sus, t]

(* {Derivative[1][sus1][t], Derivative[1][sus2][t], Derivative[1][sus3][t]} *)

Taking the first Part does just that

D[sus, t][[1]]

(* Derivative[1][sus1][t] *)

Look at the output of

{D[f[x, y], x], D[f[x, y], y], D[f[x, y], x, y], D[f[x, y], y, x], 
 D[f[x, y], {{x, y}}], D[f[x, y], {{y, x}}]}

EDIT: Your equations are unnecessarily complicated and you appear to be using Union where you should be using Join. For example,

Union[
  Table[D[sus, t][[i]] == -ip[[i]]*sus[[i]]*inf[[i]], {i, 3}],
  Table[D[inf, t][[i]] == ip[[i]]*sus[[i]]*inf[[i]] - rr[[i]]*inf[[i]], 
    {i, 3}],
  Table[D[rec, t][[i]] == rr[[i]]*inf[[i]], {i, 3}],
  Table[(sus[[i]] /. t -> 0) == sus0[[i]], {i, 3}],
  Table[(inf[[i]] /. t -> 0) == inf0[[i]], {i, 3}],
  Table[(rec[[i]] /. t -> 0) == rec0[[i]], {i, 3}]];

can be written more simply (and more clearly) as

Join @@ (Thread /@ {
     D[sus, t] == -ip*sus*inf,
     D[inf, t] == ip*sus*inf - rr*inf,
     D[rec, t] == rr*inf,
     (sus /. t -> 0) == sus0,
     (inf /. t -> 0) == inf0,
     (rec /. t -> 0) == rec0});
$\endgroup$
3
  • $\begingroup$ I edited the question with two identical models and they should generate the same outcomes but as you will see, they do not generate the same outcomes. Model 1 generates the correct outcome but Model 2 is not. I think I do something fundamentally wrong. I do not know where. $\endgroup$ – Tugrul Temel Dec 5 '20 at 21:13
  • $\begingroup$ and MarcB: I have found out the problem, which is not related to Derivative. In both models, the way I coded the derivatives is identical and generates the identical results when I increase the time interval with the right choices of the differential equations. Thank you for your feedback. $\endgroup$ – Tugrul Temel Dec 5 '20 at 21:47
  • $\begingroup$ Thanks so much for your revised Code. It made my life so easy now that I can run models with simple statements. You solved my main problem. Your help is much appreciated. $\endgroup$ – Tugrul Temel Dec 5 '20 at 23:10

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