Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Mathematica] Trouble understanding subscripted variables

  1. Mar 5, 2012 #1

    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 (Text):

    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,
  2. jcsd
  3. Mar 5, 2012 #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.
  4. Mar 6, 2012 #3


    User Avatar
    Gold Member

    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, " =. "
  5. Mar 6, 2012 #4
    Ok. I think I should use


    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: Mar 6, 2012
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook