MathGroup Archive 1995

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

Search the Archive

Re: How do you input a solution from FindRoot into another equation?

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg765] Re: How do you input a solution from FindRoot into another equation?
  • From: Sigurd.Schelstraete at rug.ac.be (Sigurd Schelstraete)
  • Date: Tue, 18 Apr 1995 00:56:04 -0400
  • Organization: University of Ghent, Belgium

Dave Wagner (wagner at bullwinkle.cs.Colorado.EDU) wrote:
: In article <3mfauj$l4o at news0.cybernetics.net>,
: Robert Dack <rdack at acs.ucalgary.ca> wrote:
: >
: >Can someone tell me how to take the output from the FindRoot
: >cammand and input it into the next command line?  The output
: >comes out in the form { x -> 2.435 }, so the usual [%] command on
: >the next line won't work.
: >

: some examples of how to extract the answers.

: In[20]:=
:     Solve[x^2==2, x]
: Out[20]=
:     {{x -> -Sqrt[2]}, {x -> Sqrt[2]}}

: In[21]:=
:     { %[[1,1,2]], %[[2,1,2]] }
: Out[21]=
:     {-Sqrt[2], Sqrt[2]}

: In[22]:=
:     Solve[{x^2-2 == y, x+y==0}]
: Out[22]=
:     {{y -> -1, x -> 1}, {y -> 2, x -> -2}}

: In[23]:=
:     { %[[1,1,2]], %[[1,2,2]], %[[2,1,2]] }
: Out[23]=
:     {-1, 1, 2}

I've already mailed my solution to the original author of this thread, 
but maybe it's useful to repeat it here, since I think it's slightly 
more elegant than method presented above. For instance, in the last 
example, you have to know in which order the solutions will appear (in 
this case y comes before x). Instead of "FindRoot[lhs==rhs,{x,xo}]", you 
can use:

x /. FindRoot[lhs==rhs,{x,x0}]

Mma will replace this (internally) with:

x /. {x->2.35466}

Giving the output:

2.35466

without the need to go delving into the List {x->2.35466}.

This can also be used when more than one solution is given (as is the 
case with Solve or NSolve), e.g.:

x /. Solve[x^2==4,x]

gives:

{-2,2}

And it still works when more than one variable is present and you are 
only interested in one of the variables, e.g:

x /. Solve[x^2-2==y,x+y==0]

is replaced with:

x /. {{y->-1,x->1},{y->2,x->-2}}

wich gives as the final output:

{1,-2}


Sigurd Schelstraete
University of Gent
Belgium



  • Prev by Date: Re: Sequential Plotting, and "INKEY$", in Mathematica?
  • Next by Date: Re: FactorInteger Print Formatting
  • Previous by thread: Re: How do you input a solution from FindRoot into another equation?
  • Next by thread: Default methods within Mathematica