# Choosing the smallest real part

1. May 13, 2015

### joshmccraney

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!

2. May 13, 2015

### joshmccraney

I should say, what I'm really wanting to do is operate over each of the outputs and take the smallest value.

3. May 14, 2015

### kontejnjer

Assuming you have a (flattened) list of values:
Code (Text):
Min[Re[values]]
Assuming you have a (flattened) list of rules:
Code (Text):
Min[Re[varname/.values[[#]]&/@Range@Length@values]]
A (shorter) alternative would be:
Code (Text):
Min[Re[values[[All, 2]]]]

4. May 16, 2015

### joshmccraney

Thanks so much for the help! Sadly this isn't working, or perhaps I just have no clue! My code is

Code (Text):
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!

5. May 16, 2015

### kontejnjer

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.

6. May 16, 2015

### joshmccraney

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 (Text):

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?

7. May 17, 2015

### kontejnjer

Alright, now it's a bit clearer. On the last expression, you can use:
Code (Text):
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).