1
$\begingroup$

I'm having trouble with nesting if statements in Mathematica due to the lack of elif function as it is done in Python. There are some questions relating to this topic but the solutions imply on very particular problems and I'm just interested in clean If statement nesting for evaluation on different equations.

I have 4 equations that are solved using NSolve. I want Mathematica to solve one equation AFTER another and if it occurs, that one equation (let's say the 2nd one) satisfies a condition, display the result and start a new iteration (no need to evaluate 3rd and 4th equation).

The code is as follows:

For[hi = 1, hi <= 3, hi++,
 For[ti = 0 Degree, ti <= 3 Degree, ti+=1 Degree,
  a = NSolve[{
     (x1*y1)/2 == s*hi,
     x1*Cos[ti] == y1*Sin[ti],
     0 < x1 <= v,
     0 < y1 <= s
     },{x1, y1}];
  If[Length[a] != 0,
   Print["h=", hi, " t=", ti, " ->1. ", a[[1]]],
   b = NSolve[{
       (x2 + z2)/2 == hi,
       (x2 - z2) Cos[ti] == s*Sin[ti],
       0 < x2 <= v,
       0 < z2 <= v
       }, {x2, z2}]
     &&
     If[Length[b] != 0,
      Print["h=", hi, " t=", ti, " ->2.", b[[1]]],
      c = NSolve[{
          w3*v + ((y3 - w3) v)/2 == s hi,
          v Cos[ti] == (y3 - w3) Sin[ti],
          0 < w3 <= s,
          0 < y3 <= s
          }, {w3, y3}]
        &&
        If[Length[c] != 0,
         Print["h=", hi, " t=", ti, " ->3. ", c[[1]]],
         d = NSolve[{
             v w4 + s z4 - z4 w4 + v s == 2 s hi,
             (w4 - s) Sin[ti] == (z4 - v) Cos[ti],
             0 < w4 <= s,
             0 < z4 <= v
             }, {w4, z4}]
           && 
           Print["h=", hi, " t=", ti, " ->4. ", d[[1]]]]
      ]
   ]
  ]
 ]

My reasoning is as follows: Evaluate "a". If "a" gives a non-empty result display it, else evaluate "b" AND see if "b" gives a non-empty result. If "b" gives a non-empty result, display it, else evaluate "c" etc.

v and s are just some constants with moderate values (eg. 100).

$\endgroup$
  • $\begingroup$ Which might be better suited than nesting If. $\endgroup$ – Daniel Lichtblau Jul 9 '17 at 14:59
  • $\begingroup$ How does that fair with displaying results? Will it try to display all result even the ones that aren't acceptable? $\endgroup$ – KeVal Jul 9 '17 at 15:04
  • $\begingroup$ You'll have to try it to find out... $\endgroup$ – Daniel Lichtblau Jul 9 '17 at 15:19
1
$\begingroup$

Since what you want to do for each of the satisfactory conditions is almost identical, you could make use of ; and the fact that Or bails out immediately on the first True case it finds.

For[hi = 1, hi <= 3, hi++,
 For[ti = 0 Degree, ti <= 3 Degree, ti += 1 Degree,
  Or[
   sol=NSolve[{(x1*y1)/2==s*hi,x1*Cos[ti]==y1*Sin[ti],0<x1<=v,0<y1<=s}, {x1, y1}];
      n=1; Length[sol] != 0,
   sol=NSolve[{(x2+z2)/2==hi,(x2-z2)Cos[ti]==s*Sin[ti],0<x2<=v,0<z2<=v}, {x2,z2}];
      n=2; Length[sol] != 0,
   sol=NSolve[{w3*v+((y3-w3)v)/2==s hi,v Cos[ti]==(y3-w3)Sin[ti],0<w3<=s,0<y3<=s}, {w3,y3}];
      n=3; Length[sol] != 0,
   sol=NSolve[{v w4+s z4-z4 w4+v s==2 s hi,(w4-s)Sin[ti]==(z4-v)Cos[ti],0<w4<=s,0<z4<=v},{w4,z4}];
      n=4; True]; 
  Print["h=", hi, " t=", ti, " ->", n, sol[[1]]]]]

But I would urge you to leave yourself a really clear comment explaining to your future self what, why and how you did this in this way. This isn't very different from using Which.

$\endgroup$
  • $\begingroup$ This works perfectly, thank you very much! I'm not yet so familiar with Mathematica and haven't used much more than For loops and single If statements. I'm more skilled in Python so I try to use similar functions but I see Mathematica does some quite differently. I'll look into Which function though! $\endgroup$ – KeVal Jul 9 '17 at 18:44

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.