1
$\begingroup$

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[1], {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][#[[1]], {x, 0, L}] == 0 & /@ Transpose[{eqns2}];
eqns2 = Array[Eq, n];
theEqns2 = LstEqns2;
Eq[1] = 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
$\endgroup$
2
  • 3
    $\begingroup$ 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}]. $\endgroup$
    – Domen
    Feb 6 at 17:31
  • $\begingroup$ I edited the question. It wasn't really N in my code, I used N here for simplicity and forgot that it is reserved $\endgroup$
    – user15933
    Feb 6 at 18:52

1 Answer 1

5
$\begingroup$
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[1], Eq[2], Eq[3], Eq[4], Eq[5]} *)

theEqns = LstEqns

enter image description here

EDIT: If the RHS of the equations aren't zero

n = RandomInteger[{1, 10}]

(* 6 *)

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

eqns2 = Array[Eq, n];

rhs = Array[RightPart, n];

theEqns2 = LstEqns2

enter image description here

EDIT 2: For the revised question

n = 1;
LstEqns2 := 
  Inactive[NIntegrate][#[[1]], {x, 0, L}] == 0 & /@ Transpose[{eqns2}];
eqns2 = Array[Eq, n];
theEqns2 = LstEqns2;
Eq[1] = 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.} *)
$\endgroup$
6
  • $\begingroup$ Thank you. What modifications there will be needed if Integral[Eq[i]] is equal not to zero by to some RightPart[i]? $\endgroup$
    – user15933
    Feb 6 at 19:58
  • $\begingroup$ 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 $\endgroup$
    – user15933
    Feb 9 at 21:38
  • $\begingroup$ Have you defined L and the vectors? Without seeing your code I cannot say why it doesn’t work as you expect. $\endgroup$
    – Bob Hanlon
    Feb 9 at 21:49
  • $\begingroup$ 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 $\endgroup$
    – user15933
    Feb 10 at 14:41
  • $\begingroup$ 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. $\endgroup$
    – Bob Hanlon
    Feb 10 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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