Unwanted re-evaluation of a variable inside Manipulate












4












$begingroup$


In the below Manipulate expression:



Discretize=Function[{f,steps,x1},Table[f[x],{x,0,x1,Floor[x1/steps]}]];
MakePoints=Function[var,Table[x^2+RandomReal[{-var,var}],{x,0,15,1}]];
Manipulate[
GetDiff = Function[
Total[dta] - Total[mdl]
];
dta = MakePoints[15];
mdl = Discretize[Function[x, τ*x^2], Length[dta] - 1,
Length[dta] - 1];
ListLinePlot[{dta, mdl},
PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
PlotLegends -> {"data", "model"}],
{{τ, 1}, .01, 3, .01},
Dynamic[
diff = GetDiff;
"τ: " <> ToString[τ] <>
"nΣdata: " <> ToString[Total[dta]] <>
"nΣmodel: " <> ToString[Total[mdl]] <>
"nΣdata-Σmodel: " <> ToString[diff]
]
]


Why does varying the parameter seemingly reevaluate dta? I get a constantly changing dta line while I vary the parameter.



Manipulate plot










share|improve this question











$endgroup$

















    4












    $begingroup$


    In the below Manipulate expression:



    Discretize=Function[{f,steps,x1},Table[f[x],{x,0,x1,Floor[x1/steps]}]];
    MakePoints=Function[var,Table[x^2+RandomReal[{-var,var}],{x,0,15,1}]];
    Manipulate[
    GetDiff = Function[
    Total[dta] - Total[mdl]
    ];
    dta = MakePoints[15];
    mdl = Discretize[Function[x, τ*x^2], Length[dta] - 1,
    Length[dta] - 1];
    ListLinePlot[{dta, mdl},
    PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
    PlotLegends -> {"data", "model"}],
    {{τ, 1}, .01, 3, .01},
    Dynamic[
    diff = GetDiff;
    "τ: " <> ToString[τ] <>
    "nΣdata: " <> ToString[Total[dta]] <>
    "nΣmodel: " <> ToString[Total[mdl]] <>
    "nΣdata-Σmodel: " <> ToString[diff]
    ]
    ]


    Why does varying the parameter seemingly reevaluate dta? I get a constantly changing dta line while I vary the parameter.



    Manipulate plot










    share|improve this question











    $endgroup$















      4












      4








      4





      $begingroup$


      In the below Manipulate expression:



      Discretize=Function[{f,steps,x1},Table[f[x],{x,0,x1,Floor[x1/steps]}]];
      MakePoints=Function[var,Table[x^2+RandomReal[{-var,var}],{x,0,15,1}]];
      Manipulate[
      GetDiff = Function[
      Total[dta] - Total[mdl]
      ];
      dta = MakePoints[15];
      mdl = Discretize[Function[x, τ*x^2], Length[dta] - 1,
      Length[dta] - 1];
      ListLinePlot[{dta, mdl},
      PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
      PlotLegends -> {"data", "model"}],
      {{τ, 1}, .01, 3, .01},
      Dynamic[
      diff = GetDiff;
      "τ: " <> ToString[τ] <>
      "nΣdata: " <> ToString[Total[dta]] <>
      "nΣmodel: " <> ToString[Total[mdl]] <>
      "nΣdata-Σmodel: " <> ToString[diff]
      ]
      ]


      Why does varying the parameter seemingly reevaluate dta? I get a constantly changing dta line while I vary the parameter.



      Manipulate plot










      share|improve this question











      $endgroup$




      In the below Manipulate expression:



      Discretize=Function[{f,steps,x1},Table[f[x],{x,0,x1,Floor[x1/steps]}]];
      MakePoints=Function[var,Table[x^2+RandomReal[{-var,var}],{x,0,15,1}]];
      Manipulate[
      GetDiff = Function[
      Total[dta] - Total[mdl]
      ];
      dta = MakePoints[15];
      mdl = Discretize[Function[x, τ*x^2], Length[dta] - 1,
      Length[dta] - 1];
      ListLinePlot[{dta, mdl},
      PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
      PlotLegends -> {"data", "model"}],
      {{τ, 1}, .01, 3, .01},
      Dynamic[
      diff = GetDiff;
      "τ: " <> ToString[τ] <>
      "nΣdata: " <> ToString[Total[dta]] <>
      "nΣmodel: " <> ToString[Total[mdl]] <>
      "nΣdata-Σmodel: " <> ToString[diff]
      ]
      ]


      Why does varying the parameter seemingly reevaluate dta? I get a constantly changing dta line while I vary the parameter.



      Manipulate plot







      manipulate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 30 at 1:14









      m_goldberg

      88.1k872199




      88.1k872199










      asked Jan 29 at 19:11









      pedroospedroos

      1474




      1474






















          3 Answers
          3






          active

          oldest

          votes


















          4












          $begingroup$

          Your MakePoints[ ] function has a RandomReal[ ] function call in it, so it is randomizing each time you move the Manipulate slider. Just move it outside.



          dta = MakePoints[15];

          Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
          (*dta=MakePoints[15];*)
          ...Etc.]


          or you can wrap the internal random call with a BlockRandom[ ]



          Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
          dta = BlockRandom@MakePoints[15];
          .... Etc. ]





          share|improve this answer











          $endgroup$













          • $begingroup$
            Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
            $endgroup$
            – pedroos
            Jan 29 at 19:55






          • 1




            $begingroup$
            When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
            $endgroup$
            – MikeY
            Jan 29 at 20:04





















          6












          $begingroup$

          Your code can be fixed and made much simpler and more efficient, all at the same time. Like so;



          Discretize = Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]];

          MakePoints = Function[var, Table[x^2 + RandomReal[{-var, var}], {x, 0, 15, 1}]];

          SeedRandom[1];
          Manipulate[
          mdl = Discretize[Function[x, τ x^2], Length[dta] - 1, Length[dta] - 1];
          tmdl = Total[mdl];
          Column[{
          ListLinePlot[{dta, mdl},
          PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
          PlotLegends -> {"data", "model"},
          ImageSize -> Medium],
          Row[{"Σdata: ", tdta}],
          Row[{"Σmodel: ", tmdl}],
          Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
          {{dta, MakePoints[15]}, None},
          {{tdta, Total[dta]}, None},
          {mdl, None},
          {tmdl, None},
          {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
          TrackedSymbols :> {τ}]


          demo



          Notes





          1. GetDiff is not needed.

          2. Introducing some local variables with specifications of the form {varspec, None}, which are automatically dynamic, makes for cleaner code and makes it easy to set static values for data and tdta.

          3. Calling MakePoints as an initializer in the specification of dta fixes you problem of unwanted re-evaluation.

          4. Only τ need be tracked, which reduces the load on the front-end.

          5. Introducing Column and Row much simplifies the formatting of the output.

          6. Adding the Appearance -> "Labeled" option to the specification of τ eliminates the need to write code to show τ in the output,

          7. This approach does not require calling Dynamic explicitly anywhere in the Manipulate expression.


          Update



          As usual I didn't stop thinking about this problem after I posted the above code. Eventually, I realized that there were some issues that needed to be addressed:




          1. There is a wired-in dependence on having 15 data points and plotting over a domain of 0 – 15.

          2. The list plot is given only range values and so used the default domain of 1 – 15; it should adjusted to start at zero.

          3. Changing the code to support a user-specified number of data points also requires permitting a user-specified range for the plot.

          4. There an error in way tmdl is initialized that needs fixing.


          The 1st three issues are inherited from the OP's code; the last is my very own.



          Here is the revised code. The modifications are not extensive, but I believe them to be worth posting



          Discretize = Function[{f, xmax}, Table[f[x], {x, 0, xmax}]];
          MakePoints = Function[xmax, Table[x^2 + RandomReal[{-xmax, xmax}], {x, 0, xmax}]];

          SeedRandom[1];
          With[{xmax = 20, ymax = 400},
          Manipulate[
          mdl = Discretize[Function[x, τ x^2], xmax];
          tmdl = Total[mdl];
          Column[
          {ListLinePlot[{dta, mdl},
          DataRange -> {0, xmax},
          PlotRange :> ymax,
          PlotLegends -> {"data", "model"},
          ImageSize -> Medium],
          Row[{"Σdata: ", tdta}],
          Row[{"Σmodel: ", tmdl}],
          Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
          {{dta, MakePoints[xmax]}, None},
          {tdta, None},
          {mdl, None},
          {tmdl, None},
          {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
          Initialization :> (tdta = Total[dta]),
          TrackedSymbols :> {τ}]]


          Here is how things look when dta consists of 20 points.



          demo






          share|improve this answer











          $endgroup$





















            4












            $begingroup$

            Another option using DynamicModule which is the proper tool for interfaces that have local variables:



            DynamicModule[
            {MakePoints, Discretize, dta, tdta, mdl, tmdl},
            Manipulate[
            mdl = Discretize[Function[x, τ x^2], Length[dta] - 1,
            Length[dta] - 1];
            tmdl = Total[mdl];
            Grid[
            {
            {
            ListLinePlot[
            {dta, mdl},
            PlotRange -> {{0, Length[dta] - 1}, {All, 250}},
            PlotLegends -> {"data", "model"}, ImageSize -> Medium
            ],
            SpanFromLeft
            },
            {Subscript["Σ", "data"], ":", tdta},
            {Subscript["Σ", "model"], ":", tmdl},
            {
            Row@{Subscript["Σ", "data"], "-",
            Subscript["Σ", "model"]}, ":",
            tdta - tmdl
            }
            },
            Alignment -> Left
            ],
            {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
            TrackedSymbols :> {τ}
            ],
            Initialization :> {
            MakePoints =
            Function[var, Range[0, 15]^2 + RandomReal[{-var, var}, 16]],
            Discretize =
            Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]],
            dta = MakePoints[15],
            tdta = Total[dta]
            }
            ]





            share|improve this answer









            $endgroup$














              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              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%2f190484%2funwanted-re-evaluation-of-a-variable-inside-manipulate%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4












              $begingroup$

              Your MakePoints[ ] function has a RandomReal[ ] function call in it, so it is randomizing each time you move the Manipulate slider. Just move it outside.



              dta = MakePoints[15];

              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              (*dta=MakePoints[15];*)
              ...Etc.]


              or you can wrap the internal random call with a BlockRandom[ ]



              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              dta = BlockRandom@MakePoints[15];
              .... Etc. ]





              share|improve this answer











              $endgroup$













              • $begingroup$
                Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
                $endgroup$
                – pedroos
                Jan 29 at 19:55






              • 1




                $begingroup$
                When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
                $endgroup$
                – MikeY
                Jan 29 at 20:04


















              4












              $begingroup$

              Your MakePoints[ ] function has a RandomReal[ ] function call in it, so it is randomizing each time you move the Manipulate slider. Just move it outside.



              dta = MakePoints[15];

              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              (*dta=MakePoints[15];*)
              ...Etc.]


              or you can wrap the internal random call with a BlockRandom[ ]



              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              dta = BlockRandom@MakePoints[15];
              .... Etc. ]





              share|improve this answer











              $endgroup$













              • $begingroup$
                Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
                $endgroup$
                – pedroos
                Jan 29 at 19:55






              • 1




                $begingroup$
                When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
                $endgroup$
                – MikeY
                Jan 29 at 20:04
















              4












              4








              4





              $begingroup$

              Your MakePoints[ ] function has a RandomReal[ ] function call in it, so it is randomizing each time you move the Manipulate slider. Just move it outside.



              dta = MakePoints[15];

              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              (*dta=MakePoints[15];*)
              ...Etc.]


              or you can wrap the internal random call with a BlockRandom[ ]



              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              dta = BlockRandom@MakePoints[15];
              .... Etc. ]





              share|improve this answer











              $endgroup$



              Your MakePoints[ ] function has a RandomReal[ ] function call in it, so it is randomizing each time you move the Manipulate slider. Just move it outside.



              dta = MakePoints[15];

              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              (*dta=MakePoints[15];*)
              ...Etc.]


              or you can wrap the internal random call with a BlockRandom[ ]



              Manipulate[GetDiff = Function[Total[dta] - Total[mdl]];
              dta = BlockRandom@MakePoints[15];
              .... Etc. ]






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 29 at 20:04

























              answered Jan 29 at 19:20









              MikeYMikeY

              3,758916




              3,758916












              • $begingroup$
                Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
                $endgroup$
                – pedroos
                Jan 29 at 19:55






              • 1




                $begingroup$
                When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
                $endgroup$
                – MikeY
                Jan 29 at 20:04




















              • $begingroup$
                Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
                $endgroup$
                – pedroos
                Jan 29 at 19:55






              • 1




                $begingroup$
                When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
                $endgroup$
                – MikeY
                Jan 29 at 20:04


















              $begingroup$
              Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
              $endgroup$
              – pedroos
              Jan 29 at 19:55




              $begingroup$
              Can I ask if there is a way to not re-evaluate a variable that's independent from the parameter being manipulated? Thanks.
              $endgroup$
              – pedroos
              Jan 29 at 19:55




              1




              1




              $begingroup$
              When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
              $endgroup$
              – MikeY
              Jan 29 at 20:04






              $begingroup$
              When you use Manipulate, the internals of the body of the command get evaluated no matter which parameter you are manipulating, so you have to use some tricks to suppress the random call. Somebody else smarter than I am may have a solution.
              $endgroup$
              – MikeY
              Jan 29 at 20:04













              6












              $begingroup$

              Your code can be fixed and made much simpler and more efficient, all at the same time. Like so;



              Discretize = Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]];

              MakePoints = Function[var, Table[x^2 + RandomReal[{-var, var}], {x, 0, 15, 1}]];

              SeedRandom[1];
              Manipulate[
              mdl = Discretize[Function[x, τ x^2], Length[dta] - 1, Length[dta] - 1];
              tmdl = Total[mdl];
              Column[{
              ListLinePlot[{dta, mdl},
              PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
              PlotLegends -> {"data", "model"},
              ImageSize -> Medium],
              Row[{"Σdata: ", tdta}],
              Row[{"Σmodel: ", tmdl}],
              Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
              {{dta, MakePoints[15]}, None},
              {{tdta, Total[dta]}, None},
              {mdl, None},
              {tmdl, None},
              {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
              TrackedSymbols :> {τ}]


              demo



              Notes





              1. GetDiff is not needed.

              2. Introducing some local variables with specifications of the form {varspec, None}, which are automatically dynamic, makes for cleaner code and makes it easy to set static values for data and tdta.

              3. Calling MakePoints as an initializer in the specification of dta fixes you problem of unwanted re-evaluation.

              4. Only τ need be tracked, which reduces the load on the front-end.

              5. Introducing Column and Row much simplifies the formatting of the output.

              6. Adding the Appearance -> "Labeled" option to the specification of τ eliminates the need to write code to show τ in the output,

              7. This approach does not require calling Dynamic explicitly anywhere in the Manipulate expression.


              Update



              As usual I didn't stop thinking about this problem after I posted the above code. Eventually, I realized that there were some issues that needed to be addressed:




              1. There is a wired-in dependence on having 15 data points and plotting over a domain of 0 – 15.

              2. The list plot is given only range values and so used the default domain of 1 – 15; it should adjusted to start at zero.

              3. Changing the code to support a user-specified number of data points also requires permitting a user-specified range for the plot.

              4. There an error in way tmdl is initialized that needs fixing.


              The 1st three issues are inherited from the OP's code; the last is my very own.



              Here is the revised code. The modifications are not extensive, but I believe them to be worth posting



              Discretize = Function[{f, xmax}, Table[f[x], {x, 0, xmax}]];
              MakePoints = Function[xmax, Table[x^2 + RandomReal[{-xmax, xmax}], {x, 0, xmax}]];

              SeedRandom[1];
              With[{xmax = 20, ymax = 400},
              Manipulate[
              mdl = Discretize[Function[x, τ x^2], xmax];
              tmdl = Total[mdl];
              Column[
              {ListLinePlot[{dta, mdl},
              DataRange -> {0, xmax},
              PlotRange :> ymax,
              PlotLegends -> {"data", "model"},
              ImageSize -> Medium],
              Row[{"Σdata: ", tdta}],
              Row[{"Σmodel: ", tmdl}],
              Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
              {{dta, MakePoints[xmax]}, None},
              {tdta, None},
              {mdl, None},
              {tmdl, None},
              {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
              Initialization :> (tdta = Total[dta]),
              TrackedSymbols :> {τ}]]


              Here is how things look when dta consists of 20 points.



              demo






              share|improve this answer











              $endgroup$


















                6












                $begingroup$

                Your code can be fixed and made much simpler and more efficient, all at the same time. Like so;



                Discretize = Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]];

                MakePoints = Function[var, Table[x^2 + RandomReal[{-var, var}], {x, 0, 15, 1}]];

                SeedRandom[1];
                Manipulate[
                mdl = Discretize[Function[x, τ x^2], Length[dta] - 1, Length[dta] - 1];
                tmdl = Total[mdl];
                Column[{
                ListLinePlot[{dta, mdl},
                PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
                PlotLegends -> {"data", "model"},
                ImageSize -> Medium],
                Row[{"Σdata: ", tdta}],
                Row[{"Σmodel: ", tmdl}],
                Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
                {{dta, MakePoints[15]}, None},
                {{tdta, Total[dta]}, None},
                {mdl, None},
                {tmdl, None},
                {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                TrackedSymbols :> {τ}]


                demo



                Notes





                1. GetDiff is not needed.

                2. Introducing some local variables with specifications of the form {varspec, None}, which are automatically dynamic, makes for cleaner code and makes it easy to set static values for data and tdta.

                3. Calling MakePoints as an initializer in the specification of dta fixes you problem of unwanted re-evaluation.

                4. Only τ need be tracked, which reduces the load on the front-end.

                5. Introducing Column and Row much simplifies the formatting of the output.

                6. Adding the Appearance -> "Labeled" option to the specification of τ eliminates the need to write code to show τ in the output,

                7. This approach does not require calling Dynamic explicitly anywhere in the Manipulate expression.


                Update



                As usual I didn't stop thinking about this problem after I posted the above code. Eventually, I realized that there were some issues that needed to be addressed:




                1. There is a wired-in dependence on having 15 data points and plotting over a domain of 0 – 15.

                2. The list plot is given only range values and so used the default domain of 1 – 15; it should adjusted to start at zero.

                3. Changing the code to support a user-specified number of data points also requires permitting a user-specified range for the plot.

                4. There an error in way tmdl is initialized that needs fixing.


                The 1st three issues are inherited from the OP's code; the last is my very own.



                Here is the revised code. The modifications are not extensive, but I believe them to be worth posting



                Discretize = Function[{f, xmax}, Table[f[x], {x, 0, xmax}]];
                MakePoints = Function[xmax, Table[x^2 + RandomReal[{-xmax, xmax}], {x, 0, xmax}]];

                SeedRandom[1];
                With[{xmax = 20, ymax = 400},
                Manipulate[
                mdl = Discretize[Function[x, τ x^2], xmax];
                tmdl = Total[mdl];
                Column[
                {ListLinePlot[{dta, mdl},
                DataRange -> {0, xmax},
                PlotRange :> ymax,
                PlotLegends -> {"data", "model"},
                ImageSize -> Medium],
                Row[{"Σdata: ", tdta}],
                Row[{"Σmodel: ", tmdl}],
                Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
                {{dta, MakePoints[xmax]}, None},
                {tdta, None},
                {mdl, None},
                {tmdl, None},
                {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                Initialization :> (tdta = Total[dta]),
                TrackedSymbols :> {τ}]]


                Here is how things look when dta consists of 20 points.



                demo






                share|improve this answer











                $endgroup$
















                  6












                  6








                  6





                  $begingroup$

                  Your code can be fixed and made much simpler and more efficient, all at the same time. Like so;



                  Discretize = Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]];

                  MakePoints = Function[var, Table[x^2 + RandomReal[{-var, var}], {x, 0, 15, 1}]];

                  SeedRandom[1];
                  Manipulate[
                  mdl = Discretize[Function[x, τ x^2], Length[dta] - 1, Length[dta] - 1];
                  tmdl = Total[mdl];
                  Column[{
                  ListLinePlot[{dta, mdl},
                  PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
                  PlotLegends -> {"data", "model"},
                  ImageSize -> Medium],
                  Row[{"Σdata: ", tdta}],
                  Row[{"Σmodel: ", tmdl}],
                  Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
                  {{dta, MakePoints[15]}, None},
                  {{tdta, Total[dta]}, None},
                  {mdl, None},
                  {tmdl, None},
                  {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                  TrackedSymbols :> {τ}]


                  demo



                  Notes





                  1. GetDiff is not needed.

                  2. Introducing some local variables with specifications of the form {varspec, None}, which are automatically dynamic, makes for cleaner code and makes it easy to set static values for data and tdta.

                  3. Calling MakePoints as an initializer in the specification of dta fixes you problem of unwanted re-evaluation.

                  4. Only τ need be tracked, which reduces the load on the front-end.

                  5. Introducing Column and Row much simplifies the formatting of the output.

                  6. Adding the Appearance -> "Labeled" option to the specification of τ eliminates the need to write code to show τ in the output,

                  7. This approach does not require calling Dynamic explicitly anywhere in the Manipulate expression.


                  Update



                  As usual I didn't stop thinking about this problem after I posted the above code. Eventually, I realized that there were some issues that needed to be addressed:




                  1. There is a wired-in dependence on having 15 data points and plotting over a domain of 0 – 15.

                  2. The list plot is given only range values and so used the default domain of 1 – 15; it should adjusted to start at zero.

                  3. Changing the code to support a user-specified number of data points also requires permitting a user-specified range for the plot.

                  4. There an error in way tmdl is initialized that needs fixing.


                  The 1st three issues are inherited from the OP's code; the last is my very own.



                  Here is the revised code. The modifications are not extensive, but I believe them to be worth posting



                  Discretize = Function[{f, xmax}, Table[f[x], {x, 0, xmax}]];
                  MakePoints = Function[xmax, Table[x^2 + RandomReal[{-xmax, xmax}], {x, 0, xmax}]];

                  SeedRandom[1];
                  With[{xmax = 20, ymax = 400},
                  Manipulate[
                  mdl = Discretize[Function[x, τ x^2], xmax];
                  tmdl = Total[mdl];
                  Column[
                  {ListLinePlot[{dta, mdl},
                  DataRange -> {0, xmax},
                  PlotRange :> ymax,
                  PlotLegends -> {"data", "model"},
                  ImageSize -> Medium],
                  Row[{"Σdata: ", tdta}],
                  Row[{"Σmodel: ", tmdl}],
                  Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
                  {{dta, MakePoints[xmax]}, None},
                  {tdta, None},
                  {mdl, None},
                  {tmdl, None},
                  {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                  Initialization :> (tdta = Total[dta]),
                  TrackedSymbols :> {τ}]]


                  Here is how things look when dta consists of 20 points.



                  demo






                  share|improve this answer











                  $endgroup$



                  Your code can be fixed and made much simpler and more efficient, all at the same time. Like so;



                  Discretize = Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]];

                  MakePoints = Function[var, Table[x^2 + RandomReal[{-var, var}], {x, 0, 15, 1}]];

                  SeedRandom[1];
                  Manipulate[
                  mdl = Discretize[Function[x, τ x^2], Length[dta] - 1, Length[dta] - 1];
                  tmdl = Total[mdl];
                  Column[{
                  ListLinePlot[{dta, mdl},
                  PlotRange -> {{0, Length[dta] - 1}, {0, 250}},
                  PlotLegends -> {"data", "model"},
                  ImageSize -> Medium],
                  Row[{"Σdata: ", tdta}],
                  Row[{"Σmodel: ", tmdl}],
                  Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
                  {{dta, MakePoints[15]}, None},
                  {{tdta, Total[dta]}, None},
                  {mdl, None},
                  {tmdl, None},
                  {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                  TrackedSymbols :> {τ}]


                  demo



                  Notes





                  1. GetDiff is not needed.

                  2. Introducing some local variables with specifications of the form {varspec, None}, which are automatically dynamic, makes for cleaner code and makes it easy to set static values for data and tdta.

                  3. Calling MakePoints as an initializer in the specification of dta fixes you problem of unwanted re-evaluation.

                  4. Only τ need be tracked, which reduces the load on the front-end.

                  5. Introducing Column and Row much simplifies the formatting of the output.

                  6. Adding the Appearance -> "Labeled" option to the specification of τ eliminates the need to write code to show τ in the output,

                  7. This approach does not require calling Dynamic explicitly anywhere in the Manipulate expression.


                  Update



                  As usual I didn't stop thinking about this problem after I posted the above code. Eventually, I realized that there were some issues that needed to be addressed:




                  1. There is a wired-in dependence on having 15 data points and plotting over a domain of 0 – 15.

                  2. The list plot is given only range values and so used the default domain of 1 – 15; it should adjusted to start at zero.

                  3. Changing the code to support a user-specified number of data points also requires permitting a user-specified range for the plot.

                  4. There an error in way tmdl is initialized that needs fixing.


                  The 1st three issues are inherited from the OP's code; the last is my very own.



                  Here is the revised code. The modifications are not extensive, but I believe them to be worth posting



                  Discretize = Function[{f, xmax}, Table[f[x], {x, 0, xmax}]];
                  MakePoints = Function[xmax, Table[x^2 + RandomReal[{-xmax, xmax}], {x, 0, xmax}]];

                  SeedRandom[1];
                  With[{xmax = 20, ymax = 400},
                  Manipulate[
                  mdl = Discretize[Function[x, τ x^2], xmax];
                  tmdl = Total[mdl];
                  Column[
                  {ListLinePlot[{dta, mdl},
                  DataRange -> {0, xmax},
                  PlotRange :> ymax,
                  PlotLegends -> {"data", "model"},
                  ImageSize -> Medium],
                  Row[{"Σdata: ", tdta}],
                  Row[{"Σmodel: ", tmdl}],
                  Row[{"Σdata-Σmodel: ", tdta - tmdl}]}],
                  {{dta, MakePoints[xmax]}, None},
                  {tdta, None},
                  {mdl, None},
                  {tmdl, None},
                  {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                  Initialization :> (tdta = Total[dta]),
                  TrackedSymbols :> {τ}]]


                  Here is how things look when dta consists of 20 points.



                  demo







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 30 at 7:20

























                  answered Jan 29 at 23:40









                  m_goldbergm_goldberg

                  88.1k872199




                  88.1k872199























                      4












                      $begingroup$

                      Another option using DynamicModule which is the proper tool for interfaces that have local variables:



                      DynamicModule[
                      {MakePoints, Discretize, dta, tdta, mdl, tmdl},
                      Manipulate[
                      mdl = Discretize[Function[x, τ x^2], Length[dta] - 1,
                      Length[dta] - 1];
                      tmdl = Total[mdl];
                      Grid[
                      {
                      {
                      ListLinePlot[
                      {dta, mdl},
                      PlotRange -> {{0, Length[dta] - 1}, {All, 250}},
                      PlotLegends -> {"data", "model"}, ImageSize -> Medium
                      ],
                      SpanFromLeft
                      },
                      {Subscript["Σ", "data"], ":", tdta},
                      {Subscript["Σ", "model"], ":", tmdl},
                      {
                      Row@{Subscript["Σ", "data"], "-",
                      Subscript["Σ", "model"]}, ":",
                      tdta - tmdl
                      }
                      },
                      Alignment -> Left
                      ],
                      {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                      TrackedSymbols :> {τ}
                      ],
                      Initialization :> {
                      MakePoints =
                      Function[var, Range[0, 15]^2 + RandomReal[{-var, var}, 16]],
                      Discretize =
                      Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]],
                      dta = MakePoints[15],
                      tdta = Total[dta]
                      }
                      ]





                      share|improve this answer









                      $endgroup$


















                        4












                        $begingroup$

                        Another option using DynamicModule which is the proper tool for interfaces that have local variables:



                        DynamicModule[
                        {MakePoints, Discretize, dta, tdta, mdl, tmdl},
                        Manipulate[
                        mdl = Discretize[Function[x, τ x^2], Length[dta] - 1,
                        Length[dta] - 1];
                        tmdl = Total[mdl];
                        Grid[
                        {
                        {
                        ListLinePlot[
                        {dta, mdl},
                        PlotRange -> {{0, Length[dta] - 1}, {All, 250}},
                        PlotLegends -> {"data", "model"}, ImageSize -> Medium
                        ],
                        SpanFromLeft
                        },
                        {Subscript["Σ", "data"], ":", tdta},
                        {Subscript["Σ", "model"], ":", tmdl},
                        {
                        Row@{Subscript["Σ", "data"], "-",
                        Subscript["Σ", "model"]}, ":",
                        tdta - tmdl
                        }
                        },
                        Alignment -> Left
                        ],
                        {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                        TrackedSymbols :> {τ}
                        ],
                        Initialization :> {
                        MakePoints =
                        Function[var, Range[0, 15]^2 + RandomReal[{-var, var}, 16]],
                        Discretize =
                        Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]],
                        dta = MakePoints[15],
                        tdta = Total[dta]
                        }
                        ]





                        share|improve this answer









                        $endgroup$
















                          4












                          4








                          4





                          $begingroup$

                          Another option using DynamicModule which is the proper tool for interfaces that have local variables:



                          DynamicModule[
                          {MakePoints, Discretize, dta, tdta, mdl, tmdl},
                          Manipulate[
                          mdl = Discretize[Function[x, τ x^2], Length[dta] - 1,
                          Length[dta] - 1];
                          tmdl = Total[mdl];
                          Grid[
                          {
                          {
                          ListLinePlot[
                          {dta, mdl},
                          PlotRange -> {{0, Length[dta] - 1}, {All, 250}},
                          PlotLegends -> {"data", "model"}, ImageSize -> Medium
                          ],
                          SpanFromLeft
                          },
                          {Subscript["Σ", "data"], ":", tdta},
                          {Subscript["Σ", "model"], ":", tmdl},
                          {
                          Row@{Subscript["Σ", "data"], "-",
                          Subscript["Σ", "model"]}, ":",
                          tdta - tmdl
                          }
                          },
                          Alignment -> Left
                          ],
                          {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                          TrackedSymbols :> {τ}
                          ],
                          Initialization :> {
                          MakePoints =
                          Function[var, Range[0, 15]^2 + RandomReal[{-var, var}, 16]],
                          Discretize =
                          Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]],
                          dta = MakePoints[15],
                          tdta = Total[dta]
                          }
                          ]





                          share|improve this answer









                          $endgroup$



                          Another option using DynamicModule which is the proper tool for interfaces that have local variables:



                          DynamicModule[
                          {MakePoints, Discretize, dta, tdta, mdl, tmdl},
                          Manipulate[
                          mdl = Discretize[Function[x, τ x^2], Length[dta] - 1,
                          Length[dta] - 1];
                          tmdl = Total[mdl];
                          Grid[
                          {
                          {
                          ListLinePlot[
                          {dta, mdl},
                          PlotRange -> {{0, Length[dta] - 1}, {All, 250}},
                          PlotLegends -> {"data", "model"}, ImageSize -> Medium
                          ],
                          SpanFromLeft
                          },
                          {Subscript["Σ", "data"], ":", tdta},
                          {Subscript["Σ", "model"], ":", tmdl},
                          {
                          Row@{Subscript["Σ", "data"], "-",
                          Subscript["Σ", "model"]}, ":",
                          tdta - tmdl
                          }
                          },
                          Alignment -> Left
                          ],
                          {{τ, 1}, .01, 3, .01, Appearance -> "Labeled"},
                          TrackedSymbols :> {τ}
                          ],
                          Initialization :> {
                          MakePoints =
                          Function[var, Range[0, 15]^2 + RandomReal[{-var, var}, 16]],
                          Discretize =
                          Function[{f, steps, x1}, Table[f[x], {x, 0, x1, Floor[x1/steps]}]],
                          dta = MakePoints[15],
                          tdta = Total[dta]
                          }
                          ]






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 30 at 4:36









                          b3m2a1b3m2a1

                          28.5k359164




                          28.5k359164






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Mathematica Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f190484%2funwanted-re-evaluation-of-a-variable-inside-manipulate%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith