Solve yields unreal solutions, NSolve yields real solutions

Discussion in 'Mathematica' started by Mukhtar Bekkali, Jun 25, 2005.

1. Mukhtar BekkaliGuest

I thought NSolve is the same as Solve just numeric. The below code
gives real answers, replacing NSolve with Solve in the last line
generates Unreal results. I do not understand why.

\!$$\* RowBox[{\(Clear["\<Global*\>"]$$, "\n", $$Remove["\<Global*\>"]$$,
"\[IndentingNewLine]", $$Needs["\<MiscellaneousRealOnly\>"]$$,
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
StyleBox["{",
FontColor->RGBColor[1, 0, 1]],
StyleBox[$$\[Lambda], \[Gamma], k$$,
FontColor->RGBColor[1, 0, 1]],
StyleBox["}",
FontColor->RGBColor[1, 0, 0]]}],
StyleBox["=",
FontColor->RGBColor[1, 0, 0]],
RowBox[{
StyleBox["{",
FontColor->RGBColor[1, 0, 0]],
RowBox[{"1",
StyleBox[",",
FontColor->RGBColor[1, 0, 0]], $$2\/3$$, ",", "0"}],
"}"}]}],
";"}], "\[IndentingNewLine]", $$Table[h\_i = \[Gamma], {i, 1, k}];$$,
"\[IndentingNewLine]", $$Table[h\_i = 1 - \[Gamma], {i, k + 1, 2}];$$,
"\[IndentingNewLine]", $$Table[{p\_i, b\_i} = {p\_i, b\_i}, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_p = Table[p\_i, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_b = Table[b\_i, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_u = Table[u\_i = 1 - \[Lambda] \((b\_i - \[Beta])$$\^2 - p\_i, {i,
1,
2}];\), "\[IndentingNewLine]", $${I\_0, I\_1, I\_2} = {0, \[Beta] /. \(Solve[ u\_1 \[Equal] u\_2, \[Beta]]$$[$$[1]$$], 1};\),
"\[IndentingNewLine]", $$T\_m = Table[m\_i = I\_i - I\_\(i - 1$$, {i, 1, 2}];\),
"\[IndentingNewLine]", $$T\_R = Table[R\_i = \(p\_i$$
m\_i - $$(\(h\_i$$
b\_i\^2\/2 + $$(1 - h\_i)$$ $$(1 - b\_i)$$\^2\/2)\), {i,
1, 2}];\),
"\[IndentingNewLine]", $$FOCP = Table[D[R\_i, p\_i], {i, 1, 2}];$$,

"\[IndentingNewLine]", $$T\_p = T\_p /. \(Solve[FOCP \[Equal] 0, T\_p]$$[$$[1]$$];\),
"\[IndentingNewLine]", $$Table[p\_i = T\_p[\($$], {i, 1, 2}];\),

"\[IndentingNewLine]", $$FOCB = Table[D[R\_i, b\_i], {i, 1, 2}];$$,

"\[IndentingNewLine]", $$NSolve[FOCB \[Equal] 0, T\_b]$$}]\)

Mukhtar Bekkali, Jun 25, 2005

2. Peter PeinGuest

I do not use RealOnly, so I can not tell you exactly what's going on,
but I *guess* the intermediate complex numbers vanish due to RealOnly.

Solve gives me only Reals in this case:

sol = RootReduce[Solve[FOCB == 0, Subscript[T, b]]]];
N[sol]

{{Subscript[b, 1] -> 1.8560333600623977,
Subscript[b, 2] -> 9.206972531099007},
{Subscript[b, 1] -> 0.3071686114422483,
Subscript[b, 2] -> 0.9103220918432908},
{Subscript[b, 1] -> -8.463201971504645,
Subscript[b, 2] -> -0.8172946229422982}}

I *guess* the intermediate complex numbers vanish due to RealOnly.

Peter Pein, Jun 26, 2005

3. Jean-Marc GullietGuest

Hi Mukhtar,

If I remember correctly the package *RealOnly* works only on
machine-size numbers and redefine only few functions such as *Power*.
Thus the only effect of the package on the *Solve* command is to
suppress at the end of all computations the display of any non-real
numbers. Internally, the algorithms still work on the complex field.

As you can see below, the imaginary part of each number is very small
and can be dropped by using the command *Chop*. You can check that both
solution sets are eventually identical.

