1
$\begingroup$

I'm working with a system of n-equations andNsolve 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]

How can I add that:

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.

$\endgroup$
  • $\begingroup$ Try to use FindRoot $\endgroup$ – Alexei Boulbitch Jun 26 '18 at 20:44
  • 1
    $\begingroup$ There is no such thing like Nsolve; there is NSolve, though. $\endgroup$ – corey979 Jun 26 '18 at 20:48
  • $\begingroup$ Mmm it doesnt work, I get this error: Nsolve: eq1 should be a length 1 list of real-valued quantities. $\endgroup$ – Rodrigo Jun 26 '18 at 21:25
  • 1
    $\begingroup$ 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.] $\endgroup$ – Michael E2 Jun 26 '18 at 22:10
4
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ Mmm couldn't add the AllTrue to the NSolve with my current structure of EQ and X $\endgroup$ – Rodrigo Jun 26 '18 at 21:30
  • $\begingroup$ @Rodrigo I guess you need to explain why in the question. (E.g. add a minimal working example.) $\endgroup$ – Michael E2 Jun 26 '18 at 21:34
  • $\begingroup$ 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. $\endgroup$ – Rodrigo Jun 26 '18 at 21:48
  • $\begingroup$ @Rodrigo "Working" means it works when a user pastes it into Mathemetica -- thanks for the edit. $\endgroup$ – Michael E2 Jun 26 '18 at 22:07

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.