Using Mathematica » Basics
Before we can really begin working with Mathematica we also need to discuss the function
Clear , which can be a life-saver.
It just removes any definitions we've given to a symbol, which can be a bigger deal than one might think. Consider the following case:
Notice we get an error because the way Mathematica reads this, we're trying to assign a function to the number 1000.
Here, though, all we need to do is use
Clear and we're good to go.
Sometimes we've made a lot of definitions and we want to
Clear them all, just to prevent hard to find errors from cropping up. In this case we can do the following:
What this does is removes all the definitions for any symbol that looks like
Global`symbolName which is usually every symbol we've defined.
Sometimes we want to protect some symbols, like fundamental constants, from being erased. In this case we just need to make a symbol that doesn't look like
Global`symbolName . A useful way to do this is to put the type of thing it is before it:
Constant`PlanckConstant = 6.626*10-34 (*J*s*);
This sort of thing is discussed more at length in the Contexts section, but first let's just see that this does what we want it to. First let's define a bunch of things:
a=100; b=2.50; c[x_]=RandomReal*x; x[x_]:=x[x]; a b c x
And now we'll clear them:
Clear["Global`*"] a b c x
But our constant is fine:
And for those who don't want to type out a long name every time they use this constant in a problem, here's a trick: use some variable just for the problem to which you assign the value of the constant. e.g:
h=Constant`PlanckConstant; c=Constant`SpeedOfLight; λ=1*10^-3 (*m*); h*c/λ (*J*)
Then just clear all of this before starting the next problem:
And now none of that can leak into the next problem:
h c λ