numeric approximation



On Feb. 7, 2012, I wrote
-----------------------------------------
>(%i1) load(nint);
>(%o1) "c:/work2/nint.mac"

>(%i2) g : 1/(sin(x)^2 + log(x))$

>(%i3) f(t) := nint(g,x,1,t)$

>(%i4) f(5);

>(%o4) 2.729767710374242

>(%i5) goutL;

>(%o5) [[qag,2.729767710374242,3.3769724748722686E-12,93,0]]
>--------------------------------
>then one can make a table, for example:

>(%i6) ntable (func, x0, dx, xf):= 
>block([nL, fL, nfL, jj, ii],
>   nL : makelist (zz, zz, x0, xf, dx),
>   fL : map ('func, nL),
>   nfL : makelist ( ["  ", nL[jj], "  ", fL[jj] ], jj, length(nL)),
>   for ii thru length (nfL) do apply ('print, nfL[ii]))$

>(%i7) ntable ( 'f, 2, 2, 10)$

>   2    0.80395247146645 
>   4    2.307860373006648 
>   6    3.188145387618254 
>   8    4.049252091167718 
>   10    4.843537149824256 

>(%i8) time(%);

>(%o8) [1.64]
--------------------------------------
Of course it is much easier (but slightly slower)
to use print with a do loop:
------------------------------------------
(%i20) for j:2 step 2 thru 10 do
            print("  ",j,"  ",f(j))$

   2    0.80395247146645 
   4    2.307860373006648 
   6    3.188145387618254 
   8    4.049252091167718 
   10    4.843537149824256 

(%i21) time(%);
(%o21) [2.11]
---------------------------------
and much easier to define a table function
in terms of the do loop:
----------------------------------------
(%i22) ftable(func,x0,xf,dx) := 
block([jj],
  for jj:x0 step dx thru xf do
       print("  ",jj,"   ",func(jj)))$

(%i23) ftable(f,2,10,2)$
   2     0.80395247146645 
   4     2.307860373006648 
   6     3.188145387618254 
   8     4.049252091167718 
   10     4.843537149824256 

(%i24) time(%);
(%o24) [2.71]
--------------------------

Ted Woollett