Physics 20 Reading List #2
For some of you, last week marked your first encounter with
Mathematica. I think you have gotten a good feeling for the power of
this tool, but you might also have been puzzled by its sometimes
inconsistent or counterintuitive behavior, and you might have
wondered what is the best way to achieve certain things.
In particular, see below for some more hints about the homework
assignment due on Friday 10/7/2005.
~~~
Some confusion can easily arise from the fact that Mathematica
allows several different programming styles.
Your first short reading is from a section of
the Mathematica manual describing these styles.
At the end this page, Wolfram technical writers show you how to define
the factorial function in twelve different ways (although some of them
are clearly the same way repeated with slightly different syntax).
What power! What confusion!
You may have noticed that functional programming tends to be very
effective in Mathematica. This method organizes tasks around the
definition and application of expressions (i.e., functions) rather
than the execution of commands. It discourages side effects (as the
assignment of permanent values to variables) and encourages the reuse
and composition of functions. (Optional) If you would like to foray
into the realm of more advanced functional programming, take a look at
this section of the Mathematica manual.
~~~
Your second reading: a 1993 interview of Stephen Wolfram, Mathematica's creator. He recounts why the software was created, and how it gained widespread acceptance in the industrial and educational community by appearing at the time when workstations (as opposed to mainframes) powerful enough to run it were beginning to be available.
Wolfram makes a good point about the blending of mathematics and
computation in Mathematica, allowing a ''gentle start'', and harking
back to the times when computer science was being developed by eminent
mathematicians such as von Neumann and Turing. It is not by chance
that this computational physics laboratory begins with Mathematica.
I also agree with Wolfram when he points out that Mathematica contains a
much broader variety of programming notions and constructs than most
other languages, and therefore can be very useful to teach computation
to beginners. However, the price for this is a lack of
elegance and beauty. In this lab we care about beauty as a defense
from complexity! With Mathematica, it can be very easy to build
byzantine structures that encode very sophisticated mathematics and
numerical analysis, but debugging them can be extremely difficult.
~~~
Speaking of debugging, one thing that is troublesome to deal with in Mathematica is the evaluation (or non-evaluation) of expressions. For example, the difference between = and := is that only the former evaluates the right-hand side. See this site for some examples.
An even more troublesome occurrence is when evaluations occur (or do
not occur) 'automatically' in a way different from what you might expect. You may encounter this problem in the last part of the homework assignment, if you try to use a function like FindRoot[] on a function that requires some of its arguments to be numeric, like NDSolve. Notice that NDSolve[] works only if everything except for the independent variable is numerical, so
NDSolve[{y''[t]==-9.8, y'[0]==0, y[0]==0}, y, {t,0,10}]
will work, but
NDSolve[{y''[t]== g, y'[0]==0, y[0]==0}, y, {t,0,10}]
will not work unless g has been assigned a numerical value.
Why is this a problem? Well, suppose you have a function
and you want to pass this function into FindRoot[], iterating over values of g to find a zero. The problem is that
FindRoot[myfunction[g],{g,-5,-20}]
will try to evaluate myfunction before it tries different values of g, and this evaluation is done with a symbolic value of g, so then NDSolve will fail.
There are at least two solutions to this problem. (If you find more, feel free to post a comment!) The first is to use the Hold[] function, which tells Mathematica not to evaluate the function until later:
FindRoot[Hold[myfunction[g]],{g,-5,-20}]
In this case, myfunction[g] is evaluated only after the FindRoot function plugs in numerical values.
Another (in my opinion, better) solution is to define myfunction[] only for numerical arguments:
In this case the Hold[] in the FindRoot[] function is no longer necessary. The ?NumericQ tells Mathematica that the definition you have typed in is valid only for numeric values of g. (If you had previously defined myfunction[] with a general value of g, the new restrictive definition will be added to the original one, but will not replace it. Then calling myfunction[] with non-numeric g will still use the old definition, resulting in an error from NDSolve[]. You should use Clear[] to fix this).
Here are some more links to learn about the standard Mathematica
evaluation process, and about those functions that have nonstandard evaluation rules.
(Optional) Richard Gaylord has an interesting tutorial on the
Fundamentals of Mathematica Programming. This material is not elementary: check it out if you are interested in the internal organization of Mathematica.
Mathematica. I think you have gotten a good feeling for the power of
this tool, but you might also have been puzzled by its sometimes
inconsistent or counterintuitive behavior, and you might have
wondered what is the best way to achieve certain things.
In particular, see below for some more hints about the homework
assignment due on Friday 10/7/2005.
Some confusion can easily arise from the fact that Mathematica
allows several different programming styles.
Your first short reading is from a section of
the Mathematica manual describing these styles.
At the end this page, Wolfram technical writers show you how to define
the factorial function in twelve different ways (although some of them
are clearly the same way repeated with slightly different syntax).
What power! What confusion!
You may have noticed that functional programming tends to be very
effective in Mathematica. This method organizes tasks around the
definition and application of expressions (i.e., functions) rather
than the execution of commands. It discourages side effects (as the
assignment of permanent values to variables) and encourages the reuse
and composition of functions. (Optional) If you would like to foray
into the realm of more advanced functional programming, take a look at
this section of the Mathematica manual.
Your second reading: a 1993 interview of Stephen Wolfram, Mathematica's creator. He recounts why the software was created, and how it gained widespread acceptance in the industrial and educational community by appearing at the time when workstations (as opposed to mainframes) powerful enough to run it were beginning to be available.
Wolfram makes a good point about the blending of mathematics and
computation in Mathematica, allowing a ''gentle start'', and harking
back to the times when computer science was being developed by eminent
mathematicians such as von Neumann and Turing. It is not by chance
that this computational physics laboratory begins with Mathematica.
I also agree with Wolfram when he points out that Mathematica contains a
much broader variety of programming notions and constructs than most
other languages, and therefore can be very useful to teach computation
to beginners. However, the price for this is a lack of
elegance and beauty. In this lab we care about beauty as a defense
from complexity! With Mathematica, it can be very easy to build
byzantine structures that encode very sophisticated mathematics and
numerical analysis, but debugging them can be extremely difficult.
Speaking of debugging, one thing that is troublesome to deal with in Mathematica is the evaluation (or non-evaluation) of expressions. For example, the difference between = and := is that only the former evaluates the right-hand side. See this site for some examples.
An even more troublesome occurrence is when evaluations occur (or do
not occur) 'automatically' in a way different from what you might expect. You may encounter this problem in the last part of the homework assignment, if you try to use a function like FindRoot[] on a function that requires some of its arguments to be numeric, like NDSolve. Notice that NDSolve[] works only if everything except for the independent variable is numerical, so
NDSolve[{y''[t]==-9.8, y'[0]==0, y[0]==0}, y, {t,0,10}]
will work, but
NDSolve[{y''[t]== g, y'[0]==0, y[0]==0}, y, {t,0,10}]
will not work unless g has been assigned a numerical value.
Why is this a problem? Well, suppose you have a function
myfunction[g_] := ( sol=NDSolve[{y''[t]== g, y'[0]==25, y[0]==0},y,{t,0,10}];
y[5] /. sol[[1]]);
and you want to pass this function into FindRoot[], iterating over values of g to find a zero. The problem is that
FindRoot[myfunction[g],{g,-5,-20}]
will try to evaluate myfunction before it tries different values of g, and this evaluation is done with a symbolic value of g, so then NDSolve will fail.
There are at least two solutions to this problem. (If you find more, feel free to post a comment!) The first is to use the Hold[] function, which tells Mathematica not to evaluate the function until later:
FindRoot[Hold[myfunction[g]],{g,-5,-20}]
In this case, myfunction[g] is evaluated only after the FindRoot function plugs in numerical values.
Another (in my opinion, better) solution is to define myfunction[] only for numerical arguments:
myfunction[g_?NumericQ] := ( sol=NDSolve[{y''[t]== g, y'[0]==25, y[0]==0},y,{t,0,10}];
y[5] /. sol[[1]]);
In this case the Hold[] in the FindRoot[] function is no longer necessary. The ?NumericQ tells Mathematica that the definition you have typed in is valid only for numeric values of g. (If you had previously defined myfunction[] with a general value of g, the new restrictive definition will be added to the original one, but will not replace it. Then calling myfunction[] with non-numeric g will still use the old definition, resulting in an error from NDSolve[]. You should use Clear[] to fix this).
Here are some more links to learn about the standard Mathematica
evaluation process, and about those functions that have nonstandard evaluation rules.
(Optional) Richard Gaylord has an interesting tutorial on the
Fundamentals of Mathematica Programming. This material is not elementary: check it out if you are interested in the internal organization of Mathematica.

0 Comments:
Post a Comment
<< Home