# recursively solve equation and saving the values

I am new to Mathematica. I am trying to recursively solve an equation and saving the values into a Table. Since I need the real solutions only I use Reduce.

The following is my code:

f = 5.68672 T + 6.46776 T^3

t = Table[i, {i, 0.2, 3, 0.2}]

sols = Table[Reduce[f == t[[i]], T, Reals], {i, Length[t]}]
{T == 0.0351204, T == 0.06995, T == 0.104221, T == 0.137708,
T == 0.170237, T == 0.201687, T == 0.231988, T == 0.26111,
T == 0.289058, T == 0.315857, T == 0.34155, T == 0.366188,
T == 0.389828, T == 0.412529, T == 0.434347}


The solution is OK as I compared it to other software as well. The only thing that matters me is how to remove the T== , i.e. I just need the solution and then to copy this into another table, say sols2 that contains

sols2 = {0.0351204, 0.06995, 0.104221, T == 0.137708,.., 0.434347}


I tried the following: sols2 = T/.sols but get the following error:

sols2 = ReplaceAll::reps: ... is neither a list of replacement rules nor
a valid dispatch table, and so cannot be used for replacing. >>


Does anyone know how to remove the T==?

• What equation are you actually trying to solve? I don't see one. Apr 2 '12 at 1:05

You get

  {0.0351204, 0.06995, 0.104221, 0.137709, 0.170237, 0.201687, 0.231988, 0.26111, 0.289058, 0.315857, 0.34155, 0.366188, 0.389828, 0.412529, 0.434347}


using any of the methods below:

   Last /@ sols


or

   sols[[All,2]]


or

  sols /. (a_ == b_) -> b


All give a list of solutions with T== removed.

Update: an alternative approach to get a list of solutions for T is to Map Reduce[...] on the list t:

  solutionlist = Map[Reduce[f == #, T, Reals][] &, t]

• Thank you so much. Really appreciate it! Apr 1 '12 at 13:05
• @Wayan, my pleasure.
– kglr
Apr 1 '12 at 13:16

After your Reduce you can do something as:

T /. (sols /. a_Equal :> {Rule @@ a})

{0.0351204, 0.06995, 0.104221, 0.137709, 0.170237, 0.201687, \
0.231988, 0.26111, 0.289058, 0.315857, 0.34155, 0.366188, 0.389828, \
0.412529, 0.434347}


T == 0.0351204 has the FullForm: Equal[T, 0.0351204] and you want the second argument of the function Equal, therefore you could use:

sols /. Equal -> (#2 &)


Alternatively, you can use ToRules and then make the replacement you attempted:

T /. ToRules /@ sols


As a side matter, there is a cleaner syntax for your second Table application:

Table[Reduce[f == i, T, Reals], {i, t}]


At least in Mma8 you can pass Reals to Solve or NDSolve and then proceed like you tried:

sols = Table[First@NSolve[f == t[[i]], T, Reals], {i, Length[t]}]
T/.sols

• Hi Peter, I copy and paste your code but the following error arise: T /. sols ReplaceAll::reps: {T==0.0351204,T==0.06995,T==0.104221,T==0.137709,T==0.170237,T==0.201687,<<3>>,T==0.315857,T==0.34155,T==0.366188,T==0.389828,T==0.412529,T==0.434347} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> T /. {T == 0.0351204, T == 0.06995, T == 0.104221, T == 0.137709, T == 0.170237, T == 0.201687, T == 0.231988, T == 0.26111, T == 0.289058, T == 0.315857, T == 0.34155, T == 0.366188, T == 0.389828, T == 0.412529, T == 0.434347} Apr 1 '12 at 13:09
• @Wayan It seems, you took Reduce instead of Solve or NSolve because I see "==" signs in your output. Apr 1 '12 at 13:18
• Oh, yeah. I just realized there was a typo. I works now. Thanks Peter! Apr 1 '12 at 13:21