Plotting results from two nested For-loops

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












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









share|improve this question



















  • 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














up vote
3
down vote

favorite
1












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









share|improve this question



















  • 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












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









m_goldberg

81.8k869190




81.8k869190










asked yesterday









harazogo

483




483







  • 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












  • 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







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










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]


enter image description here






share|improve this answer



























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


    enter image description here






    share|improve this answer





























      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:



      enter image description here



      Have fun!






      share|improve this answer




















        Your Answer




        StackExchange.ifUsing("editor", function ()
        return StackExchange.using("mathjaxEditing", function ()
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        );
        );
        , "mathjax-editing");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "387"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













         

        draft saved


        draft discarded


















        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






























        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]


        enter image description here






        share|improve this answer
























          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]


          enter image description here






          share|improve this answer






















            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]


            enter image description here






            share|improve this answer












            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]


            enter image description here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered yesterday









            Ulrich Neumann

            4,787413




            4,787413




















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


                enter image description here






                share|improve this answer


























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


                  enter image description here






                  share|improve this answer
























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


                    enter image description here






                    share|improve this answer














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


                    enter image description here







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited yesterday

























                    answered yesterday









                    kglr

                    159k8184384




                    159k8184384




















                        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:



                        enter image description here



                        Have fun!






                        share|improve this answer
























                          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:



                          enter image description here



                          Have fun!






                          share|improve this answer






















                            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:



                            enter image description here



                            Have fun!






                            share|improve this answer












                            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:



                            enter image description here



                            Have fun!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            Alexei Boulbitch

                            20.3k2369




                            20.3k2369



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                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













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                Confectionery