Plotting results from two nested For-loops
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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
plotting list-manipulation
add a comment |Â
up vote
3
down vote
favorite
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
plotting list-manipulation
1
Just sayin': usingFor
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.
– kirma
yesterday
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
plotting list-manipulation
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
plotting list-manipulation
plotting list-manipulation
edited yesterday


m_goldberg
81.8k869190
81.8k869190
asked yesterday
harazogo
483
483
1
Just sayin': usingFor
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.
– kirma
yesterday
add a comment |Â
1
Just sayin': usingFor
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.
– kirma
yesterday
1
1
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.– kirma
yesterday
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.– kirma
yesterday
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
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]
add a comment |Â
up vote
3
down vote
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]])]
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
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]
add a comment |Â
up vote
3
down vote
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]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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]
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]
answered yesterday
Ulrich Neumann
4,787413
4,787413
add a comment |Â
add a comment |Â
up vote
3
down vote
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]])]
add a comment |Â
up vote
3
down vote
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]])]
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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]])]
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]])]
edited yesterday
answered yesterday
kglr
159k8184384
159k8184384
add a comment |Â
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
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!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
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!
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!
answered yesterday


Alexei Boulbitch
20.3k2369
20.3k2369
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181686%2fplotting-results-from-two-nested-for-loops%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
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.– kirma
yesterday