Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #12164
| From | Murray Eisenberg <murray@math.umass.edu> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: How do I assign the solution obtained by FindRoot to a variable? |
| Date | 2012-10-24 07:43 +0000 |
| Message-ID | <k6866m$md7$1@smc.vnet.net> (permalink) |
| References | <20121023045437.AE4F2685C@smc.vnet.net> |
| Organization | Time-Warner Telecom |
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
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
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
csiph-web