Re: Re: rootsearch in a piecewise function
- To: mathgroup at smc.vnet.net
- Subject: [mg60272] Re: [mg60257] Re: rootsearch in a piecewise function
- From: Chris Chiasson <chris.chiasson at gmail.com>
- Date: Fri, 9 Sep 2005 04:07:14 -0400 (EDT)
- References: <dfovrd$feo$1@smc.vnet.net> <200509081047.GAA19136@smc.vnet.net>
- Reply-to: chris.chiasson at gmail.com
- Sender: owner-wri-mathgroup at wolfram.com
juejung, You appear to be unaware of the Hold attribute of some of the functions you are using. Also, you may be neglecting the fact that your function was only defined for x>0. By the way, there isn't much distinction between "parameters" and "variables" in Mathematica. I made some changes to your code. It now produces the results I think you want: reps[1]={a\[Rule]3,b\[Rule]4,c\[Rule]20,d\[Rule]1.5} expr[3]=-6+c/x+d^2 expr[2]=10+a*x^(1/2)-x-b expr[1]=Which[0<x<b,Evaluate[expr[2]],b\[LessEqual]x<10,Evaluate[expr[3]], 10\[LessEqual]x,x^(1/2)] Block[Evaluate[reps[1][[All,1]]],Set@@@reps[1];Plot[expr[1],{x,0,15}]; FindRoot[Evaluate[expr[1]],{x,4,0,\[Infinity]}]] Block[Evaluate[reps[1][[All,1]]],Set@@@reps[1]; Plot[Evaluate[D[expr[1],x]],{x,0,15}]; FindRoot[Evaluate[D[expr[1],x]],{x,2,0,\[Infinity]}]] On 9/8/05, Peter Pein <petsie at dordos.net> wrote: > 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/ > > -- Chris Chiasson http://chrischiasson.com/ 1 (810) 265-3161
- References:
- Re: rootsearch in a piecewise function
- From: Peter Pein <petsie@dordos.net>
- Re: rootsearch in a piecewise function