Message Boards Message Boards

0
|
5857 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Extract the real solutions among many which are complex?

Posted 6 years ago

Here is the equation and the solution: I want the two real solutions.

In[2]:= soln1[a_, b_, c_, \[Xi]_, \[Lambda]_, \[Phi]_, rn_] := 
  NSolve[\[Rho]^2 + 
      2 \[Rho] (a Sin[\[Theta]] Cos[\[Phi]] + 
         b Sin[\[Theta]] Sin[\[Phi]] + c Cos[\[Theta]]) + a^2 + b^2 + 
      c^2 - rn^2 == 
     0 && \[Rho] - \[Lambda] rn Cos[\[Theta] + \[Xi] Cos[\[Phi]]]^2 ==
      0, {\[Rho], \[Theta]}];

In[8]:= ss = soln1[.5, .5, .5, .2, .5, .1, 1]

During evaluation of In[8]:= NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information.

Out[8]= {{\[Rho] -> -2.51366 - 3.61652 I, \[Theta] -> 
   1.8313 - 1.79756 I}, {\[Rho] -> -2.51366 + 3.61652 I, \[Theta] -> 
   1.8313 + 1.79756 I}, {\[Rho] -> -0.146207 - 
    0.0312241 I, \[Theta] -> -1.82025 + 
    0.520643 I}, {\[Rho] -> -0.146207 + 
    0.0312241 I, \[Theta] -> -1.82025 - 0.520643 I}, {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}, {\[Rho] -> 
   0.311045 - 0.210578 I, \[Theta] -> 
   2.25043 - 0.388346 I}, {\[Rho] -> 
   0.311045 + 0.210578 I, \[Theta] -> 
   2.25043 + 0.388346 I}, {\[Rho] -> 0.417435, \[Theta] -> -0.617471}}
POSTED BY: Hong-Yee Chiu
6 Replies
Posted 6 years ago

Fantastic, it worked! I got:

In[7]:= ssReal = Cases[{\[Rho], \[Theta]} /. ss, {_Real, _Real}]

Out[7]= {{0.152974, 0.785684}, {0.417435, -0.617471}}

Thank you and others who responded to my problem.

POSTED BY: Hong-Yee Chiu

@Hong-Yee Chiu

Please make sure you know the rules: https://wolfr.am/READ-1ST

The rules explain how to format your code properly. If you do not format code, it may become corrupted and useless to other members. Please EDIT your post and make sure code blocks start on a new paragraph and look framed and colored like this.

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

enter image description here

POSTED BY: EDITORIAL BOARD
Posted 6 years ago

To select the real solutions from the list of all solutions just add the following line

ssReal=Cases[{\[Rho], \[Theta]} /. ss, {_Real, _Real}]
POSTED BY: Mike Luntz
Posted 6 years ago

Thanks for your suggestion. It has problems as follows. (1) The original soln1 gives the following results:

soln1[a_, b_, c_, \[Xi]_, \[Lambda]_, \[Phi]_, rn_] := 
  NSolve[\[Rho]^2 + 
      2 \[Rho] (a Sin[\[Theta]] Cos[\[Phi]] + 
         b Sin[\[Theta]] Sin[\[Phi]] + c Cos[\[Theta]]) + a^2 + b^2 + 
      c^2 - rn^2 == 
     0 && \[Rho] - \[Lambda] rn Cos[\[Theta] + \[Xi] Cos[\[Phi]]]^2 ==
      0, {\[Rho], \[Theta]}];
(*evaluation*)
In[8]:= ss = soln1[.5, .5, .5, .2, .5, .1, 1]

During evaluation of In[8]:= NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information.

Out[8]= {{\[Rho] -> -2.51366 - 3.61652 I, \[Theta] -> 
   1.8313 - 1.79756 I}, {\[Rho] -> -2.51366 + 3.61652 I, \[Theta] -> 
   1.8313 + 1.79756 I}, {\[Rho] -> -0.146207 - 
    0.0312241 I, \[Theta] -> -1.82025 + 
    0.520643 I}, {\[Rho] -> -0.146207 + 
    0.0312241 I, \[Theta] -> -1.82025 - 0.520643 I}, {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}, {\[Rho] -> 
   0.311045 - 0.210578 I, \[Theta] -> 
   2.25043 - 0.388346 I}, {\[Rho] -> 
   0.311045 + 0.210578 I, \[Theta] -> 
   2.25043 + 0.388346 I}, {\[Rho] -> 0.417435, \[Theta] -> -0.617471}}
