Re: How do I assign the solution obtained by FindRoot to a variable?

*To*: mathgroup at smc.vnet.net*Subject*: [mg128480] Re: How do I assign the solution obtained by FindRoot to a variable?*From*: Murray Eisenberg <murray at math.umass.edu>*Date*: Wed, 24 Oct 2012 03:31:07 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-newout@smc.vnet.net*Delivered-to*: mathgroup-newsend@smc.vnet.net*References*: <20121023045437.AE4F2685C@smc.vnet.net>

On Oct 23, 2012, at 12:54 AM, NS Lin <nansheng.lin at gmail.com> wrote: > I would like to store the root/solution obtained by the FindRoot[] function to a variable. How can I do that? > This is a faq, but usually Mathematica novices ask it about Solve. The situation is similar. soln = x /. FindRoot[Cos[x] - x, {x, 0.5}] That works because FindRoot returns the one-element list {x -> 0.739085}. In the case of Solve, you may obtain more than one solution, e.g.: Solve[x^2 - x == 0, x] {{x -> 0}, {x -> 1}} In that situation, you'll need to pick out which solution you want, e.g.: x /. Solve[x^3 - x == 0, x][[2]] Notice that the result of Solve is a list of one-element lists. This is so even when the equation has only a single solution, e.g.: Solve[3 x == 1, x] {{x -> 1/3}} In that case you can either use indexing to form Solve[3 x == 1, x][[1]] before using "x /. ", or just use First: x /. First@Solve[3 x == 1, x] The situation is quite similar for DSolve. --- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2838 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305

**References**:**How do I assign the solution obtained by FindRoot to a variable?***From:*NS Lin <nansheng.lin@gmail.com>