Localized Variables and Switches

Local Variables

In the above, the values of a and  xvalue are used repeatly—however, if we are only interested in the resulting minimum, their final values have little meaning or importance.  Suppose, we use the value of a or xvalue without clearing—what will the value be?

"index_52.gif"

"index_53.gif"

"index_54.gif"

"index_55.gif"

As this could cause problems if a is used in a function call, say Sin[2 π a], it is a good idea to localize variables.

"index_56.gif"

"index_57.gif"

"index_58.gif"

"index_59.gif"

"index_60.gif"

"index_61.gif"

"index_62.gif"

"index_63.gif"

Switches: If, Which

"index_64.gif"

"index_65.gif"

"index_66.gif"

"index_67.gif"

"index_68.gif"

"index_69.gif"

"index_70.gif"

"index_71.gif"

The above program is ok, but not very useful because it only works for  the current value of a.  It would be more useful to have something that worked for any value of a and could use it over again—that is, turn it into a tool.  This involves patterns and function definitions.


Created by Wolfram Mathematica 6.0  (06 September 2007) Valid XHTML 1.1!