Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Package Problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg93930] Re: Package Problem
  • From: "m.g." <mg at michaelgamer.de>
  • Date: Fri, 28 Nov 2008 07:11:51 -0500 (EST)
  • References: <ggjf00$lsg$1@smc.vnet.net> <gglt56$8ne$1@smc.vnet.net>

On 27 Nov., 11:35, Albert Retey <a... at gmx-topmail.de> wrote:
> Hi,
>
>
>
> >   I have the following problem. I made a short Module to calculate th=
e
> > zeros of a given Function.I did this simply in the following way:
>
> > Nullstellen[f_, intervall_List] :=
> >  Module[{func, startwerte, werte, vzwechsel, liste,
> >    a = intervall[[1]], b = intervall[[2]]},
> >   func = Function[x, Evaluate[f]];
> >   werte = Table[x, {x, a, b, Abs[b - a]/100.}]; (* start-values *)
> >   liste = ({#, func[x] /. x -> #} ) & /@ werte;
> >   vzwechsel =    Select[Split[liste, (Sign[Last[#1]] == -Si=
gn[Last
> > [#2]]) &],  Length[#] == 2 &]; (* Sign Change*)
> >   startwerte = Map[First, vzwechsel, {2}];
> >   Map[FindRoot[func[x] == 0, {x, #[[1]], #[[2]]}] &, startwerte]
> >   ]
>
> > This works fine, but when I copy exactly this code in my own
> > package... it works not. I get an output like this:
>
> > Nullstellen[x^2 - 2, {-2, 2}]
>
> > {{-2.,-2+x^2},{-1.8,-2+x^2},{-1.6,-2+x^2},{-1.4,-2+x^2},{-1.2,-2+x^2},
> > \
> > {-1.,-2+x^2},{-0.8,-2+x^2},{-0.6,-2+x^2},{-0.4,-2+x^2},{-0.2,-2+x^2},
> > {\
> > 1.11022*10^-16,-2+x^2},{0.2,-2+x^2},{0.4,-2+x^2},{0.6,-2+x^2},{0.8,-2+
> > \
> > x^2},{1.,-2+x^2},{1.2,-2+x^2},{1.4,-2+x^2},{1.6,-2+x^2},{1.8,-2+x^2},
> > {\
> > 2.,-2+x^2}}
>
> > So the mapping of the function onto the values does not work.
>
> > Can anyone explain the reason (and give me a hint how I can manage
> > this function to work within a package).
>
> I have not tested it but am quite sure that the problem is that the x
> within your package is created in the private context of your package,
> while the  x you use in the function call is in the Global` context,
> that is they are different symbols. There are various ways to solve the
> problem, but the simplest and also the one which is following the
> conventions of the built in functions (compare Integrate or Plot) is to
> pass the symbol as an additional argument, e.g. define:
>
> Nullstellen[expr_, {x_Symbol, a_, b_}] := Module[{
>    func, startwerte, werte, vzwechsel, liste
>    },
>   werte = Table[x, {x, a, b, Abs[b - a]/100.}];(*start-values*)
>   liste = ({#, expr /. x -> #}) & /@ werte;
>   vzwechsel = Select[
>     Split[liste, (Sign[Last[#1]] == -Sign[Last[#2]]) &],
>     Length[#] == 2 &
>     ];(*Sign Change*)
>   startwerte = Map[First, vzwechsel, {2}];
>   Map[FindRoot[expr == 0, {x, #[[1]], #[[2]]}] &, startwerte]
>   ]
>
> hth,
>
> albert (from cold but sunny Austria :-)
>
> > Mike from cold an rainy Germany

Thank you Alfred, with the hints of David and you I saw it
immidiately. I now realized it according to your suggestions and it
works fine - AND folllows the conventions of Mathematica standard
functions :-)

Greetings (now also from cold and sunny Germany )

Mike


  • Prev by Date: Re: How to hold a form and convert it to a string
  • Next by Date: Re: Floating-Point Computing
  • Previous by thread: Re: Package Problem
  • Next by thread: In V7, is it possible to parallelize DensityPlot?