Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128488] Re: How do I assign the solution obtained by FindRoot to a variable?
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Wed, 24 Oct 2012 03:33:47 -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>

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 <nansheng.lin at 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 <hanlonr357 at 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
>>
>>
>> On Tue, 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?
>> >
>> > Thanks!
>> >
>> > NS
>> >
>
>



  • Prev by Date: Re: How do I assign the solution obtained by FindRoot to a variable?
  • Next by Date: Re: How do I assign the solution obtained by FindRoot to a variable?
  • Previous by thread: Re: How do I assign the solution obtained by FindRoot to
  • Next by thread: Re: How do I assign the solution obtained by FindRoot to a variable?