2
$\begingroup$

I'm trying to pick/select elements from a list based on another list's value. I haven't been able to implement the solution given to similar questions, such as this and this. Here is a MWE:

Clear["Global`*"]

f[a_, x_] := x^3 + x^2 - x - a
xsol[a_] := x /. NSolve[f[a, x] == 0, x, Reals]
g[a_] := xsol[a]^2 - 20/(5 - xsol[a]) + 30
h[a_] := 5*xsol[a]^3
Manipulate[{xsol[a], g[a], h[a]}, {a, 0.1, 100}]

For each $a$, Manipulate will show 3 lists: $xsol[a]$, $g[a]$ and $h[a]$ . The number of elements in each list depends on $a$ (in this particular example, it's 1 or 3).

The goal is to select elements from those 3 lists based on the value of the corresponding $g[a]$ element being larger than 25.9. For example, for $a=.1$, the three lists are ex ante:

{{-1.58954, -0.0922716, 0.681807}, {29.4915, 26.081, 
  25.8333}, {-20.0808, -0.00392802, 1.58473}}

Considering the selection criterion, I want to select only the first two elements of each list:

{{-1.58954, -0.0922716}, {29.4915, 26.081}, {-20.0808, -0.00392802}}

Now, for each particular value of $a$ (say, $a=.1$), I'm able to do it by testing each element $g[.1][[1]]$, $g[.1][[2]]$ and $g[.1][[3]]$ and selecting appropriately. However, in the context of Manipulate, the number of elements changes as I move the slider across the different values of $a$. For that reason, I'm at a loss on how to code it; as I move the slider, I want only the selected elements to be shown. That is, when $a=.1$ the lists shown must be:

{{-1.58954, -0.0922716}, {29.4915, 26.081}, {-20.0808, -0.00392802}}

and when $a=85$, the lists shown must be empty. Any ideas on how to achieve that?

$\endgroup$
2
  • 2
    $\begingroup$ I get xsol[0.1] = {-1.58954, -0.0922716, 0.681807} and xsol[85]={4.15769}; Also your code keeps manipulating and working till 100 on the slider. The selection criterion "for what" is g[a]>25.9? For a=85, "nothing is shown" or "nothing must be shown". Please clarify. $\endgroup$
    – Syed
    Commented Aug 12, 2021 at 21:41
  • $\begingroup$ Little confused by the manipulation but as far as the list selection: a = {{-1.58954, -0.0922716}, {29.4915, 26.081}, {-20.0808, -0.00392802}} Transpose@Select[Transpose@a, #[[2]] > 25.9 &] $\endgroup$
    – Teabelly
    Commented Aug 12, 2021 at 23:15

1 Answer 1

3
$\begingroup$
Clear["Global`*"]

f[a_, x_] := x^3 + x^2 - x - a
xsol[a_] := x /. NSolve[f[a, x] == 0, x, Reals]
g[a_] := xsol[a]^2 - 20/(5 - xsol[a]) + 30
h[a_] := 5*xsol[a]^3
Manipulate[
 Transpose@
  Select[Transpose@{xsol[a], g[a], h[a]}, #[[2]] > 25.9 &], {a, 0.1, 
  100}]

Is this what you are looking for?

There is quickly only a list with single elements?

$\endgroup$
1
  • $\begingroup$ yes and yes. It's a mwe, but it does convey what I was looking for. What you've proposed solve the problem. $\endgroup$
    – Ararat
    Commented Aug 12, 2021 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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