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.
For
. You use semicoli where commata are in order. Even better: useDo
. Btw: Mathematica is neither Pascal nor Maple. An simple assignment is done with=
, not with:=
. $\endgroup$:=
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$For
loop? Additionally, when you useModule
with no local variables, you don't need it. It has no effect other than lengthening your code. $\endgroup$