I want to use Table
or Array
to generate a list of events for NDSolve
, but the variables (gap[k]
) in the rules of the events are not being assigned their values. Is there a simple way to modify this?
I found some answers related to my problem (here and here), but they don't seem to solve it.
Clear["Global`*"];
nk = 3;
Table[gap[k] = k, {k, nk}];
Table[With[{ki = k},
{WhenEvent[f[ki][t] == 0, {c[ki][t] -> d[ki][t] + gap[ki]}]}],{k, 1, nk}]
Array[
{WhenEvent[x[#][t] == 0, {c[#][t] -> d[#][t] + gap[#]}],
WhenEvent[y[#][t] == 0, {c[#][t] -> d[#][t] + gap[#]}]} &, nk]
update1 : my problem vs. problem here
If I understand right, the answer here is to use Evaluate
to force computation of variables in the event. But in my problem, using Evaluate
on variable gap[k]
is not working. Please check the result of the following code.
Table[With[{ki = k}, {WhenEvent[Evaluate@gap[ki] + f[ki][t] == 0,
{c[ki][t] -> d[ki][t] + Evaluate@gap[ki]}]}], {k, 1, nk}]
Evaluate
:Table[With[{ki = k}, {WhenEvent[ Evaluate[gap[ki] + f[ki][t] == 0], {c[ki][t] -> d[ki][t] + Evaluate@gap[ki]}]}], {k, 1, nk}]
As mentioned in Details section of document ofEvaluate
: "Evaluate
only overridesHoldFirst
etc. attributes when it appears directly as the head of the function argument that would otherwise be held. " $\endgroup$