# How to loop over a varible while plotting a function

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?

• So, you want one plot for each value of m..? Jun 23, 2016 at 6:26
• Yes I want while ListPlot don't fix m at single value .. otherwise give it a rang say from 2 to 10.
– S.S.
Jun 23, 2016 at 6:30
• Make styleps a function of m, and put a plots = Table[..., {m, 2, 10}]; around ListPlot...? Jun 23, 2016 at 6:39
• May be the first step is a good idea, i.e. styleps[m_], but I think the second is not ..
– S.S.
Jun 23, 2016 at 7:00
• 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. Jun 23, 2016 at 11:38

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:

• Really thanks guys, you are the best ones :)
– S.S.
Jun 24, 2016 at 0:54

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}]


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]


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]


• @ Sumit . Can not we in ListPlot define a range for m -> {-2,2} instead of discrete values ?
– S.S.
Jun 23, 2016 at 10:50
• 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] Jun 23, 2016 at 11:23
• So how to probably define m in Range in your code ? .. I tried some things but it was going wrong for me
– S.S.
Jun 23, 2016 at 11:51
• 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?
– S.S.
Jun 23, 2016 at 13:02
• 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. Jun 23, 2016 at 13:47