0
$\begingroup$

I have an equation that has no analytical solution and can have multiple roots. I am focusing on Real roots that are in the (0,1) interval. For example:

Block[{A = 0.3, B = 1}, 
 Reduce[1/(16 (1 - x)^7) + (
     4.5 (1 - x) (-1 + 3 x))/(-B - 0.5 A + 4.5 (1 - x)^2 x)^3 == 0 && 
   0 < x < 1, x, Reals]]

For some values of A and B, I may have:

  • no root that satisfies my constraints
  • 1 root
  • 2 roots

I would like to always pick up, when it exists, the lowest root. Otherwise, I would like to set `x=1'.

I have tried two different things: Select+ Conditional; creating a table and picking up the lowest+Conditional. I have failed in both. Any ideas on how to proceed? Thank you!

$\endgroup$
1
$\begingroup$

If Reduce returns an Or expression, you can apply Sort to get the lowest, something like this:

With[{A = 3/10, B = 1},
 Block[{s},
  s = Reduce[1/(16 (1 - x)^7) + (4.5 (1 - x) (-1 + 3 x))/
        (-B - 0.5 A + 4.5 (1 - x)^2 x)^3 == 0 &&
     0 < x < 1, x, 
    Reals];
  If[Head[s] === Or, First[Sort[s]], s]
  ]
 ]

(*  x == 0.349826  *)  

Try it with {A,B}={-1,0} for the single solution case, and {A,B}={-1,2} for the no solution case.

Sanity Check: The following code will perform a quick sanity check to confirm that Sort will give us the right answers:

nTest = 20;
testCases = Thread[x == RandomReal[{0, 1}, nTest]]
Sort[testCases]

Exercising the above code over various ranges like {0,1}, {-1,1} and {-100,100} indicates Sort is doing its job, as long as the expressions are all of the assumed form. To get the least of the expressions, we apply First to the sorted list. To get the greatest of the expressions, we apply Last.

Why Greater did not work: A comment mentioned that Sort[s,Greater] did not work to return the highest answer. So, why didn't Greater work? The documentation for Greater says it returns True or False for real numbers. We are trying to sort expressions. Since Greater[x==1, x==2] does not return True or False, we are not really comparing the expressions.

How to use Greater Since Greater works with real numbers, we can simply point to the real numbers in our expressions, like this:

Sort[testCases, Greater[Last[#1], Last[#2]] &]

Here, we are applying Last to expressions like x == 3.14159, so Last returns just the real number. We pass 2 real numbers to Greater, and voilà, Greater makes the comparison.

$\endgroup$
  • $\begingroup$ Thank you for your answer, @LouisB. I am not sure if Sort is indeed working. If I try to get the highest number by doing If[Head[s] === Or, First[Sort[s, Greater]], s] it doesn't change anything. So I am not sure if Mathematica is sorting the values or the name of the variable that is being solved to be honest. $\endgroup$ – Laura K Feb 22 '18 at 0:33
  • $\begingroup$ @LauraK You are right to be skeptical. It would be wise to examine the output of Reduce to make sure it has the assumed form. I added some ideas on testing Sort alone and using Sort with Greater. $\endgroup$ – LouisB Feb 22 '18 at 4:30
  • $\begingroup$ Your comments are great. Very good information you have added there. Your answer actually gave me a good idea: to use ToRules. I have mentioned below in case that is of any use to you. Thank you again! $\endgroup$ – Laura K Feb 22 '18 at 15:15
0
$\begingroup$

LouisB's help gave me a good idea on how to proceed. I could first transform Reduce's results into a sequence and then select the answers and Order them.

To select the lowest solution:

s=Block[{A = 0.3, B = 1}, 
 Reduce[1/(16 (1 - x)^7) + (
     4.5 (1 - x) (-1 + 3 x))/(-B - 0.5 A + 4.5 (1 - x)^2 x)^3 == 0 && 
   0 < x < 1, x, Reals]]
ss={ToRules[s]}
First[Sort[ss[[All, 1, 2]]]]

To select the highest solution:

s=Block[{A = 0.3, B = 1}, 
 Reduce[1/(16 (1 - x)^7) + (
     4.5 (1 - x) (-1 + 3 x))/(-B - 0.5 A + 4.5 (1 - x)^2 x)^3 == 0 && 
   0 < x < 1, x, Reals]]
ss={ToRules[s]}
First[Sort[ss[[All, 1, 2]], Greater]]

I hope someone can benefit from this answer that was inspired by LouisB's help from above.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.