1

I have a code which goes like this:

ClearAll["Global`*"]
tbl = Table[{x, N[x^2]}, {x, 0.1, 10, 0.1}];
const1 = 2;
const2 = 1.2;
newrow[{k_, r1_}] := {k, r1, (k + r1) 50/const2, (k + r1) 1.6/2.6};
newtbl = Map[newrow, tbl];

Export["file" <> ToString[const1] <> ".dat", newtbl];

rowlength = Length[newtbl];
xaxis = Table[newtbl[[i, 1]], {i, 1, rowlength}];
yaxis1 = Table[newtbl[[i, 2]], {i, 1, rowlength}];
ListLinePlot[Transpose[{xaxis, #}] & /@ {yaxis1}, 
 AxesStyle -> Directive[GrayLevel[0], AbsoluteThickness[1.6]], 
 PlotLabel -> None, LabelStyle -> {16, GrayLevel[0]}, 
 AxesLabel -> {"k\[Rho]", "\[Omega]"}, PlotRange -> Full]

The output graph looks like this:

enter image description here

In the exported file one can see that there are 4 columns and I am plotting 1st and 2nd column. Now, I would like to plot with certain condition that has to be imposed. E.g. I want to plot 2nd colum only when

  1. the third column value is between 1000 and 2000, and also
  2. the fourth column value is greater than 10 (it can go upto any value)

I want both this condition to be satisfied simultaneously and generate plot. In this case one can see that condition gets satisfied only when the first column has value in between 4.5 to 6.4.How can this be accomplished?

The solution that is described here does not serve the purpose.

What should I do?

Thanks in advance.

sreeraj t
  • 303
  • 1
  • 10

1 Answers1

2

First, let's create your table in a simpler way.

row[{k_, r1_}] := {k, r1, (k + r1) 50/1.2, (k + r1) 1.6/2.6}
tbl = Table[row@{x, x^2}, {x, 0.1, 10., 0.1}];

Now let's filter the table on the 3rd item of each tuple.

Cases[tbl, {x_, y_, p_ /; LessEqual[1000., p, 2000], q_} -> {x, y, p, q}]
{{4.5, 20.25, 1031.25, 15.2308}, {4.6, 21.16, 1073.33, 15.8523}, 
 {4.7, 22.09, 1116.25, 16.4862}, {4.8, 23.04, 1160., 17.1323}, 
 {4.9, 24.01, 1204.58, 17.7908}, {5., 25., 1250., 18.4615}, 
 {5.1, 26.01, 1296.25, 19.1446}, {5.2, 27.04, 1343.33, 19.84}, 
 {5.3, 28.09, 1391.25, 20.5477}, {5.4, 29.16, 1440., 21.2677}, 
 {5.5, 30.25, 1489.58, 22.}, {5.6, 31.36, 1540., 22.7446}, 
 {5.7, 32.49, 1591.25, 23.5015}, {5.8, 33.64, 1643.33, 24.2708}, 
 {5.9, 34.81, 1696.25, 25.0523}, {6., 36., 1750., 25.8462}, 
 {6.1, 37.21, 1804.58, 26.6523}, {6.2, 38.44, 1860., 27.4708}, 
 {6.3, 39.69, 1916.25, 28.3015}, {6.4, 40.96, 1973.33, 29.1446}}

Note that in every case the 4th item is greater than 10., so there is no need to filter on the 4th item (if there were, we write q_ /; q > 10). Consequently, we make the plot with:

data = Cases[tbl, {x_, y_, p_ /; LessEqual[1000., p, 2000], _} -> {x, y}];
ListLinePlot[data,
  Mesh -> Full,
  AxesLabel -> {"kρ", "ω"},
  ImageSize -> Large]

plot

m_goldberg
  • 107,779
  • 16
  • 103
  • 257