Choosing the smallest real part

  • Thread starter Thread starter member 428835
  • Start date Start date
AI Thread Summary
The discussion focuses on selecting the smallest real part from complex solutions in Mathematica. A user is attempting to solve a complex equation involving an integral and is seeking a systematic method to identify the smallest real part of the solutions. Suggestions include using NSolve or FindRoot instead of Roots for non-polynomial equations and avoiding capital letters for user-defined variables to prevent errors. The conversation also emphasizes the importance of expanding the integrand and differentiating with respect to the variable to simplify the problem. Ultimately, techniques such as Min[Re[values]] and Min@Part[Flatten@List@ToRules@(expr),All,2] are recommended for extracting the smallest real part from the results.
member 428835
Hi PF!

I am solving something in mathematica and I am finding three solutions, all of which are complex. I want to select the smallest real part. I know I could simply numerically check each, but is there a more systematic way to do this?

Thanks!
 
Physics news on Phys.org
I should say, what I'm really wanting to do is operate over each of the outputs and take the smallest value.
 
Assuming you have a (flattened) list of values:
Code:
Min[Re[values]]
Assuming you have a (flattened) list of rules:
Code:
Min[Re[varname/.values[[#]]&/@Range@Length@values]]
A (shorter) alternative would be:
Code:
Min[Re[values[[All, 2]]]]
 
kontejnjer said:
Assuming you have a (flattened) list of values:
Code:
Min[Re[values]]
Assuming you have a (flattened) list of rules:
Code:
Min[Re[varname/.values[[#]]&/@Range@Length@values]]
A (shorter) alternative would be:
Code:
Min[Re[values[[All, 2]]]]
Thanks so much for the help! Sadly this isn't working, or perhaps I just have no clue! My code is

Code:
values = ComplexExpand[
  Roots[D[Integrate[( 
        f''[x] f[x] + 2 (f'[x])^2 + x f'[x] - 2 a/(1 + a) f[x])^2, {x,
        0, 1}], A] == 0, A]]

and when I use what you've said Mathematica is echoing out the responses. Am I applying this incorrectly?

Thanks so much for taking the time to help me out!
 
From what I can tell, you're trying to solve an equation of the kind \frac{\partial}{\partial a}\int_0^1(f''[x] f[x] + 2 (f'[x])^2 + x f'[x] - 2 a/(1 + a) f[x])^2=0 for a, correct? I believe this is a bit too much for Mathematica, unless you happen to know the exact form of f(x). I do have a couple of tips though:
1. do NOT use Roots unless you're absolutely sure the end equation is a polynomial. This is because Roots works specifically on those, and it cannot solve equations such as x=\sin x or something transcendental. Use NSolve, or, if you're lucky enough to know around which value the solutions are, use FindRoot instead. They both give you a (sometimes unflattened) list of replacement rules (things like {var->value1,var->value2,...}) on which you can use Flatten and my suggestion to find the smallest real part.
2. do NOT use capital letters for variable/function names, anything user-made really. This is because all Mathematica expressions begin with a capital letter, and you may sometimes unintentionally try to use one as a variable, which will most likely result in errors.
3. sometimes you have to make the first couple of steps in order to make the algebraic manipulation possible. In your case, I'm pretty sure you can expand the integrand, then put the derivative under the integral sign (since the lower and upper limits of integration are constants), and differentiate wrt to a. From there though, I'm pretty sure you need the explicit form of f(x) since the integration cannot be performed symbolically.
 
  • Like
Likes member 428835
kontejnjer said:
From what I can tell, you're trying to solve an equation of the kind \frac{\partial}{\partial a}\int_0^1(f''[x] f[x] + 2 (f'[x])^2 + x f'[x] - 2 a/(1 + a) f[x])^2=0 for a, correct? I believe this is a bit too much for Mathematica, unless you happen to know the exact form of f(x). I do have a couple of tips though:
1. do NOT use Roots unless you're absolutely sure the end equation is a polynomial.
Thanks a ton for the tips! I really appreciate it. And yes, ##f(x)## is a quadratic, with the leading coefficient to be determined. The entire code is then
Code:
a = 0;(*CHECK THE a VALUE HERE*)
f[x_] := A x^2 + B x + F
F = -(A + B);
B = -1/2 - 2 A;
ComplexExpand[
Roots[D[Integrate[(
       f''[x] f[x] + 2 (f'[x])^2 + x f'[x] - 2 a/(1 + a) f[x])^2, {x,
      0, 1}], A] == 0, A]]
Although I'll probably change the variables from capital to lowercase. From here, I'm getting three solutions. Is there a technique you are aware of that can find the smallest real value from the three this code produces?
 
Alright, now it's a bit clearer. On the last expression, you can use:
Code:
Min@Part[Flatten@List@ToRules@(expr),All,2]
Also, if you want an approximate numerical solution instead of an exact one, you can use NRoots instead of Roots, or just apply N to the entire expression. If you happen to be dealing with complex numbers, just replace Min@ by Min@Re@ for the smallest real part, and by Min@Im@ for the smallest complex part.

Note: the "@"s are a shorthand notation for [ ], that is, "f@expr" is equivalent to "f[expr]" (it's used to avoid unnecessary brackets).
 
Back
Top