MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Coding for Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg100597] Re: [mg100555] Coding for Mathematica
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Tue, 9 Jun 2009 03:53:31 -0400 (EDT)
  • Organization: Mathematics & Statistics, Univ. of Mass./Amherst
  • References: <200906080604.CAA19997@smc.vnet.net>
  • Reply-to: murray at math.umass.edu

One could write an essay about Mathematica programming styles based upon 
the example of Newton's Method. (Indeed, I've done essentially that in 
one conference paper and in some materials for classes in years past.)

Please, no offense intended in saying this: your programming style does 
betray somebody coming to Mathematica with, perhaps, too much exposure 
to classical programming languages where the only option was to take a 
one-step-at-a-time procedural approach.

In the example at hand, there's no reason to manufacture a loop 
explicitly as you did. Instead, you could do the following, which 
exploits Mathematica's NestList functional programming approach:

   f[x_] := x^2 - 2
   g[x_] = x - f[x]/f'[x];
   iterates = NestList[g, 1., 9];
   formatNumber[t_]:=NumberForm[t,10]
   Transpose[{Range[10], formatNumber /@ iterates}] // TableForm

This may look longer than what you have below, but I purposely set it up 
so that it could readily be adapted so as to make a function that does 
the whole thing as a "chunk".  And so as to separate out the formatting 
aspect from the computational aspect.

(I didn't include anything about plotting the original function f, since 
that does not seem directly related to what you asked.)

One thing in your procedural code is not going to do what you expect, 
namely, to display each iterate to 10 decimal digits. Instead of 
N[#,10], which still by default displays to only 6 digits, you need 
NumberForm[#,10], which changes the number of displayed digits to what 
the 2nd argument specifies.

Actually, Newton's method converges so rapidly that you won't see any 
difference in the iterates after the first 5, even if you asked to 
display 15 or 16 decimal digits.

Now to answer what you explicitly asked: Yes, the result of evaluating 
each of the Input cells In[3] through In[5] will be to print a 
two-column table with second column the successive iterates.

FAQneeds wrote:
> Im a first time user of Mathematica. So im trying to put in a command 
> related to Newton's Method. I would just like to know if once im done 
> typing this command in am i suppose to see the results immediately 
> or do i have go to something in the program to see the results of my 
> command. Here is the Code please can anyone tell me what im doing wrong
> i will be really grateful:
> 
> In[1] :=(*your name,06/06/09*)
> In[2] :=(*Newton's Method*)
> In[3] := f[x_] := x^2 - 2
> In[4] := x[1] = 1.0
> In[5] := For[n = 1, n < 10, n++,
>   {x[n + 1] = x[n] - f[x[n]]/f'[x[n]];
>    Print[n + 1, "  ", N[x[n + 1], 10]];}]
> In[6] := Plot[f[x], {x, 0, 2}]
> 

-- 
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


  • Prev by Date: Re: Future for Mathematica on Solaris
  • Next by Date: Re: Multi-level Menu (ActionMenu)
  • Previous by thread: Re: Coding for Mathematica
  • Next by thread: Re: Coding for Mathematica