Re: rootsearch in a piecewise function

*To*: mathgroup at smc.vnet.net*Subject*: [mg60257] Re: rootsearch in a piecewise function*From*: Peter Pein <petsie at dordos.net>*Date*: Thu, 8 Sep 2005 06:47:52 -0400 (EDT)*References*: <dfovrd$feo$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

juejung schrieb: > hi group, > > why does the root search in the following piecewise function not work, > when the plot function before evaluates just fine. > if i use the /.para already at the point where i define f2 and f3 then the > root command works. however, the actual functions f2 and f3 are much > longer and i would like to replace parametervalues only at the point where > i do in the example below. it seems that findroot is evaluated before the > replacement takes place?? > > thanks > juergen > > para = {a -> 3, b -> 4, c -> 20, d -> 1.5}; > f2[x_] := 10 + a* x^(1/2) - x - b; > f3[x_] := -6 + c/x + d^2; > f1[x_] := Which[0 < x < b, f2[x], b <= x < 10, f3[x],10 <= x, x^(1/2)] > /.para; > df1[x_] = D[f1[x], x]; > > Plot[f1[x] /.para, {x, 0.1, 15}] > Plot[df1[x] /.para, {x,0.1, 15}] > > FindRoot[{f1[x] == 0} /.para, {x, 2}] > FindRoot[{df1[x] == 0} /.para, {x, 2}] > Hi Juergen, you'll have to map Evaluate at f1[x], because Which has Attribute HoldAll. This means, without Evaluate, f1[x] contains only calls to f2 and f3. And the expression f2[x] does not contain any of your parameters. Compare: f1[x] Which[0 < x < 4, f2[x], 4 <= x < 10], f3[x], 10 <= x, Sqrt[x]] with Evaluate /@ f1[x] Which[0 < x < 4, 10 - b + a*Sqrt[x] - x, 4 <= x < 10], -6 + d^2 + c/x, 10 <= x, Sqrt[x]] FindRoot[Evaluate /@ f1[x]==0 /. para, {x, 5}] will find the root at 16/3. (The start value 2 leads the algorithm towards negative x-values, where f is not defined). and FindRoot[Evaluate /@ df1[x]==0 /. para, {x, 2}] gives you the location of the maximum at 9/4. -- Peter Pein, Berlin GnuPG Key ID: 0xA34C5A82 http://people.freenet.de/Peter_Berlin/

**Follow-Ups**:**Re: Re: rootsearch in a piecewise function***From:*Chris Chiasson <chris.chiasson@gmail.com>