# Plotting results from two nested For-loops

I have two nested For-loops in my code. I would like all the results to be in one table, as shown below. My problem is that I want to print with ListPlot from this table all the solutions in one graph, but with k as parameter, i.e., to have 10 different graphs in one. I think that the solution would be to save k like in an excel I would save it in separate sheets, but I don't find a way to do this. Could somebody help me with my idea or suggest something else? Thanks!

Results = {};
For[k = 1, k < 11, k = k + 1,
For[b = 1, b < 5, b = b + 0.5,
sol = NSolve[2 k + b - c - 10 == 0, c];
AppendTo[Results, Flatten@{k, b, c /. sol}];]]

Results

• Just sayin': using For is almost always the wrong way to approach a solution in Mathematica. Although it does exist in the language, importing code of this paradigm from other languages is very likely to just complicate your life with no additional benefit. Commented Sep 11, 2018 at 14:39

Here is a solution using Table:

tb = Table[{k, b, c /. NSolve[2 k + b - c - 10 == 0, c][[1]]}, {k, 1,10}, {b, 1, 5, .5}]
Map[#[[All, 2 ;; 3]] &, tb];
ListPlot[%, Joined -> True]


Use Solve (or NSolve) once to get a symbolic solution:

soln = Solve[2 k + b - c - 10 == 0, c][[1]]


{c -> -10 + b + 2 k}

results = Table[{k, b, c /. soln}, {k, 1, 10}, {b, 1, 4.5, .5}];

ListPlot[results[[All,All,{2,3}]],
Joined->True, PlotMarkers->Automatic,
PlotLegends -> ("k = " <> ToString[#] & /@ results[[All,1,1]])]


Try this:

 lst = Flatten[Table[{k, b, Solve[2 k + b - c - 10 == 0, c][[1, 1, 2]]}, {k, 1,
10}, {b, 1, 4}], 1];
Clear[x,y,z];
Manipulate[
lst1 = Select[lst, #[[1]] == k &] /. {x_, y_, z_} -> {y, z};
ListPlot[lst1, PlotRange -> {-7.5, 14.5}], {k, Range[10], SetterBar}]


yielding this:

Have fun!