Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

1,489 views
Skip to first unread message

NS Lin

unread,
Oct 23, 2012, 1:02:40 AM10/23/12
to
I would like to store the root/solution obtained by the FindRoot[] function to a variable. How can I do that?

Thanks!

NS

Bob Hanlon

unread,
Oct 24, 2012, 3:31:03 AM10/24/12
to
eqn = Sin[x] + Exp[x];

z = x /.
FindRoot[eqn, {x, 0}]

-0.588533

eqn /. x -> z

0.

eqns = {Exp[x - 2] == y, y^2 == x};

{r, s} = {x, y} /.
FindRoot[eqns,
{{x, 1}, {y, 1}}]

{0.019026, 0.137935}

eqns /. {x -> r, y -> s}

{True, True}


Bob Hanlon

Nan-Sheng Lin

unread,
Oct 24, 2012, 3:31:51 AM10/24/12
to

Thank you very much for all the responses I received. I did not realize
that I only replied to the sender, and not cc-ing the group.

Anyway, all the responses have been helpful, and it is clear to me now. I
do have a follow-up question - how do I use FindRoot[] or any other
function to find ALL the roots of a non-polynomial function within a
specified range?

Thanks!

NS

Harvey P. Dale

unread,
Oct 24, 2012, 3:32:44 AM10/24/12
to
If the sought root is, for example, the square root of 2, and the variable is sqrt2:

sqrt2=x/.FindRoot[x==Sqrt[2],{x,1.4}]

Best,

Harvey

Dr. Wolfgang Hintze

unread,
Oct 24, 2012, 3:32:54 AM10/24/12
to
In[472]:= eq = x == Exp[-x]

Out[472]= x == E^(-x)

In[475]:= z = x /. FindRoot[eq, {x, 1}]

Out[475]= 0.5671432904097838

In[477]:= Exp[-z]

Out[477]= 0.5671432904097838

Regards,
Wolfgang

Bob Hanlon

unread,
Oct 24, 2012, 3:33:34 AM10/24/12
to
Plot can be used to help identify starting values for FindRoot

eqn = Sin[x] + Exp[x];

z = x /.
FindRoot[eqn, {x, 0}]

-0.588533

Plot[eqn, {x, -10, 2}, Epilog -> {Red, AbsolutePointSize[4],
Point[{z, 0}]}]

For negative x, the Sin[x] term will cause the expression to
oscillate. Pick some range of interest

pts = {x, 0} /. Table[
FindRoot[eqn, {x, -n*Pi}],
{n, 0, 12}];

Plot[eqn, {x, -40, 0.75},
Epilog -> {Red, AbsolutePointSize[4],
Point[pts]}]

eqns = {Exp[x - 2] == y, y^2 == x};

Plot[
Evaluate[y /. Solve[#, y] & /@ eqns],
{x, -.5, 2.75},
Frame -> True,
Axes -> False]

pts = {x, y} /.
FindRoot[eqns,
{{x, #[[1]]}, {y, #[[2]]}}] & /@
{{0.1, 0.1}, {2.5, 1.5}}

{{0.019026, 0.137935}, {2.44754, 1.56446}}

Plot[
Evaluate[y /. Solve[#, y] & /@ eqns],
{x, -.5, 2.75},
Frame -> True,
Axes -> False,
Epilog -> {Red, AbsolutePointSize[4],
Point[pts]}]

You can also use Ted Ersek's Root Search package

http://forums.wolfram.com/mathgroup/archive/2012/Apr/msg00444.html



Bob Hanlon


On Tue, Oct 23, 2012 at 12:55 PM, Nan-Sheng Lin <nanshe...@gmail.com> wrote:
> Thank you very much for all the responses I received. I did not realize
> that I only replied to the sender, and not cc-ing the group.
>
> Anyway, all the responses have been helpful, and it is clear to me now. I
> do have a follow-up question - how do I use FindRoot[] or any other function
> to find ALL the roots of a non-polynomial function within a specified range?
>
> Thanks!
>
> NS
>
> On Tue, Oct 23, 2012 at 8:11 AM, Bob Hanlon <hanlo...@gmail.com> wrote:
>>
>> eqn = Sin[x] + Exp[x];
>>
>> z = x /.
>> FindRoot[eqn, {x, 0}]
>>
>> -0.588533
>>
>> eqn /. x -> z
>>
>> 0.
>>
>> eqns = {Exp[x - 2] == y, y^2 == x};
>>
>> {r, s} = {x, y} /.
>> FindRoot[eqns,
>> {{x, 1}, {y, 1}}]
>>
>> {0.019026, 0.137935}
>>
>> eqns /. {x -> r, y -> s}
>>
>> {True, True}
>>
>>
>> Bob Hanlon
>>
>>

Nasser M. Abbasi

unread,
Oct 24, 2012, 3:43:07 AM10/24/12
to
On 10/23/2012 12:02 AM, NS Lin wrote:

> I would like to store the root/solution obtained by the FindRoot[] function to a variable.
>How can I do that?
>


In[7]:= Clear[x]

sol=FindRoot[Sin[x]+Exp[x],{x,0}]
Out[8]= {x->-0.588533}

In[9]:= x=x/.sol
Out[9]= -0.588533

In[10]:= x
Out[10]= -0.588533



Murray Eisenberg

unread,
Oct 24, 2012, 3:43:18 AM10/24/12
to
On Oct 23, 2012, at 12:54 AM, NS Lin <nanshe...@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 mur...@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






Bill Rowe

unread,
Oct 24, 2012, 3:43:55 AM10/24/12
to
On 10/23/12 at 12:54 AM, nanshe...@gmail.com (NS Lin) wrote:

>I would like to store the root/solution obtained by the FindRoot[]
>function to a variable. How can I do that?

Here is a simple example of how to do that

In[1]:= y = x /. FindRoot[x^2 - 10, {x, 2}];
y

Out[2]= 3.16228


Alexei Boulbitch

unread,
Oct 25, 2012, 1:40:09 AM10/25/12
to
I would like to store the root/solution obtained by the FindRoot[] function to a variable. How can I do that?

Thanks!

NS

Hi, Nansheng,

Assume this:

eq=x^2-x-1==0;

is your equation, and this:

FindRoot[eq, {x, 1}]

{x -> 1.61803}

is what FindRoot returns you. Then you may simply do the following:

y = FindRoot[eq, {x, 1}][[1, 2]];
y

1.61803

Is this the thing you had in mind?

Have fun, Alexei


Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




0 new messages