# Constrain variables when using NSolve

I'm working with a system of n-equations and Nsolve takes too long to get the results. I'm trying to speed things up by adding a restriction to the solutions, but I'm not sure how to set the restriction to all the variables in the list.

EQ = {eq1, eq2, ..., eqN}
X = {var1, var2, ..., varN}
NSolve[EQ, X]


var1,va2, ..., varN ∈ Interval[{0,a}]


without going 1 by 1 since the size n may change?

### Edit

So here is the actual code for n=4:

EQ =
{5. (1/4 (Subsuperscript[respr, 1, 2]/2 - Subscript[respr, 1] + 1/2) +
1/4 (Subsuperscript[respr, 2, 2]/2 - Subscript[respr, 2] + 1/2) +
1/4 (Subsuperscript[respr, 3, 2]/2 - Subscript[respr, 3] + 1/2) +
1/4 (Subsuperscript[respr, 4, 2]/2 - Subscript[respr, 4] +
1/2)) -
Subscript[respr, 1] + (1 - Subscript[respr, 1])
((1 - Subscript[respr, 1])/
(4 (1/4 (1 - Subscript[respr, 2]) +
1/4 (1 - Subscript[respr, 3]) +
1/4 (1 - Subscript[respr, 4])) + 0.8) + 1) + 0.5,
5. (1/4 (Subsuperscript[respr, 1, 2]/2 - Subscript[respr, 1] + 1/2) +
1/4 (Subsuperscript[respr, 2, 2]/2 - Subscript[respr, 2] + 1/2) +
1/4 (Subsuperscript[respr, 3, 2]/2 - Subscript[respr, 3] + 1/2) +
1/4 (Subsuperscript[respr, 4, 2]/2 - Subscript[respr, 4] +
1/2)) -
Subscript[respr, 2] + (1 - Subscript[respr, 2])
((1 - Subscript[respr, 2])/
(4 (1/4 (1 - Subscript[respr, 1]) +
1/4 (1 - Subscript[respr, 3]) +
1/4 (1 - Subscript[respr, 4])) + 0.8) + 1) + 0.5,
5. (1/4 (Subsuperscript[respr, 1, 2]/2 - Subscript[respr, 1] +
1/2) + \1/4 (Subsuperscript[respr, 2, 2]/2 -
Subscript[respr, 2] + 1/2) +
1/4 (Subsuperscript[respr, 3, 2]/2 - Subscript[respr, 3] + 1/2) +
1/4 (Subsuperscript[respr, 4, 2]/2 - Subscript[respr, 4] +
1/2)) -
Subscript[respr, 3] + (1 - Subscript[respr, 3])
((1 - Subscript[respr, 3])/
(4 (1/4 (1 - Subscript[respr, 1]) +
1/4 (1 - Subscript[respr, 2]) +
1/4 (1 - Subscript[respr, 4])) + 0.8) + 1) + 0.5,
5. (1/4 (Subsuperscript[respr, 1, 2]/2 - Subscript[respr, 1] + 1/2) +
1/4 (Subsuperscript[respr, 2, 2]/2 - Subscript[respr, 2] + 1/2) +
1/4 (Subsuperscript[respr, 3, 2]/2 - Subscript[respr, 3] + 1/2) +
1/4 (Subsuperscript[respr, 4, 2]/2 - Subscript[respr, 4] +
1/2)) +
((1 - Subscript[respr, 4])/
(4 (1/4 (1 - Subscript[respr, 1]) +
1/4 (1 - Subscript[respr, 2]) +
1/4 (1 - Subscript[respr, 3])) + 0.8) + 1)
(1 - Subscript[respr, 4]) - Subscript[respr, 4] + 0.5}

X =
{Subscript[respr, 1], Subscript[respr, 2],
Subscript[respr, 3], Subscript[respr, 4]}


Im solving this by:

NSolve[EQ, X]


I would like to set a restriction to all the variables Subscript[respr,1]. However I cannot use && Subscript[respr,1] \[Element] Interval[{0,1}], 1 by 1 since n may be 10 or 100 in each run.

• Try to use FindRoot Jun 26 '18 at 20:44
• There is no such thing like Nsolve; there is NSolve, though. Jun 26 '18 at 20:48
• Mmm it doesnt work, I get this error: Nsolve: eq1 should be a length 1 list of real-valued quantities. Jun 26 '18 at 21:25
• Note that the variable Subsuperscript[respr, 1, 2] has no relation to the variable Subscript[respr, 1]. I suppose you meant to square the variable Subscript[respr, 1]? That's done with Subscript[respr, 1]^2 (or Power[]). [The standard advice is to avoid Subscript, except for output formatting -- see mathematica.stackexchange.com/a/18395/4999.] Jun 26 '18 at 22:10

Something like this?:

NSolve[{x^2 + y^2 == 1, x^2 - y^2 + y^3 == 1/2,
AllTrue[{x, y}, {#} ∈ Interval[{0, 2}] &]}]
(*  {{x -> 0.802265, y -> 0.596968}}  *)


I don't really think that will speed things up, though, but that might depend on the actual system of equations.

Update: Response to updated question

Block[{Subsuperscript = Subscript[#1, #2]^#3 &},
NSolve[EQ~Append~AllTrue[X, {#} \[Element] Interval[{0, 1}] &], X]
]
(*
{{Subscript[respr, 1] -> 0.808928, Subscript[respr, 2] -> 0.808928,
Subscript[respr, 3] -> 0.808928, Subscript[respr, 4] -> 0.808928}}
*)


I'm going out on a limb and guessing that Subsuperscript[respr, 1, 2], etc. are meant to represent Subscript[respr, 1]^2 and so forth.

• Mmm couldn't add the AllTrue to the NSolve with my current structure of EQ and X Jun 26 '18 at 21:30
• @Rodrigo I guess you need to explain why in the question. (E.g. add a minimal working example.) Jun 26 '18 at 21:34
• well I thought I had that, I have a list of equations (EQ), and a list of variables (X) and the syntax that I'm using. I'll work into something more detailed, then. Jun 26 '18 at 21:48
• @Rodrigo "Working" means it works when a user pastes it into Mathemetica -- thanks for the edit. Jun 26 '18 at 22:07