I have the following code:


K1 = 12;
K3 = 195/10;
eo = 885/100;
ea = 145/10;
L = 1;

ele[E2_] = -(1/(2 K3)) L (E2^2 ea eo Sin[2 g[z]] - (K1 - K3) Sin[
       2 g[z]] g'[z]^2 + (K1 + K3 + (K1 - K3) Cos[2 g[z]]) g''[z]) // 

pnd = ParametricNDSolveValue[{ele[E2] == 0, g[0] == 0, g[L] == 0}, 
  g, {z, 0, L}, {E2, gp0}, 
  Method -> 
   "BoundaryValues" -> {"Shooting", 
     "StartingInitialConditions" -> {g[0] == 0, g'[0] == gp0}}]

Plot[Evaluate@Table[pnd[E2, gp0][z], {gp0, 0, 15, 0.05}, {E2, 0, 2}],
 {z, 0, L},
 PlotRange -> All]

the output is:

enter image description here

I want to have only the plots with circles (I edit the output and put the circles just to indicate what I want) such as for values=0 or values>0 for {z=0,1} and store it in another table.

I tried to run a if statement on the

aa=Evaluate[Table[pnd[E2, gp0][z], {gp0, 0, 15, 0.05}, {E2, 0, 2}],
     {z, 0, L}]

such that for aa >=0 from {z=0,L} it can store in an another table.

I want the data series, in a table, for only those curves which lie entirely above the x-axis

  • $\begingroup$ The "circles" are regions within your plot area z, pnd ? Which conditions define these circles? $\endgroup$ Jan 18, 2023 at 14:07
  • $\begingroup$ @UlrichNeumann No, I just edited the picture, so my question should be clear. I want only the plots which are circled by me in windows paint. $\endgroup$
    – a019
    Jan 18, 2023 at 14:09
  • $\begingroup$ Perhaps option PlotRange->{0,Automatic} gives what you're looking for? $\endgroup$ Jan 18, 2023 at 14:11
  • $\begingroup$ @UlrichNeumann No, I already tried, I want only the table with encircled plot lines. $\endgroup$
    – a019
    Jan 18, 2023 at 14:16
  • $\begingroup$ @HighPerformanceMark Yes, I'll edit the question as well. $\endgroup$
    – a019
    Jan 18, 2023 at 14:27

2 Answers 2


We may calculate a small number of points for each function and then determine to which group it belongs.

First, store all your function in a table like:

funs=Flatten[Table[pnd[E2, gp0][z], {gp0, 0, 15, 0.05}, {E2, 0, 2}]];

To determine which functions result in zero, we must define an interval because machine numbers are not exact. I arbitrarily choose eps=10^-30. Now we may select the functions that are equal to zero and those above zeros:

eps = 10^-30.;
res = Reap[
   Do[mima = MinMax[Table[funs[[i]], {z, 0, 1, 0.2}]]; 
    Which[-eps < mima[[1]] < eps, Sow[i, x1],
     -eps < mima[[1]] && eps < mima[[2]], Sow[i, x2]
    , {i, Length[funs]}]

"res" contains now two lists, the first with indices of functions that are zero, the second from functions above zero. Here is the result:

enter image description here


To get only the index of the first function in each group, we may proceed as:

We first choose a x-value where all the functions differ, e.g.: 0.2. Then we get all the function values there and delete duplicates. Then we get the position of the first occurrence of each value.

d02 = funs /. z -> 0.2;
vals = DeleteDuplicates[Select[d02, NonNegative], 
   Abs[#1 - #2] < 10^-10 &];
Position[d02, #, 1, 1] & /@ vals // Flatten

(* {1, 38, 75, 339} *)
  • $\begingroup$ Thanks! it is working other way around : "res" contains now two lists, the first with indices of functions that are zero and above zero, the second from functions below zero. $\endgroup$
    – a019
    Jan 20, 2023 at 10:14
  • 1
    $\begingroup$ @Muhammed Ali Oh, nobody is perfect $\endgroup$ Jan 20, 2023 at 12:00
  • $\begingroup$ Thanks! exactly, is there any way we can sort the list greater than zero in an increasing order. So, the very first output with zero is selected then all the rest outputs of zeros are discarded and the next greater than zero is sorted and all other equal to this same output is discarded. So, in the end, I will have only three outputs for E2=0, E2=1 and E2=3. The indices are 1, 38, 414 (if we check manually).. $\endgroup$
    – a019
    Jan 20, 2023 at 12:09
  • 1
    $\begingroup$ Look what I have added, hope I understand you correctly. $\endgroup$ Jan 20, 2023 at 13:31
  • $\begingroup$ Thanks! just a comment, the eps=10^-40 and in vals, 10^-0.8 works fine. As, in the output, number of indices and E2 should be equal and the above two parameters works fine for E2, 0,1,2. $\endgroup$
    – a019
    Jan 20, 2023 at 14:03

Here's a way to investigate the solutions. In ParametricNDSolveValue, the parameters can be specified in the function description in the same order as the parameters supplied to ParametricNDSolveValue. So the solutions are pnd[E2,gp0][z]. You can start by just viewing the solutions one by one as E2 and gp0 are varied in a Manipulate construct and just pick out the ones you want manually. Next version you can check the values of the functions and pick them out programatically. Below is the first step:

gpTable = Table[gVal, {gVal, 0, 15, 1/4}];
 Plot[pnd[E2, gpTable[[gIndex]]][z], {z, 0, L}, 
  PlotRange -> {{0, 1}, {-2, 2}}, PlotStyle -> Red],
 {{gIndex, 0}, 0, Length@gpTable, 1}, {{E2, 0}, 0, 2, 1}
  • $\begingroup$ Thanks! I was able to check and pick them out one by one manually. However, this can not be the solution as I will change the parameters K1, K3 and ea and this will change everything again and I have to do the whole thing manually again. Next Version? $\endgroup$
    – a019
    Jan 19, 2023 at 8:27

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.