Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: recursively solve equation and save the values only

  • To: mathgroup at smc.vnet.net
  • Subject: [mg125806] Re: recursively solve equation and save the values only
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Tue, 3 Apr 2012 04:48:59 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

On 4/2/12 at 4:27 AM, wsmjy2012 at gmail.com (wyn smjy) wrote:

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

>The following is my code:

>f = 5.68672 T + 6.46776 T^3

It is generally a bad habit to use a single upper case letter as
a variable in Mathematica. There are too many instances where
this conflicts with built-in symbols. Avoiding this habit
ensures such conflicts cannot arise.

>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}

More efficient would be:

sols = Table[Reduce[f == i, t, Reals], {i, 0.2, 3, 0.2}];

>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 produce the following
>error:

This won't work since t == value isn't a replacement rule. Given
usage of Reduce the numeric values can be extracted using Cases.
That is:

In[17]:= Cases[sols, _?NumericQ, Infinity]

Out[17]= {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}

But do note, you could have obtained exactly the same result
using NSolve. For example,

In[18]:= sols =
  Flatten@Table[t /. NSolve[f == i, t, Reals], {i, .2, 3, .2}]

Out[18]= {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}

I've used Flatten since NSolve returns a list rather than a number.




  • Prev by Date: Re: How to count
  • Next by Date: Re: recursively solve equation and save the values only
  • Previous by thread: Re: recursively solve equation and save the values only
  • Next by thread: Re: recursively solve equation and save the values only