# Creating a variable-length list of integral equations using a loop

I need to automatically form a list of equations that later will be used in FindRoot. The number of equations (NN) is variable, so I need to use a For loop. When I am doing this

LstEqns = {}; For[i = 1, i <= NN, i++,
LstEqns =
Append[LstEqns,
NIntegrate[Eq[i], {x, 0, L}] == 0];
];


I am getting a warning that L is not a valid limit of integration (the value is not provided at this point) and in the list I am not getting i substituted by its proper values, i.e. LstEqns looks like {NIntegrate[Eq[i], {x, 0, L}] == 0,...} instead of {NIntegrate[Eq, {x, 0, L}] == 0,...}. The value of L is provided later (as well as actual Eq[i]), after forming the list. So the question is - how can I get a list of equations with a proper numbering? Mathematica 11.3 is what I am using if this matters.

UPD

n = 1;
LstEqns2 :=
Inactive[NIntegrate][#[], {x, 0, L}] == 0 & /@ Transpose[{eqns2}];
eqns2 = Array[Eq, n];
theEqns2 = LstEqns2;
Eq = x*y + 1;
L = 1;

FindRoot[theEqns2, {y, 0}, Evaluated -> False]

• doesn't work ("The function value is not a list of numbers")

Manually copying the output of theEqns2 into FindRoot doesn't work either:

FindRoot[Inactive[NIntegrate][x*y + 1, {x, 0, L}], {y, 0},
Evaluated -> False]

• same error

However, the same line as above will work if I delete inactive NIntegrate (that has a different color in Mathematica) and placeholder and then write NIntegrate again

FindRoot[NIntegrate[x*y + 1, {x, 0, L}] == 0, {y, 0},
Evaluated -> False]

• this code works
• First, don't use capital N as a variable because it has a predefined meaning in Mathematica. Second, use Hold or Inactive, for example: Hold@NIntegrate[Eq[i], {x, 0, L}] or Inactive[NIntegrate][Eq[i], {x, 0, L}]. Feb 6 at 17:31
• I edited the question. It wasn't really N in my code, I used N here for simplicity and forgot that it is reserved Feb 6 at 18:52

Clear["Global*"]


Whenever you think that you "need" a For loop, you probably don't.

LstEqns := Inactive[NIntegrate][#, {x, 0, L}] == 0 & /@ eqns


Note that what you are labeling Eq[i] are not equations but rather expressions. Since they will be the integrand and will later be the LHS of an equation, they should not have a Head of Equal

n = RandomInteger[{1, 10}]

(* 5 *)

eqns = Array[Eq, n]

(* {Eq, Eq, Eq, Eq, Eq} *)

theEqns = LstEqns EDIT: If the RHS of the equations aren't zero

n = RandomInteger[{1, 10}]

(* 6 *)

LstEqns2 :=
Inactive[NIntegrate][#[], {x, 0, L}] == #[] & /@
Transpose[{eqns2, rhs}]

eqns2 = Array[Eq, n];

rhs = Array[RightPart, n];

theEqns2 = LstEqns2 EDIT 2: For the revised question

n = 1;
LstEqns2 :=
Inactive[NIntegrate][#[], {x, 0, L}] == 0 & /@ Transpose[{eqns2}];
eqns2 = Array[Eq, n];
theEqns2 = LstEqns2;
Eq = x*y + 1;
L = 1;


theEqns2 must be activated; and since FindRoot has the attribute HoldAll, you must Evaluate the first argument, i.e.,

FindRoot[Evaluate[theEqns2 // Activate], {y, 0}, Evaluated -> False] // Quiet

(* {y -> -2.} *)

• Thank you. What modifications there will be needed if Integral[Eq[i]] is equal not to zero by to some RightPart[i]? Feb 6 at 19:58
• Thanks, this forms the equation set. However, FindRoot with theEqns2 plugged in doesn't work - probably, because NIntegrate is inactive. What is the proper way to activate it? Just Activate[theEqns2] doesn't work Feb 9 at 21:38
• Have you defined L and the vectors? Without seeing your code I cannot say why it doesn’t work as you expect. Feb 9 at 21:49
• Yes, everything is defined. Using formed as shown in the example theEqns2 doesn't work in FindRoot. Copypasted set of equations {...} doesn't work either - NIntegrate is inactive (has a different color). If, however, I manually delete the inactive NIntegrate command and the placeholder appearing after that and write NIntegrate once again, the equations work in FindRoot Feb 10 at 14:41
• No Inactive function will evaluate until you Activate` it. I cannot work with a vague description of code. Post a question with the actual code for a minimal example that shows what you have tried. Feb 10 at 16:05