Hi,
Thx for answer. It's great.
I have improved the code and the result is :
******** start code ***********
/* batch file = script for Maxima */
/* global variables */
xMin:0.0;
xMax:3.0;
iMax:10;
ER:2.0;
/* definition of function */
LastIteration(x):=
block
(
i:0,
if (x>ER) then return(i),
loop,
x:x*x,
i:i+1,
if ((x<ER) and (i<iMax)) then go(loop),
return(i)
);
/* drawing */
my_preamble: "set xzeroaxis; set arrow 1 from 2,4 to 2,2 lw 3 lt 1; set
arrow 2 from 1,1 to 1,3 lw 3 lt 1; set ytics (0,1,2,3,4,5,'iMax' 10);
set xtics (0,1,'ER' 2,3); set title 'Last iteration for X(n+1)=X(n)*X(n)
for iMax=10 ER=2'";
/* pass to plot2d the function name ( for example f ), not a call to the
function, for example f(x) */
plot2d(LastIteration,[x,xMin,xMax],[y,-1,iMax+1],[ylabel, "Last
Iteration"],[gnuplot_preamble, my_preamble]);
***** end code *************
It works as I expected.
For me it is the most important thing in Maxima. Simple code,
which looks like pseudocode but works.
Thx
Adam
> 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