0
|
6515 Views
|
|
1 Total Likes
View groups...
Share
GROUPS:

# Global and local variables

Posted 10 years ago
 (* Capture Recapture History *)  myCHcc[K_] := Table[IntegerDigits[n, 2, K], {n, 1, 2^K - 1}]  (* Capture Recapture Probability *)  myindprobCC[CH_]:=Module[{K,ind,fi,li,ans},      (* Get $K$ *)    K = Length[ CH ];        (* First and Last Capture times *)    fi = First[Position[CH, 1]][];    li =  Last[Position[CH, 1]][];        (* Get probabilities *)        ans =  Sum[   Product[     Subscript[\[Phi],j]    *    Subscript[p,j+1]^CH[[j+1]]    *    (1-Subscript[p,j+1])^(1-CH[[j+1]]),    {j,fi,d-1}   ]*(1-Subscript[\[Phi],d]),   {d, li, K}  ];        ans /. Subscript[\[Phi], K]-> 0]K = 3xij = myCHcc[K]ans = Map[myindprobCC, xij]Runs perfectly.There variables involved arepars =Variables[ans]However, say by accident, I have doneSubscript[p, 3] = "Dummy"BEFORE, I define my function "myindprobCC[CH_]", then we have a problem.Is there a way to "automatically" put ALL "possible" variables, to the Module as local variables, like what i did for "{K,ind,fi,li,ans}".The problem is that, with the input arguments K_ and C_, the variables vary.And this is just a simple example.One way I can think of is to use Table[] to creat them and declare as local variables by hand, but is there a simpler way?Thanks
 Making a "subscripted variable" a local symbol in a Module also has difficulties. In:= Module[{Subscript[a, 1]},                                                                 Subscript[a, 1] = 1;                                                                     Print[Subscript[a, 1]]                                                                   ]                                                                                Module::lvsym: Local variable specification {a } contains a  ,                                                1            1      which is not a symbol or an assignment to a symbol. Out= Module[{a }, a  = 1; Print[a ]]                 1    1             1This is because Subscript[a, 1] is a function call, not a Mathematica symbol. The attached notebook has suggestions for making subscripted objects robust enough for executable code. (If you can postpone subscripts until you are making static output for presentation, that might save some work.) Attachments: