Subject: Best way to use a compiled function in findroot
From: Stavros Macrakis
Date: Wed, 1 Sep 2010 12:44:54 -0400
It is not surprising that
find_root(f(x)-y,x,-1,1);
has trouble. This means: find the root of the expression returned by f(x)-y
with respect to the variable returned by the expression x. Since f is
defined to be a function of a floating-point argument, the unevaluated
symbol x is not a legal argument. And you certainly don't want to find the
root with respect to some variable *named by* x, but the variable x itself.
What perhaps makes this difficult is that Maxima -- as a convenience
feature -- "auto-quotes" symbols which don't have variables, so in many
contexts you can get away with saying sin(x) when you really mean '(sin(x)).
So it seems that what you really want is: find the root of the expression
f(x) itself -- i.e. '(f(x)) -- with respect to the constant variable x --
i.e. '(x).
Thus:
g(y):=find_root('(f(x))-y,'x,-1,1)$
f(x):=block(mode_declare(x,float),sin(x))$
compile(f)$
g(.5) => .5235
sin(g(.5)) => .5
Tested on Maxima 5.20.1 GCL 2.6.8 Windows XP.
-s
On Wed, Sep 1, 2010 at 07:32, Leo Butler <l.butler at ed.ac.uk> wrote:
>
>
> On Wed, 1 Sep 2010, Sergio Callegari wrote:
>
> < On 31/08/2010 18:31, Robert Dodier wrote:
> < > On Tue, Aug 31, 2010 at 10:07 AM, Sergio Callegari
> < > <sergio.callegari at gmail.com> wrote:
> < >
> < >
> < > > (%i5) g(y):=block([x:gensym()],findroot('f(x)-y,x,-1,1));
> < > > (%o5) g(y) := block([x : gensym()], findroot('f(x) - y, x, - 1,
> 1))
> < > >
> < > > I get:
> < > >
> < > > (%i6) g(0);
> < > > (%o6) findroot(f(g900), g900, - 1, 1)
> < > >
> < > There is no findroot function. There is a function find_root, however.
> < >
> < >
> < > > g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, - 1,
> 1))
> < > >
> < > > g(0) hangs forever...
> < > >
> < > Did you omit the final semicolon?\
> < >
> < I wonder if I'll ever get used to the difference between wxmaxima
> (impossible
> < to cut and paste on the ml, but nice autosemicolon with shift+enter) and
> the
> < text mode maxima!
> <
> < In any case, it still does not work
> <
> < g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, - 1,
> 1));
> < (%o3) g(y) := block([x : gensym()], findroot(lambda([x], f(x) - y), x, -
> 1,
> < 1))
> < (%i4) g(0);
> < (%o4) findroot(lambda([x], f(x) - y), g899, - 1, 1)
>
> How about:
>
> (%i2) g(y) := block([x : ?gensym()], find_root(lambda([x], f(x) - y), x, -
> 1, 1))$
>
> (%i3) f(x) := block(mode_declare(x,float),sin(x)); compile(f);
>
> (%o3) f(x):=block(mode_declare(x,float),sin(x))
>
> ; In: LAMBDA ($X)
>
> ; (SIN $X)
> ; Note: Unable to avoid inline argument range check
> ; because the argument range (DOUBLE-FLOAT) was not within 2^64
> ;
>
> ; Compilation unit finished.
> ; 1 note
>
> (%o4) [f]
> (%i5) g(0.5);
>
> (%o5) .5235987755982989
> (%i6) f(%);
>
> (%o6) 0.5
>
> Leo
>
> --
> The University of Edinburgh is a charitable body, registered in
> Scotland, with registration number SC005336.
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>