0
$\begingroup$

I'm trying to run a simulation, which at the end gives a value 'qber'. Now I want to run this simulation multiple times with different values of 'p' each time. And these value of 'p' are actually the elements of a list already defined before. My code is as follows:

x := RandomChoice[{0, π}];
y := RandomChoice[{0, π/2, π, 3 π/2}];
prob = {0.25, 0.05}
f2[{result_, plist_}] := 
   With[{k = RandomReal[]}, 
     Which[
       k < 0.7, {0, plist}, 
       (k > 0.7) && (k < 0.7 + plist[[1]]), 
         {π/10, If[plist[[1]] == prob[[2]], plist, Reverse @ plist]},
       True, {-π/10, If[plist[[1]] == prob[[1]], plist, Reverse @ plist]}]]
 mylist = #[[1]] & /@ NestList[f2, {0, prob}, 9]
 e = Accumulate[mylist]
{0.15, 0.15} 
{0, 0, 0, -(π/10), π/10, 0, -(π/10), 0, 0, -(π/10)}
{0, 0, 0, -(π/10), π/10, 0, -(π/10), 0, 0, -(π/10)}

Then the last list in the above output is used for the values of p, in the following for-loop:

For[i = 1; i <= 10, i++, 
  p = e[[i]]; 
  Module[{}, 
    dat3 = Table[x - y + p, 100];
    dat34 := Mod[dat3, 2 π];
    dat334 := Transpose[{dat3, dat34}];
    ph0 := Select[dat334, #[[1]] == 0 &];
    ph1 := Select[dat334, #[[1]] == π || #[[1]] == -π &];
    ph2 := 
      Select[dat334, 
        #[[1]] == π/2 || #[[1]] == -π/2  || #[[1]] == 3 π/2 || #[[1]] == -3 π/2 &];
    dats := ph0[[All, 2]];
    datt := ph1[[All, 2]];
    datu := ph2[[All, 2]];
    datscos := N[(Cos[(dats)/2])^2];
    dattcos := N[(Cos[(datt)/2])^2];
    datucos := N[(Cos[(datu)/2])^2];
    det1ss := Length[datscos]; 
    det1tt := Length[dattcos];
    det1uu := Length[datucos];
    rr := RandomReal[{0, 1}];
    For[j = 1; det1s = 0, j <= det1ss, j++, 
      If[rr <= datscos[[j]], det1s = det1s + 1];];
    For[k = 1; det1t = 0, k <= det1tt, k++, 
      If[rr <= dattcos[[k]], det1t = det1t + 1];];
    For[l = 1; det1u = 0, l <= det1uu, l++, 
      If[rr <= datucos[[l]], det1u = det1u + 1];];
    det2s := det1ss - det1s;
    det2t := det1tt - det1t;
    det2u := det1uu - det1u;
    aa := {det1s, det2s};
    bb := {det1t, det2t};
    cc := {det1u, det2u};
    det1 := {{1, det1s}, {3, det1t}, {5, det1u}};
    det2 := {{1.5, det2s}, {3.5, det2t}, {5.5, det2u}};
    qber = N[((det2s/(det1s + det2s)) + (det1t/(det1t + det2t)))/2];];
    Print[qber]]

Now when I try to run it, it doesn't give any output. And when I keep the print command outside the for loop(just to check), it says Indeterminate. But I don't think the value of qber can come out to be Indeterminant in anyway. However, I'm unable to find any bug in the code. If you can, then please help me out.

P.S.: I also tried to run the for-loop without using Module, and directly putting all the values inside the for-loop. But still it gives the same error.

$\endgroup$
3
  • $\begingroup$ Look up the syntax of For. You use semicoli where commata are in order. Even better: use Do. Btw: Mathematica is neither Pascal nor Maple. An simple assignment is done with =, not with :=. $\endgroup$ Jun 14, 2018 at 16:13
  • 1
    $\begingroup$ @HenrikSchumacher I have used := for delayed assignment. And that gives different random values everytime I call it, and that's what I need. Also, I have used semicolon, because there are inside a module. And yes, I agree that at some places, delayed assignment wasn't required, still I used it. My bad! $\endgroup$
    – Rishabh
    Jun 14, 2018 at 16:16
  • $\begingroup$ Does it work, if you fix the For loop? Additionally, when you use Module with no local variables, you don't need it. It has no effect other than lengthening your code. $\endgroup$
    – halirutan
    Jun 14, 2018 at 21:54

1 Answer 1

3
$\begingroup$

I fixed the semicolon/comma thing and put the print statement inside the module but you have some situations where you are dividing by zero causing qber to be indeterminate. This happens when det1s = -det2s or when det1t = -det2t. Adding an if statement to qber prevents dividing by zero.

For[i = 1, i <= 10, i++,
 p = e[[i]];
 Module[{}, dat3 = Table[x - y + p, 100];
  dat34 := Mod[dat3, 2 \[Pi]];
  dat334 := Transpose[{dat3, dat34}];
  ph0 := Select[dat334, #[[1]] == 0 &];
  ph1 := Select[dat334, #[[1]] == \[Pi] || #[[1]] == -\[Pi] &];
  ph2 := Select[
    dat334, #[[1]] == \[Pi]/2 || #[[1]] == -\[Pi]/2 || #[[1]] == 
       3 \[Pi]/2 || #[[1]] == -3 \[Pi]/2 &];
  dats := ph0[[All, 2]];
  datt := ph1[[All, 2]];
  datu := ph2[[All, 2]];
  datscos := N[(Cos[(dats)/2])^2];
  dattcos := N[(Cos[(datt)/2])^2];
  datucos := N[(Cos[(datu)/2])^2];
  det1ss := Length[datscos];
  det1tt := Length[dattcos];
  det1uu := Length[datucos];
  rr := RandomReal[{0, 1}];
  For[j = 1; det1s = 0, j <= det1ss, j++, 
   If[rr <= datscos[[j]], det1s = det1s + 1];];
  For[k = 1; det1t = 0, k <= det1tt, k++, 
   If[rr <= dattcos[[k]], det1t = det1t + 1];];
  For[l = 1; det1u = 0, l <= det1uu, l++, 
   If[rr <= datucos[[l]], det1u = det1u + 1];];
  det2s := det1ss - det1s;
  det2t := det1tt - det1t;
  det2u := det1uu - det1u;
  aa := {det1s, det2s};
  bb := {det1t, det2t};
  cc := {det1u, det2u};
  det1 := {{1, det1s}, {3, det1t}, {5, det1u}};
  det2 := {{1.5, det2s}, {3.5, det2t}, {5.5, det2u}};
 (* If[det1s + det2s != 0 && det1t + det2t != 0,
   qber = N[((det2s/(det1s + det2s)) + (det1t/(det1t + det2t)))/2];
   Print[qber]];*)
  othervals := {det1sval -> det1s, det2sval -> det2s, det1tval -> det1t,
    det2tval -> det2t};
  If[det1s + det2s != 0 && det1t + det2t != 0, 
    qber = N[((det2s/(det1s + det2s)) + (det1t/(det1t + det2t)))/2];
   Print[{qberval -> qber, othervals}], 
   Print[{qberval -> Undefined, othervals}]]
  ]
 ]
$\endgroup$
3
  • $\begingroup$ Thanks for fixing it. And it gives output for only 2-3 out of 10. But I wonder how's that possible! Because as you can see from those for-loops, they start from 0, and there's no way that anyone of them can go negative. And one more thing I noticed just now is that, output is indeterminate for only non-zero values of 'p'. $\endgroup$
    – Rishabh
    Jun 15, 2018 at 10:04
  • 1
    $\begingroup$ You can have it print out some values to help debugging see last two lines of edited answer. $\endgroup$ Jun 15, 2018 at 17:15
  • 1
    $\begingroup$ Sometimes det1s, det2s det1t and det2t are zero. $\endgroup$ Jun 15, 2018 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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