Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12164 > unrolled thread

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

Started byMurray Eisenberg <murray@math.umass.edu>
First post2012-10-24 07:43 +0000
Last post2012-10-24 07:43 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: How do I assign the solution obtained by FindRoot to a variable? Murray Eisenberg <murray@math.umass.edu> - 2012-10-24 07:43 +0000

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

FromMurray Eisenberg <murray@math.umass.edu>
Date2012-10-24 07:43 +0000
SubjectRe: How do I assign the solution obtained by FindRoot to a variable?
Message-ID<k6866m$md7$1@smc.vnet.net>
On Oct 23, 2012, at 12:54 AM, NS Lin <nansheng.lin@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@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





[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web