1
$\begingroup$

Suppose three functions :

k1[s_, d_,m_] := 3 s + 5 d + m
k2[s_, d_] := 5 s - 7 d
k3[s_, d_,m_] :=  s + d - m

How to add the following code to plot conditional ki in (s,d) plan while looping over m values for a range, say form -3 to 2 ?

ps = Transpose[{RandomReal[{-2, 2.}, 1000],RandomReal[{-3, 3}, 1000]}];

styleps = Style[{##}, PointSize[.01], Piecewise[{{Blue, 0 < k1[#, #2, -3] <= 
10 && -2 < k2[#, #2] <= 2 && 0 < k3[#, #2, -3] <= 10}}, White]] & @@@ ps;

ListPlot[styleps, DataRange -> {{-1, 1}, {-1, 1}}, Frame -> True, 
GridLines -> {Table[i, {i, 0, 2, 0.1}], Table[i, {i, -1, 3, 0.2}]}, 
ImageSize -> 500, Axes -> False, GridLinesStyle -> Lighter[Gray]]

I think one can use For Loop, but I don't know exactly where it can be implied, any ideas?

$\endgroup$
8
  • $\begingroup$ So, you want one plot for each value of m..? $\endgroup$ – Marius Ladegård Meyer Jun 23 '16 at 6:26
  • $\begingroup$ Yes I want while ListPlot don't fix m at single value .. otherwise give it a rang say from 2 to 10. $\endgroup$ – S.S. Jun 23 '16 at 6:30
  • $\begingroup$ Make styleps a function of m, and put a plots = Table[..., {m, 2, 10}]; around ListPlot...? $\endgroup$ – Marius Ladegård Meyer Jun 23 '16 at 6:39
  • $\begingroup$ May be the first step is a good idea, i.e. styleps[m_], but I think the second is not .. $\endgroup$ – S.S. Jun 23 '16 at 7:00
  • 1
    $\begingroup$ A For loop will be one of the least efficient ways to do it. If you directly want to loop you can use Do[..., {m,2,10}] and then Print[ListPlot[...]] inside the body of Do, but why not just keep them in a Table? Then you can store them, put them in a GraphicsGrid etc. etc. $\endgroup$ – Marius Ladegård Meyer Jun 23 '16 at 11:38
2
$\begingroup$

Try this:

k1[s_, d_, m_] := 3 s + 5 d + m
k2[s_, d_] := 5 s - 7 d
k3[s_, d_, m_] := s + d - m

ps = Transpose[{RandomReal[{-2, 2.}, 1000], 
    RandomReal[{-3, 3}, 1000]}];

styleps[m_] := 
  Style[{##}, PointSize[.01], 
     Piecewise[{{Blue, 
        0 < k1[#, #2, m] <= 10 && -2 < k2[#, #2] <= 2 && 
         0 < k3[#, #2, m] <= 10}}, White]] & @@@ ps;

plots = Table[
   ListPlot[styleps[m], DataRange -> {{-1, 1}, {-1, 1}}, 
    Frame -> True, 
    GridLines -> {Table[i, {i, 0, 2, 0.1}], 
      Table[i, {i, -1, 3, 0.2}]}, ImageSize -> 500, Axes -> False, 
    GridLinesStyle -> Lighter[Gray]], {m, 0, 2, 2/(10 - 1)}];

This gives 10 plots with m varying from 0 to 2. We can visualize it with an animation for instance:

Animate[plots[[u]], {u, 1, Length[tab], 1}]

Should show you something like this:

enter image description here

$\endgroup$
1
  • $\begingroup$ Really thanks guys, you are the best ones :) $\endgroup$ – S.S. Jun 24 '16 at 0:54
6
$\begingroup$

I personally would favour a RegionPlot

Manipulate[RegionPlot[{0 < k1[s, d, m] <= 1, 0 < k2[s, d] <= 2, 
 2 < k3[s, d, m] <= 5}, {s, -1, 1}, {d, -1, 1}, PlotLabel -> m], {m, -5, -1}]

enter image description here

If you want to use ListPlot

col = {Red, Blue, Green};
mm = {-3.5, -2.5, -1.5};
Do[data[m] = {}; 
Do[If[0 < k1[s, d, m] <= 1 && 0 < k2[s, d] <= 2 && 2 < k3[s, d, m] <= 5, 
data[m] = Append[data[m], {s, d}]], {s, -1, 1, 0.01}, {d, -1, 1, 0.01}], {m, mm}]

ListPlot[data[#] & /@ mm, PlotStyle -> col]

enter image description here

Rectifying the method in question

ps = Flatten[Table[{i, j}, {i, 0, 1, 0.05}, {j, 0, 1, 0.05}], 1];

styleps[m_] := Select[ps, (0 < k1[#[[1]], #[[2]], m] <= 10 &&
-2 < k2[#[[1]], #[[2]]] <= 2 && 0 < k3[#[[1]], #[[2]], m] <= 10) &];

ListPlot[{styleps[-3.5], styleps[-1.5]}, PlotMarkers -> Automatic]

enter image description here

$\endgroup$
5
  • $\begingroup$ @ Sumit . Can not we in ListPlot define a range for m -> {-2,2} instead of discrete values ? $\endgroup$ – S.S. Jun 23 '16 at 10:50
  • $\begingroup$ As you see, each m will give you one set. Best you can do is define it in a range with small interval, like Range[-2,2,0.1] $\endgroup$ – Sumit Jun 23 '16 at 11:23
  • $\begingroup$ So how to probably define m in Range in your code ? .. I tried some things but it was going wrong for me $\endgroup$ – S.S. Jun 23 '16 at 11:51
  • $\begingroup$ But just in my code can not we instead setting m = -3 , put m in styleps and then loop or take range on its values? $\endgroup$ – S.S. Jun 23 '16 at 13:02
  • 1
    $\begingroup$ In your code what you are doing is writing the coordinates in blue or white ;) - so it is good as nothing. If you like to use your style, then I would suggest use Select. It will reduce your data size as well and then use with ListPlot. $\endgroup$ – Sumit Jun 23 '16 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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