In[1]:=
\!$$\* RowBox[{\(Clear["\<Global*\>"]$$, "\n", $$Remove["\<Global*\>"]$$,
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
StyleBox["{",
FontColor->RGBColor[1, 0, 1]],
StyleBox[$$\[Lambda], \[Gamma], k$$,
FontColor->RGBColor[1, 0, 1]],
StyleBox["}",
FontColor->RGBColor[1, 0, 0]]}],
StyleBox["=",
FontColor->RGBColor[1, 0, 0]],
RowBox[{
StyleBox["{",
FontColor->RGBColor[1, 0, 0]],
RowBox[{"1",
StyleBox[",",
FontColor->RGBColor[1, 0, 0]], $$2\/3$$, ",", "0"}],
"}"}]}],
";"}], "\[IndentingNewLine]", $$Table[h\_i = \[Gamma], {i, 1, k}];$$,
"\[IndentingNewLine]", $$Table[h\_i = 1 - \[Gamma], {i, k + 1, 2}];$$,
"\[IndentingNewLine]", $$Table[{p\_i, b\_i} = {p\_i, b\_i}, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_p = Table[p\_i, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_b = Table[b\_i, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_u = Table[u\_i = 1 - \[Lambda] \((b\_i - \[Beta])$$\^2 - p\_i, {i, 1,
2}];\), "\[IndentingNewLine]", $${I\_0, I\_1, I\_2} = {0, \[Beta] /. \(Solve[ u\_1 \[Equal] u\_2, \[Beta]]$$[$$[1]$$], 1};\),
"\[IndentingNewLine]", $$T\_m = Table[m\_i = I\_i - I\_\(i - 1$$, {i, 1, 2}];\),
"\[IndentingNewLine]", $$T\_R = Table[R\_i = \(p\_i$$
m\_i - $$(\(h\_i$$
b\_i\^2\/2 + $$(1 - h\_i)$$ $$(1 - b\_i)$$\^2\/2)\), {i,
1, 2}];\),
"\[IndentingNewLine]", $$FOCP = Table[D[R\_i, p\_i], {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_p = T\_p /. \(Solve[FOCP \[Equal] 0, T\_p]$$[$$[1]$$];\),
"\[IndentingNewLine]", $$Table[p\_i = T\_p[\($$], {i, 1, 2}];\),
"\[IndentingNewLine]", $$FOCB = Table[D[R\_i, b\_i], {i, 1, 2}];$$,
"\[IndentingNewLine]", $$NSolve[FOCB \[Equal] 0, T\_b]$$}]\)
Out[17]=
\!$${{b\_1 \[Rule] 1.8560333600624024, b\_2 \[Rule] 9.206972531099032}, {b\_1 \[Rule] \(-8.463201971504636$$,
b\_2 \[Rule] $$-0.8172946229422916$$}, {b\_1 \[Rule]
0.30716861144224816, b\_2 \[Rule] 0.9103220918432906}}\)
In[18]:=
\!$$\* RowBox[{\(Clear["\<Global*\>"]$$, "\n", $$Remove["\<Global*\>"]$$,
"\[IndentingNewLine]",
RowBox[{
RowBox[{
RowBox[{
StyleBox["{",
FontColor->RGBColor[1, 0, 1]],
StyleBox[$$\[Lambda], \[Gamma], k$$,
FontColor->RGBColor[1, 0, 1]],
StyleBox["}",
FontColor->RGBColor[1, 0, 0]]}],
StyleBox["=",
FontColor->RGBColor[1, 0, 0]],
RowBox[{
StyleBox["{",
FontColor->RGBColor[1, 0, 0]],
RowBox[{"1",
StyleBox[",",
FontColor->RGBColor[1, 0, 0]], $$2\/3$$, ",", "0"}],
"}"}]}],
";"}], "\[IndentingNewLine]", $$Table[h\_i = \[Gamma], {i, 1, k}];$$,
"\[IndentingNewLine]", $$Table[h\_i = 1 - \[Gamma], {i, k + 1, 2}];$$,
"\[IndentingNewLine]", $$Table[{p\_i, b\_i} = {p\_i, b\_i}, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_p = Table[p\_i, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_b = Table[b\_i, {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_u = Table[u\_i = 1 - \[Lambda] \((b\_i - \[Beta])$$\^2 - p\_i, {i, 1,
2}];\), "\[IndentingNewLine]", $${I\_0, I\_1, I\_2} = {0, \[Beta] /. \(Solve[ u\_1 \[Equal] u\_2, \[Beta]]$$[$$[1]$$], 1};\),
"\[IndentingNewLine]", $$T\_m = Table[m\_i = I\_i - I\_\(i - 1$$, {i, 1, 2}];\),
"\[IndentingNewLine]", $$T\_R = Table[R\_i = \(p\_i$$
m\_i - $$(\(h\_i$$
b\_i\^2\/2 + $$(1 - h\_i)$$ $$(1 - b\_i)$$\^2\/2)\), {i,
1, 2}];\),
"\[IndentingNewLine]", $$FOCP = Table[D[R\_i, p\_i], {i, 1, 2}];$$,
"\[IndentingNewLine]", $$T\_p = T\_p /. \(Solve[FOCP \[Equal] 0, T\_p]$$[$$[1]$$];\),
"\[IndentingNewLine]", $$Table[p\_i = T\_p[\($$], {i, 1, 2}];\),
"\[IndentingNewLine]", $$FOCB = Table[D[R\_i, b\_i], {i, 1, 2}];$$,
"\[IndentingNewLine]", $$Solve[FOCB \[Equal] 0, T\_b]$$}]\)
Out[34]=
\!$${{b\_1 \[Rule] 1\/820\ \((\(-1722$$ -
14661241\/$$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$2/3$$ +
329294\/$$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$ +
86\ $$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$ - \
$$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$2/3$$)\),
b\_2 \[Rule]
31\/10 +
3829\/$$20\ \((209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$\) +
1\/20\ $$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$},
{b\_1 \
\[Rule] 1\/41\ $$(\(-\(861\/10$$\) +
14661241\/$$40\ \((209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$2/3$$\) + $$14661241\ \[ImaginaryI]\ \@3$$\/$$40\ \((209533 + \ 410\ \[ImaginaryI]\ \@72777)$$\^$$2/3$$\) -
164647\/$$20\ \((209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3\$$\) + $$164647\ \[ImaginaryI]\ \@3$$\/$$20\ \((209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$1/3$$\) -
43\/20\ $$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$ -
43\/20\ \[ImaginaryI]\ \@3\ $$(209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$1/3$$ +
1\/40\ $$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$2/3$$ -
1\/40\ \[ImaginaryI]\ \@3\ $$(209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$2/3$$)\),
b\_2 \[Rule]
31\/10 - $$3829\ \((1 - \[ImaginaryI]\ \@3)$$\)\/$$40\ \((209533 + \ 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$\) -
1\/40\ $$(1 + \[ImaginaryI]\ \@3)$$\ $$(209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$1/3$$}, {b\_1 \[Rule]
1\/41\ $$(\(-\(861\/10$$\) +
14661241\/$$40\ \((209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$2/3$$\) - $$14661241\ \[ImaginaryI]\ \@3$$\/$$40\ \((209533 + \ 410\ \[ImaginaryI]\ \@72777)$$\^$$2/3$$\) -
164647\/$$20\ \((209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3\$$\) - $$164647\ \[ImaginaryI]\ \@3$$\/$$20\ \((209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$1/3$$\) -
43\/20\ $$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$ +
43\/20\ \[ImaginaryI]\ \@3\ $$(209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$1/3$$ +
1\/40\ $$(209533 + 410\ \[ImaginaryI]\ \@72777)$$\^$$2/3$$ +

1\/40\ \[ImaginaryI]\ \@3\ $$(209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$2/3$$)\),
b\_2 \[Rule]
31\/10 - $$3829\ \((1 + \[ImaginaryI]\ \@3)$$\)\/$$40\ \((209533 + \ 410\ \[ImaginaryI]\ \@72777)$$\^$$1/3$$\) -
1\/40\ $$(1 - \[ImaginaryI]\ \@3)$$\ $$(209533 + 410\ \[ImaginaryI]\ \ \@72777)$$\^$$1/3$$}}\)
In[35]:=
N[%]
Out[35]=
\!$${{b\_1. \[Rule] \(\(1.8560333600623968$$$$\[InvisibleSpace]$$\) +
1.109139880210888*^-15\ \[ImaginaryI],
b\_2. \[Rule] $$\(9.206972531099009$$$$\[InvisibleSpace]$$\) -
2.7755575615628914*^-16\ \[ImaginaryI]}, {b\_1. \[Rule] \
$$\(0.3071686114422483$$$$\[InvisibleSpace]$$\) +
1.0398186376977076*^-14\ \[ImaginaryI],
b\_2. \[Rule] $$\(0.9103220918432906$$$$\[InvisibleSpace]$$\) +
1.7763568394002505*^-15\ \[ImaginaryI]}, {b\_1. \[Rule] \
$$-8.463201971504644$$ - 1.0398186376977076*^-14\ \[ImaginaryI],
b\_2. \[Rule] $$-0.8172946229422982$$ -
1.7763568394002505*^-15\ \[ImaginaryI]}}\)
In[36]:=
Chop[%]
Out[36]=
\!$${{b\_1. \[Rule] 1.8560333600623968, b\_2. \[Rule] 9.206972531099009}, {b\_1. \[Rule] 0.3071686114422483, b\_2. \[Rule] 0.9103220918432906}, {b\_1. \[Rule] \(-8.463201971504644$$,
b\_2. \[Rule] $$-0.8172946229422982$$}}\)

Best regards,
/J.M.

Jean-Marc Gulliet, Jun 26, 2005