Tagged Questions

Questions on assigning and unassigning definitions to names that represent them, including issues raised by context and localization. Use tag [assignment] for issues relating to Set versus SetDelayed.

learn more… | top users | synonyms

0
votes
3answers
146 views

Clean-up Code / Reduce variables

I'm writing a playing card simulator of sorts, and everything works fine, but some Modules use a disgusting amount of variables. For Example: ...
0
votes
0answers
34 views

Block, With, Module, can anyone help me understand the distinction? [duplicate]

I've been working heavily with Block[] for months in my research so I've read the documentation for the seemingly similar commands With[], Block[], and Module[]. Personally, I've had by far the ...
1
vote
0answers
56 views

Telling mathematica that something is an integer [duplicate]

Is it possible for me to tell mathematica something is an integer? like if I typed Sin[n Pi]+Cos[n Pi] it would tell me it equals 1 for even n and -1 for odd n? ...
0
votes
0answers
91 views

Local variable names - Is there an easy way?

I apologize first, because this has been asked, like in Treat all variables as local in a module [duplicate] Variable scope similar to other languages But I don't get how to use them. Some codes ...
7
votes
1answer
234 views

Defining my own invisible operator

There are some invisible operators in Mathematica (\[ImplicitPlus],\[InvisibleTimes],...
0
votes
2answers
149 views

setting real parameters in Hamiltonian matrix

The following Matrix represents my Hamiltonian. m = {{ B/2 - d - j/2 + a, (Sqrt[2]*j)/2, M}, {(Sqrt[2]*j)/2, B/2 + a, 0}, {M, 0, (-3*B)/2 - d + j/2 + a} How can ...
3
votes
2answers
139 views

Bare symbol invocation fails but not if used with an argument

How to achieve the following behavior on a symbol f? Background: The main goal is to use the symbol to tag an abstract data structure where downvalues are used to store information. Only defined ...
10
votes
2answers
281 views

How to take derivative of parameterized coordinate?

Suppose I have a vector in $\mathbb{R}^n$ but $n$ is not known in advance. I want to be able to write functions which operate on the components of that vector, and then I'd like to be able to take ...
0
votes
2answers
121 views

Define a function on a parameter argument set to zero

Say I have an expression (matrix) that depends on several variables. f= {{a, b+c},{2a-b^2,c^5+a b}} I want to define new matrices with the single parameter ...
0
votes
0answers
35 views

Varying number of function arguments [duplicate]

I have a function of six arguments: ffit[q1_, q2_, q3_, q4_, q5_, q6_] := (a1*q1^2 + a2*q2^2 + a3*q3^2 + a4*q4^2 + a5*q5^2 + a6*q6^2) ...
1
vote
0answers
65 views

Why the variable cannot use subscript? [duplicate]

Today,I use the Lagarange method to deduce the robot 's dynamic equation .I use the mma to help me calculate. I want to use $$x_B,y_B, v_{Bx}, v_{By}, v_B ^2$$ to represent the point B 's ...
0
votes
2answers
109 views
0
votes
1answer
79 views

Circumventing the default scoping: Assign a variable inside a function [duplicate]

I'm currently trying very hard to assign a variable inside a function. Therefore I'm using a function like this, which should reassign a predefined variable. ...
1
vote
1answer
154 views

Is linear interpolation applicable here

From my understanding about Linear Interpolation especially of that of Newton's Divided Difference Polynomial, we could get the value of F(x) from the given set of ...
6
votes
1answer
154 views

How can I define a ⊕ operator?

What is wrong with the following code? CirclePlus := SparseArray[Band[{1, 1}] -> {##}] & a_ ⊕ b_ := CirclePlus[a, b] SetDelayed::write: Tag Function in ...
20
votes
3answers
383 views

Why x = x doesn't cause an infinite loop, but f[x_] := f[x] does?

If I execute: In[1] := x = x Out[1] = x and then I evaluate the symbol x: ...
4
votes
1answer
171 views

Forcing Mathematica to recognize a symbol without defining it

In the Mathematica front-end, symbols that have been defined are colored black and symbols with no definition associated are colored blue. This is useful to prevent spelling mistakes. Also, defined ...
4
votes
1answer
179 views

What does it mean when a function is defined with two pairs of brackets

I have noticed a function defined as such: f[x_][y_] := expr; How is the argument y in 2nd pair of brackets being used? What is the difference from defining a ...
1
vote
1answer
164 views
1
vote
1answer
85 views

Plotting with multiple variables [closed]

What is the simplest way of plotting across multiple variables? eg: I would like to plot something like this: Plot[{x^0,x^1,x^2,x^3,x^4,x^5,x^6}, {x, 0, 10}] ...
10
votes
4answers
554 views

Refering to global variables inside modules

How do I refer to global variables inside modules if the same name is used for a local one? In code, I want that ...
0
votes
0answers
105 views

NIntegrate with an arbitrary number of variables [duplicate]

I need to evaluate NIntegrate with a variable number of variables. Here's a concrete example. I'll accept an answer that finds a way to make the example work. I ...
3
votes
2answers
137 views

Assigning an equation as a variable

I am compiling a calculator where many slider parameters are used to define a value, where the defined value may then be reused to define another value. I am unable able to, or at least been ...
3
votes
3answers
116 views

Using a variable to tell Mathematica which existing variable to manipulate

I have an estimation routine I have coded up, I want now to do some tests on it where I pick a particular parameter and run the estimation for differing values of that parameter. What I want is to be ...
0
votes
0answers
36 views

Subscripts to a variable; why a letter shoud come before a digit?

I noticed that I cannot create a variable $f_{2s}$ since Mathematica seems to leave a space between the subscripts 2 and s ...
1
vote
2answers
127 views

Add a letter to a variable name from a list

Basically I am searching for a way to add the letter u to an existing list of variables {a,b,c,d} to obtain the variable list ...
0
votes
1answer
71 views

Collecting variables from Subscript

I have followings set: t = Transpose[{x[#] & /@ Range[20], RandomInteger[15, 20]}]; Subscript[#[[1]], #[[2]]] & /@ t The code is just to build my set.So ...
2
votes
2answers
296 views

Table of Variables

I use a list of variables {x1, x2, x3} to Solve a particular set of equations. I am now trying to generalise this depending on ...
2
votes
1answer
84 views

What is the need for function parameters? Many possible inputs

If I have many possible variables related to one another, why do I need to define my function with parameters for plotting? For example, if I have an equation like: ...
3
votes
1answer
156 views

Define a function with the condition that it must be positive

I have a function f(x) and I want to define a new function g(x) that is equal to f(x) only ...
2
votes
5answers
213 views

Treat list as cyclic

I am considering the following case. Say that T={p1,p2,p3} represents a triangle. In addition, I want to parameterize the edges of the triangle ...
1
vote
0answers
1k views

How to assign (and retrieve) variables with subscripts? [duplicate]

I like to give subscripts to my variable names when the variables are related. I.e., Subscript[r, i] = 3 3 A problem arises when I later assign ...
1
vote
3answers
118 views

Declare local variable when defining a function to mimic Table[]

The problem: I want to define a function tab to replace the long line Table[f[i,j,k,l],{i,3},{j,3},{k,3},{l,3}] by a shorter ...
4
votes
4answers
431 views

How can I prevent Superscript from being interpreted as Power?

I have very strong desire to use superscript as the index of the variable. However, it looks like that the Mathematica automatically recognize the superscript as the power and I got message that my ...
2
votes
2answers
101 views

Extracting variables from Hold expression

For example I have some global assignments x=1;y=2;z=3 and an expression Hold[x*y/z]. How can I now extract variables ...
3
votes
2answers
200 views

How to declare a function that uses local variables of a module outside this module?

I have the following code Module[{f,x=0}, f[t_] := x + t; ..........; ...f[1]...; ..........; ] My aim is to put the declaration of the function ...
6
votes
2answers
421 views

Obtaining the name of a variable as a string [duplicate]

Suppose that I have an arbitrary variable myVariable. Is there some function fun -- either built-in or one that I can define -- ...
8
votes
3answers
165 views

Protecting function definitions only for particular inputs

I would like to be able to define the values of a particular function for new inputs. f[x1]=f1; f[x2]=f2; I would also like to be able redefine the values for ...
19
votes
2answers
329 views

The dangers of SaveDefinitions — should this really happen?

I have been bitten hard by SaveDefinitions -> True. I'll describe in detail what happened below. My questions are: Is this a bug? What is the most convenient ...
1
vote
1answer
183 views

Print indexed (by subscript) variable name systematically

I have some variables : Subscript[S, 1], Subscript[S, 2]...etc. But they are defined in a for loop like For[g = 1, g < 4, g++, Subscript[S, g] = g]; Later, I ...
13
votes
3answers
349 views

Variable scope similar to other languages

Mathematica, unlike many other languages, defines the variables in the global scope unless it is explicitly asked to define a variable in a local scope, e.g., using ...
2
votes
1answer
112 views

Reuse local variable in declaration within With [duplicate]

Considering the richness of Mathematica.SE, I am quite surprised I could not find my answer... I am very sorry if it is a duplicate. Consider the following example: ...
0
votes
1answer
100 views

Arbitrary number of variables in Module, Block, etc

How do I specify an arbitrary number of variable names and initialize them inside of a Module or Block? For example, I want to do something like: ...
0
votes
0answers
121 views

How to define a variable as a function of another variable?

I want to define a variable q which is a function of t. And I want to define another variable ...
-2
votes
2answers
196 views

Mathematica not assigning variable correctly

So I have been fiddling with this the last hour and I just can't get it to work. I have 3 matrices of the same size which contain nothing but 0's in a list, I also have 3 matrices of the same size ...
3
votes
3answers
179 views

Renaming a list with name obtained interactively

I have a list say: m = {1, 2, 3} The list is inside a package, and is the result of a calculation. I want to give the user the option to rename it, for example ...
7
votes
1answer
151 views

Specifying optional arguments with variables

Perhaps this is poor programming on my part, but I would like to create a function that uses a variable as an optional parameter: var = 2; f[a_:var] := {a} The ...
1
vote
0answers
129 views

Variable scope in nested NMaximize, NMaxValue

I'm trying to use NMaxValue inside other statements (FindRoot and NMinimize). Those statements should bind variable names, but I'm getting weird warnings which seem to indicate that the variables ...
1
vote
3answers
366 views

Adding subscripts to variables

I know I can define vars = Map[Subscript[x, #1] &, {"tem", "pressure", "hum", "lat", "alt", "space", "corner"}] and ...
8
votes
1answer
122 views

Is it possible to use the global definition of a symbol as part of its new local definition set in a Block?

Consider the following expression: Block[{Print = CopyToClipboard}, doSomething[]] Now all calls to Print from ...