On Mon, 2008-04-14 at 19:10 +0200, Adam Majewski wrote:
> Hi
>
> I have a batch file :
>
> ***** start code *****
> zMin:0.0;
> zMax:3.0;
> iMax:10;
> ER:2.0;
> /* */
> LastIteration(z,ER,iMax):=
> block
> (
> i:0,
> loop,
> z:z*z,
> i:i+1,
> if ((z<ER) and(i<iMax)) then go(loop),
> return(i)
> );
> /* */
> plot2d([z,LastIteration(z,ER,iMax)],[z,zMin,zMax],[y,0,11]);
>
> ********** end code *****************************
>
> For values 0<z<=1 function should give iMax.
> For z>1 it should give integer values < iMax.
> Like : http://commons.wikimedia.org/wiki/Image:Data.png
> But it does not.
> When I remove codition (z<ER) function gives iMax for all values, so i
> is incremented properly.
> I have read thread : "Conditional inside a block not working"
> and doc but I still can't find the error in my code
The problem is in the last line. The arguments to plot2d are evaluated,
so "LastIteration(z,ER,iMax)" is evaluated at the time of the call. I
don't understand the details of what happens, but a fix is to pass the
function name, not a call to the function. Also I think plot2d wants
the function to take one argument, not three. I fixed it this way so
your function "LastIteration" could be used without change; it seems to
work:
(%i38) plotter(z) := LastIteration(z,ER,iMax);
(%o38) plotter(z) := LastIteration(z, ER, iMax)
(%i39) plot2d([z,plotter], [z,zMin,zMax],[y,0,11]);
(%o39)
At %i38 I define the plotter function which will be passed to plot2d; it
takes one argument, "z", and binds the two parameters "ER" and "iMax".
At %i39 I call "plot2d" with just the name of the plotter function.
"plot2d" will call "plotter" as it needs to, giving it values for its
one argument.
Interestingly, the results of calling "plot2d" gave a hint as to the
problem. It appears that "plot2d" wants to put the name of the
functions it is passed in the upper right corner as labels for the
colored lines. When the original code is run the name "1" appears; when
my fix is used the name "plotter" appears instead. That was my clue
that unwanted evaluating was causing the erroneous plot.
-- Bill Wood