NOTE: The two real solutions are: In[8]:= ss = soln1[.5, .5, .5, .2, .5, .1, 1]

During evaluation of In[8]:= NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information.

Out[8]= {{\[Rho] -> -2.51366 - 3.61652 I, \[Theta] -> 
   1.8313 - 1.79756 I}, {\[Rho] -> -2.51366 + 3.61652 I, \[Theta] -> 
   1.8313 + 1.79756 I}, {\[Rho] -> -0.146207 - 
    0.0312241 I, \[Theta] -> -1.82025 + 
    0.520643 I}, {\[Rho] -> -0.146207 + 
    0.0312241 I, \[Theta] -> -1.82025 - 0.520643 I}, {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}, {\[Rho] -> 
   0.311045 - 0.210578 I, \[Theta] -> 
   2.25043 - 0.388346 I}, {\[Rho] -> 
   0.311045 + 0.210578 I, \[Theta] -> 
   2.25043 + 0.388346 I}, {\[Rho] -> 0.417435, \[Theta] -> -0.617471}}

NOTE: The two real solutions are:  {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}
{\[Rho] -> 0.417435, \[Theta] -> -0.617471}
These are the two solution I want.
(2) Using your suggestion (I use soln2 to avoid conflict)
soln2[a_, b_, c_, \[Xi]_, \[Lambda]_, \[Phi]_, rn_] := 
  NSolve[\[Rho]^2 + 
      2 \[Rho] (a Sin[\[Theta]] Cos[\[Phi]] + 
         b Sin[\[Theta]] Sin[\[Phi]] + c Cos[\[Theta]]) + a^2 + b^2 + 
      c^2 - rn^2 == 
     0 && \[Rho] - \[Lambda] rn Cos[\[Theta] + \[Xi] Cos[\[Phi]]]^2 ==
      0, {\[Rho], \[Theta]}, Reals];
EVALUATE]
In[20]:= ss2 = soln2[1/2, 1/2, 1/2, 1/5, 1/2, 1/10, 1]

Out[20]= {{\[Rho] -> 
   ConditionalExpression[
    0.5 Cos[0.199001 + 1. (-0.617471 + 6.28319 C[1])]^2, 
    C[1] \[Element] Integers], \[Theta] -> 
   ConditionalExpression[1. (-0.617471 + 6.28319 C[1]), 
    C[1] \[Element] Integers]}, {\[Rho] -> 
   ConditionalExpression[
    0.5 Cos[0.199001 + 1. (0.785684 + 6.28319 C[1])]^2, 
    C[1] \[Element] Integers], \[Theta] -> 
   ConditionalExpression[1. (0.785684 + 6.28319 C[1]), 
    C[1] \[Element] Integers]}}
Please note: The two real solutions:  {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}
{\[Rho] -> 0.417435, \[Theta] -> -0.617471}

are no where to be found. Can you illuminate me on this issue? Thank you.

POSTED BY: Hong-Yee Chiu
Posted 6 years ago

Thanks for your suggestion. It has problems as follows. (1) The original soln1 gives the following results:

    soln1[a_, b_, c_, \[Xi]_, \[Lambda]_, \[Phi]_, rn_] := 
      NSolve[\[Rho]^2 + 
          2 \[Rho] (a Sin[\[Theta]] Cos[\[Phi]] + 
             b Sin[\[Theta]] Sin[\[Phi]] + c Cos[\[Theta]]) + a^2 + b^2 + 
          c^2 - rn^2 == 
         0 && \[Rho] - \[Lambda] rn Cos[\[Theta] + \[Xi] Cos[\[Phi]]]^2 ==
          0, {\[Rho], \[Theta]}];
    (*evaluation*)
    In[8]:= ss = soln1[.5, .5, .5, .2, .5, .1, 1]

During evaluation of In[8]:= NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information.

