[Mathematica] Trouble understanding subscripted variables

In summary, the programmer was looking for a function to unset a variable, and found it in the Mathematica help system.
  • #1
jackmell
1,807
54
Hi,

I don't understand how to remove the values for these subscripted variables once I have assigned a number to them. If I try to remove the value from a_1 below, I get an error message:

Code:
Remove[a]
Subscript[a, 1] = 1; 
myf = z^3 + z*w^2
myf /. z -> Subscript[a, 1]
myf /. z -> Subscript[a, 2]
Clear[Subscript[a, 1]]

So substituting a_1 and a_2 into myf does what it's suppose to do. In the first case, the value of a_1 is substituted into myf and in the second, the variable name a_2 is substituted. Now, suppose I want to remove the value from a_1 so that when I next try to substitute into myf, the variable name a_1 is used and not the value of a_1. If I try to use Clear[a_1] or Remove[a_1] it tells me that's not a variable name and if I use Remove[a], then all the values of a are removed including the ones I want to keep.

Thanks guys,
Jack
 
Physics news on Phys.org
  • #2
In[1]:= Subscript[a,1]=1; p*q+p/.p->Subscript[a,1]
Out[2]= 1+q

In[3]:= Subscript[a,1]=.; p*q+p/.p->Subscript[a,1]
Out[4]= \!\(a\_1 + q\ a\_1\)

Personal opinion: Trying to use subscripts is probably going to cause you more grief than it is worth. Your post is just the first example you will run into. The darker and darker the corners of Mathematica you start poking into the more you will find. I hope it works out for you.
 
  • #3
Wow, I love learning something new. The function you're looking for, and is used above, is UNSET.

The shortcut for it is, as above, " =. "
 
  • #4
Ok. I think I should use

Quiet[a_n=.];

That way, with the Quiet and the semicolon, no output is generated if the variable name already has been unset.

Wow! I am already saving so much time now with just this little change to my code. You guys are tops. You too Bill. :)
 
Last edited:
  • #5


Hi Jack,

Thank you for reaching out. It seems like you are trying to manipulate subscripted variables in Mathematica. Subscripted variables are useful for representing mathematical expressions in a more readable format, but they can also be a bit tricky to work with.

To remove the value of a subscripted variable, you can use the function Subscript[a, 1] =. This will set the value of a_1 to be empty or undefined. So for example, if you want to remove the value of a_1 in your code, you can use Subscript[a, 1] =.

Alternatively, you can also use the function Clear[Subscript[a, 1]] to clear the value of a_1. This function specifically targets subscripted variables, so it will not remove the values of other variables in your code.

I hope this helps clarify how to manipulate subscripted variables in Mathematica. If you continue to have trouble, feel free to reach out for further assistance. Keep up the good work as a scientist!

Best,
 

1. What does a subscripted variable mean in Mathematica?

A subscripted variable in Mathematica is a way of denoting a specific element or component within a larger expression or function. It is typically used to indicate the position or index of a particular element in a list, matrix, or other data structure.

2. How do I create a subscripted variable in Mathematica?

To create a subscripted variable in Mathematica, you can use the Subscript function. For example, Subscript[x, 1] represents the first element of a list or vector named x.

3. Can I use subscripted variables in equations and functions?

Yes, subscripted variables can be used in equations and functions just like regular variables. However, it is important to be aware of the specific rules and conventions for using them in Mathematica.

4. How can I access the values of subscripted variables in Mathematica?

To access the values of subscripted variables, you can use the Part function. For example, if x is a list, then x[[1]] represents the value of the first element, x[[2]] represents the value of the second element, and so on.

5. Are there any common mistakes to avoid when using subscripted variables in Mathematica?

One common mistake is using a single equals sign (=) instead of a double equals sign (==) when defining a subscripted variable. This will result in an assignment rather than an equality check. It is also important to make sure that the subscripts used are valid and do not conflict with any existing variables or functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
548
  • Calculus and Beyond Homework Help
Replies
21
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
261
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • Advanced Physics Homework Help
Replies
5
Views
1K
  • Thermodynamics
Replies
7
Views
808
  • Set Theory, Logic, Probability, Statistics
2
Replies
62
Views
3K
  • Linear and Abstract Algebra
Replies
1
Views
757
Back
Top