Re: Using FindRoot with free parameters
- To: mathgroup at smc.vnet.net
- Subject: [mg132590] Re: Using FindRoot with free parameters
- From: steviep <ssplotkin at gmail.com>
- Date: Mon, 14 Apr 2014 23:00:59 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- Delivered-to: l-mathgroup@wolfram.com
- Delivered-to: mathgroup-outx@smc.vnet.net
- Delivered-to: mathgroup-newsendx@smc.vnet.net
> > f[n_, x_] = Exp[-3 x^2 + 2] BesselI[n, 4 x]; > > > xf[alpha_?NumericQ, n_?NumericQ] := > Module[{x}, x /. > FindRoot[f[n, x] - alpha, {x, 2}][[1]]] > > > xf[3, 0] > > > 1.11426 > > > xf[alpha, n] /. {alpha -> 3, n -> 0} > > > 1.11426 > > > > Bob Hanlon > > > > > On Sat, Apr 12, 2014 at 5:15 AM, steviep2 > <ssplotkin at gmail.com> wrote: > > > Hi, > > I want to define a function of 2 parameters that uses FindRoot. I.e. I > > have a known but complicated function f[n_,x_] = "complicated function of > > (n,x)". I want to find the value of x where f[n,x] == alpha, and I want to > > call this a function xf[alpha_,n_]. > > > > So my attempts (this is non-working code) looks something like this: > > > > xf[alpha_, n_] = Function[{x}, x /. FindRoot[f[n,x] - alpha, {x, 2}]] > > > > or > > xf[alpha_, n_] = Function[x /. FindRoot[f[n, x] - alpha, {x, 2}]][alpha, > > n] > > > > It seems this is a pretty simple question-- basically using FindRoot but > > holding off on substituting in the parameters until later. Is there a > > simple solution? Thanks, I had actually come up this solution in the meantime: xf[alpha_, n_] = Function[{alpha, n}, x /. FindRoot[f[n, x] - alpha, {x, 2}]][alpha,n] which gives the same answer. It also gives a bunch of warnings, as does Module when "=" is used instead of ":=". Is one method preferred over another? Thanks in any event. -S