global vars in function defined with block statement used for rk()
Subject: global vars in function defined with block statement used for rk()
From: Stavros Macrakis
Date: Tue, 17 Sep 2013 18:31:49 -0400
Actually, to speak more precisely, the *expression *f(x) is evaluated
exactly once, which entails calling the *function* f on the argument x.
The returned expression 1+cos(x) is what rk works with.
This is the way most Maxima functions work. To see this, try redefining f
as follows:
f(x) := (count:count+1, sin(x))
and run plot2d(f(x),[x,0,1]). You will see that f(x) is called just once.
There is one exception I can think of, which is the 'sum' operator:
sum(f(x),x,1,3) calls f(x) three times. In effect, 'sum' is a shorthand for
a loop. Whether this exception is a good idea or not has been debated on
this mailing list and I'm not going to restart that discussion here....
-s
On Tue, Sep 17, 2013 at 6:04 PM, Stavros Macrakis <macrakis at alum.mit.edu>wrote:
> On Tue, Sep 17, 2013 at 5:05 PM, Ether Jones <maxima at etherjones.us> wrote:
>
>> ...
>> Why is the value of the global variable "count" equal to 1 after the rk
>> integration?
>> I would think the function would be called many times.
>>
>
> The function f(x) is called exactly once, and returns 1+cos(x). I think
> what you want is
>
> rk( '(f(x)), x, 0, [t, 0, 1, 0.1] )
>
>> .
>> ..
>> I couldn't find how to define local static variables in the block
>> statement, but the manual said that globals could be used.
>>
>
> Correct.
>
> -s
>
>