New answers tagged

4

The thing to do is pass the parameters to setUp and make that function hold its arguments. SetAttributes[setUp, HoldAll] setup[z_, numX_, x_] := (numX = Length[x]; Do[z[i] = StringLength[x[[i]]], {i, numX}]) g[x_List] := Module[{numX, z}, setup[z, numX, x]; Table[z[i], {i, numX}]] g[{"hi", "there"}] (* Out[1016]= {2, 5} *)


0

Local variables will be put in the first {} in Module[], for example: f[x_] := Module[{}, t = x^2]; f[2] (*--> 4*) t (*--> 4*) You will obtain t=4 after you evaluate the codes above, and if you f[x_] := Module[{t}, t = x^2]; f[2] (*--> 4*) t you will obtain t itself. Namely, t is just a global symbol(don't forget clear value ...


13

Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (Module, With or Block). For example: myFunction[x_]:= 2 Sin[x] + Exp[-x^2] But as the function definition gets more complex, instead of polluting the Global context with ...


2

As of 10.4 Where appears to no longer reside in "GeneralUtilities'" however I quite like its form so here is the <= 10.3 Definition (v. similar to M.R.'s answer) that can be placed in an init.m file. SetAttributes[Where, HoldAll]; Where[s : Verbatim[Set][x_List, y_], rest___, expr_] := With[{x2 = Quiet[Replace[Hold[x], z_Symbol :> z_, {2}]]}, ...


6

You can temporarily change CellContext of the notebook with SetOptions or CurrentValue but if the notebook isn't opened yet, this is faster: Module[{path, nb} , path = (*path*); nb = NotebookOpen[ path, CellContext -> "Global`", Visible -> False]; NotebookEvaluate[nb, InsertResults -> False]; NotebookClose[nb] ] You can use ...


0

Both Edmund's and Kuba's answers provided the key, which was just to substitute a different variable (like var or xp) that Manipulate won't touch. With that key, I created something more modular, using a Module structure to specify just once at the top which plots to make: ClearAll[f]; Module[{vars, varsAsPattern, varsToPlot, varFnSeq}, vars = {x, y, z}; ...


4

From the OP comments the slices (or contours) of a multi-dimensional function is what is being sought. A way to do this is the have each Plot in Manipulate set as Dynamic on the variables not on the x-axis. Such that, as these variables are adjusted the contour in the variable on the x-axis updates. For example, for $f(x,y,z) =z\sin(x)-y$ the contours in ...


2

As you've noticed both Plot and Manipulate are scoping constructs. And the problem is that you want to manipulate expressions that contain variables which are meant to be scoped and where is a naming conflict. What you want probably is possible but I think the fastest solution is just to avoid naming conflicts: With[{ exprsToPlot = {xp^y, x^yp}, ...



Top 50 recent answers are included