Out[8]= {{\[Rho] -> -2.51366 - 3.61652 I, \[Theta] -> 
   1.8313 - 1.79756 I}, {\[Rho] -> -2.51366 + 3.61652 I, \[Theta] -> 
   1.8313 + 1.79756 I}, {\[Rho] -> -0.146207 - 
    0.0312241 I, \[Theta] -> -1.82025 + 
    0.520643 I}, {\[Rho] -> -0.146207 + 
    0.0312241 I, \[Theta] -> -1.82025 - 0.520643 I}, {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}, {\[Rho] -> 
   0.311045 - 0.210578 I, \[Theta] -> 
   2.25043 - 0.388346 I}, {\[Rho] -> 
   0.311045 + 0.210578 I, \[Theta] -> 
   2.25043 + 0.388346 I}, {\[Rho] -> 0.417435, \[Theta] -> -0.617471}}
NOTE: The two real solutions are: In[8]:= ss = soln1[.5, .5, .5, .2, .5, .1, 1]

During evaluation of In[8]:= NSolve::ifun: Inverse functions are being used by NSolve, so some solutions may not be found; use Reduce for complete solution information.

Out[8]= {{\[Rho] -> -2.51366 - 3.61652 I, \[Theta] -> 
   1.8313 - 1.79756 I}, {\[Rho] -> -2.51366 + 3.61652 I, \[Theta] -> 
   1.8313 + 1.79756 I}, {\[Rho] -> -0.146207 - 
    0.0312241 I, \[Theta] -> -1.82025 + 
    0.520643 I}, {\[Rho] -> -0.146207 + 
    0.0312241 I, \[Theta] -> -1.82025 - 0.520643 I}, {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}, {\[Rho] -> 
   0.311045 - 0.210578 I, \[Theta] -> 
   2.25043 - 0.388346 I}, {\[Rho] -> 
   0.311045 + 0.210578 I, \[Theta] -> 
   2.25043 + 0.388346 I}, {\[Rho] -> 0.417435, \[Theta] -> -0.617471}}

NOTE: The two real solutions are: {[Rho] -> 0.152974, [Theta] -> 0.785684} {[Rho] -> 0.417435, [Theta] -> -0.617471} These are the two solution I want. (2) Using your suggestion (I use soln2 to avoid conflict)

soln2[a_, b_, c_, \[Xi]_, \[Lambda]_, \[Phi]_, rn_] := 
  NSolve[\[Rho]^2 + 
      2 \[Rho] (a Sin[\[Theta]] Cos[\[Phi]] + 
         b Sin[\[Theta]] Sin[\[Phi]] + c Cos[\[Theta]]) + a^2 + b^2 + 
      c^2 - rn^2 == 
     0 && \[Rho] - \[Lambda] rn Cos[\[Theta] + \[Xi] Cos[\[Phi]]]^2 ==
      0, {\[Rho], \[Theta]}, Reals];
EVALUATE]
In[20]:= ss2 = soln2[1/2, 1/2, 1/2, 1/5, 1/2, 1/10, 1]

Out[20]= {{\[Rho] -> 
   ConditionalExpression[
    0.5 Cos[0.199001 + 1. (-0.617471 + 6.28319 C[1])]^2, 
    C[1] \[Element] Integers], \[Theta] -> 
   ConditionalExpression[1. (-0.617471 + 6.28319 C[1]), 
    C[1] \[Element] Integers]}, {\[Rho] -> 
   ConditionalExpression[
    0.5 Cos[0.199001 + 1. (0.785684 + 6.28319 C[1])]^2, 
    C[1] \[Element] Integers], \[Theta] -> 
   ConditionalExpression[1. (0.785684 + 6.28319 C[1]), 
    C[1] \[Element] Integers]}}
Please note: The two real solutions:  {\[Rho] -> 
   0.152974, \[Theta] -> 0.785684}
{\[Rho] -> 0.417435, \[Theta] -> -0.617471}

are no where to be found. Can you illuminate me on this issue? Thank you.

POSTED BY: Hong-Yee Chiu

Simply add Real in NSolve:

soln1[a_, b_, c_, \[Xi]_, \[Lambda]_, \[Phi]_, rn_] := 
  NSolve[\[Rho]^2 + 
      2 \[Rho] (a Sin[\[Theta]] Cos[\[Phi]] + 
         b Sin[\[Theta]] Sin[\[Phi]] + c Cos[\[Theta]]) + a^2 + b^2 + 
      c^2 - rn^2 == 
     0 && \[Rho] - \[Lambda] rn Cos[\[Theta] + \[Xi] Cos[\[Phi]]]^2 ==
      0, {\[Rho], \[Theta]}, Reals];
soln1[1/2, 1/2, 1/2, 1/2, 1/2, 1/10, 1]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract