# Questions tagged [scoping]

Questions on Mathematica's scoping constructs (Block, Module, With, etc.)

496 questions
Filter by
Sorted by
Tagged with
49 views

### How to "Hold" variables assigned value inside DSolve?

Consider the following ODE: a = 0.8; b = 8; v = DSolveValue[{ u[x] == a*(b* u'[x] + u''[x]), u[4.925] == 4, u'[4.925] == 0.625 }, u[x], x] which gives ...
53 views

### Recursiveness in backtracking algorithm

In order to familiarise with using basic algorithms in Mathematica, I am coding a sudoku solver using a backtracking algorithm. I am treating the sudoku itself as an association (I want to learn to ...
45 views

### How to use two separate colours for two lines in a single 'Plot' within the 'With' command?

'''With[{k = {1, 10}}, Plot[k*x^2, {x, 0, 1}]]''' I want to use two separate colours for two k values. How to do that? Thanks in advance.
44 views

### Returning (not displaying) graphics from a Module

I want to create a Module which puts several parametric surfaces together as one object. For example, say I want to create a module which returns a cylinder that has a top, bottom, and cylindrical &...
89 views

### Is it a good idea to unprotect standard commands?

I often see in code by other people some standard functions like Sum, NonCommutativeMultiply etc get unprotected, supplemented ...
86 views

### Strange Behavior of Compile[] inside With[]

Consider, in the examples below, the difference between defining $n$ as a global variable or as an argument to function comp[], which includes a compiled function ...
55 views

### Keep irrelevant variables outside function arguments

I want to define several functions of variables x,y say ...
63 views

### NDSolve and NIntegrate with parameters

I have a problem with a numeric computation. I should solve a numeric ODE and the rhs is a numeric parametric function that I find with several NIntegrate. I'd like ...
30 views

### Return exact solution in one variable from Series as an option

I have a module which expands many functions, depending on expansion order input parameters. Here's an MRE: ...
60 views

### How to partition $[0,1]$ into $m$ equal sub-intervals and count the number of sub-intervals that intersect with my cantor set?

Suppose I define my cantor set as ...
44 views

### Why is my dynamic slider no longer working?

This used to work but I accidentally made some unrecoverable edits and it stopped displaying. It supposed to adjust the background color of the current notebook. Please have a look and see what is ...
153 views

### Bad behavior of Block function [closed]

It seems that there is an extreme bug in the Block function that has puzzled me a lot today. Below I give two code samples. In the first one can see a bug. In the second one can see that this bug is ...
44 views

### why does dataset not preserve its associations in Module[]? [closed]

Hello I am perplexed as to why a dataset, ds1 in this example, does not return the association with index and content in it. ...
69 views

### Blank plot is returned when trying to use Manipulate[Plot3D...] on the output function of NDSolve

I am currently trying to solve a transient cylindrical diffusion problem with space variable (r,z). Using NDSolve, I get a certain Interpolation function but I can't plot it no matter what I try. ...
213 views

### Proper Syntax highlighting for multi-argument With?

For the case of Block and Module one can choose to define values in the body for the variable names being scoped. So there is no ...
52 views

### Using Block with Conditions /;

I would like Block to run conditionally. I understand how to do one condition, but what about an AND operation? e.g ...
39 views

### How to return a variable name and value from within a self-contained function block?

Abstract: I have viewed other solutions on StackExchange but none actually address using a fully self-contained function to return name and value of a variable. The only function that works requires ...
59 views

### How to externally access local variables defined inside a With block

I am writing a package in which I have some constants defined inside a With block, which are then used by various functions defined inside that block. Separate to ...
39 views

### Using local variables inside WhenEvent

Using WhenEvent inside Module is quite strange. The local variables are not evaluated in ...
54 views

### Module[Monitor[ ... ]] doesn't work when called by Manipulate?

My example function progressFunction[time_] := Module[{t}, Monitor[For[t = 0, t <= time, t++, Pause[1]], ProgressIndicator[t, {0, time}]]] does ...
487 views

### What causes Module variables to leak?

Sometimes, after writing code like f[] := Module[{a}, ...] and running f[] multiple times, there will be many ...
105 views

### Local variables in sums and tables - best practices?

Stumbled on Local variables when defining function in Mathematica in math.SE and decided to ask it here. Apologies if it is a duplicate - the only really relevant question with a detailed answer I ...
43 views

### New variable in With structure appear as global, and many other strange things I don't understand

I thought I know With and Module until I see this example on Wolfram Documentation. Below is a demo code: ...
98 views

### Again about Module inside other Module

Let me start with a short preamble. I have a large function (defined through other functions) which I need to integrate numerically. This works. Trying to speed up the process, I played with ...
32 views

### Evaluating global variable within a module

I'm trying to work with something like the following: a = b; f[x_, b_] := Module[{N}, N = Abs[a]; 10 N ] f[1, 1] The output of running this is: ...
80 views

103 views

### Is it possible to use a previously defined value in subsequent definitions of a Block?

Is it possible to use a previously defined value in subsequent definitions of a Block? According to syntax coloration, this should work as expected, that is, ...
81 views

### How to keep symbol bindings local to a Notebook section?

I'm using the Notebook on Wolfram-Cloud as a ... well ... notebook to keep notes about the topic I'm studying and to answer exercises. I usually keep one notebook file per chapter, with course notes ...
42 views

### Does passing patterns between packages alter their name unbeknownst to the user? [duplicate]

This question is a partial followup to my previous question. Mainly, after using With to inject the proper argument in the function at the rhs of ...
50 views

### How can I make parameters set by an array of buttons known to other quantities in my model?

With help from @m_goldberg, I developed the following model: ...
60 views

### Handling a list of variable assignments to be used with With [duplicate]

Is there a way to use With as follows? variables := {a = 2, b = 3}; With[variables, a^b] Desired output ...
37 views

### Mathematica Function Taxicab Numbers [duplicate]

I have spent days trying to compile this code and I cannot generate a list of taxicab numbers for the Hardy-Ramanujan Theory. Here is the module I wrote earlier: ...
26 views

### Referencing a control from within a module

Below is a simple Manipulate that references a module via a button, which resets a slider and value. I would like to reference and reset the slider control for the slider "n" from within the module "...
37 views

### Problem in solving a trancedental equation which is embedded in module

I have written a small program to compute the resonance frequency, for that I am solving a transcendental equation, which comes from the determinant of the matrix ...
84 views

### Turning an equation into a function in a module

I have defined a function like this: f[q_] := Module[{g}, g[x_] := Evaluate[q]; ... ] I am calling the function like this: ...
44 views

### For different value of input module is giving same result

I have a simple module written to extract a certain shape for a certain value of ω, but I am getting the same result for different input values into the module. 1.mode[1] and mode[2] looks exactly ...
169 views

### Memory leak with FindMaximum/Minimum inside of Module

This is my first question here. I have to solve coupled first order ODE-s and find the maximum of a function which depends on the solution of the ODE. I have to do this calculation in large quantities,...
53 views

### Is it possible to create complicated packages from modules which work fine by copy paste? -- A counterexample [duplicate]

Here is a module which constructs essentially several functions in a chain [Phi], sct, ks, WE, G: ...