Re: Mathematica daily WTF
- To: mathgroup at smc.vnet.net
- Subject: [mg115175] Re: Mathematica daily WTF
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Mon, 3 Jan 2011 03:58:14 -0500 (EST)
I'd argue, rather, that little minds often have no choice but to take little steps, while big minds often prefer to take big steps. As a familiar mathematical example, take the Newton-Raphson method. Coding this could be an early exercise with Mathematica. (Of course you can just use FindRoot with the Method -> "Newton" option, but the purpose of the exercise here is to "roll one's own".) The "little-step" approach might be something like this: f[x_] := x^6 - x - 1 x0 = 1.; Module[{i, x}, x[0] = x0; For[i = 0, i < 5, x[i + 1] = x[i] - f[x[i]]/f'[x[i]]; Print[x[i]]; i++] ] The "big-step" approach might be: f[x_] := x^6 - x - 1 newtonStep[x_] := x - f[x]/f'[x] NestList[newtonStep, 1., 4] And I'd maintain that the latter not only is shorter to type and quicker to code (if you know the constructs involved), but much more directly expresses what's going on with the Newton-Raphson method. The "little-step" approach is definitely easier for those who either have not reached learning about NestList, e.g.; or who think it's fun "playing computer" instead of solving problems; or whose minds have been rotted by previous programming with languages where you were forced to set up a loop explicitly -- including specifying a counter and explicitly incrementing that counter. Moreover, if you want to generalize the code so as to vary the target function and the initial guess, it's a lot simpler to modify the "big-step" code to do so: Clear[newtonStep] newtonStep[f_, x_]:= x - f[x]/f'[x] newton[f_, x0_, n_] := NestList[newtonStep[f, #] &, x0, n] newton[#^6 - # - 1&, -1., 4] newton[#^2 - 2 &, 1., 5] // NumberForm[#, 12] & (Of course one might eventually recode either approach so as to calculate the derivative function just once.) None of this is to make any claim whatsoever as to which approach -- explicitly programming the iteration yourself in detail, or letting Mathematica handle all the details behind the scenes -- might ultimately be more efficient. (Although the whole point of the Newton-Raphson method is to get good approximations with few iterations.) Nor is this to deny that there are times when you may want or need to program a loop explicitly, e.g., when a number of arrays' values are involved. Nor, finally, is what I've said meant to discourage those who come to Mathematica from some other programming-paradigm background and who want to just get on with their work without learning anything essentially new. Although such folks might save themselves a lot of time ultimately by learning and exploiting the higher-order constructs available in Mathematica. On 1/2/2011 4:55 AM, AES wrote: > ... > I'd argue [procedural programming ] is also extremely helpful to people who _think_ physically, > or if you like procedurally, and who are primarily focused on solving > problems that have an inherently procedural character. > > The successive steps (lines, cells, expressions) in a procedural program > will very often state or mimic or reproduce what happens as a function > of time in a dynamic system, or as a function of distance as a wave > propagates, or mimic the flow of control in a complex system, or . . . > > As such, they simplify the process of _coding_ these programs; they > _document_ and make readable what the program is doing, step by step; > they make it easy to _insert later refinements_ inside the procedure > (e.g., tests for current values or for exceptional cases at points > within the procedure).... -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305