Basic substitution

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











up vote
1
down vote

favorite












I am relatively new to Mathematica, so I am sure this is basic, but I lack the basic terminology to be able to google it.



How can I make this command work?



Plot[x / a, x, 0, a] /. a->10


I would expect this to plot the same line for all values of a. But I get



Plot::plln: Limiting value a in Charting`Private`pvar$10408,0,a is not a machine-sized real number.


I understand that Plot expects numbers not variables, but what is the correct syntax to resolve this since a is ultimately a number?










share|improve this question









New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    I wonder whether "to google sth" really should replace "to look sth up"? Are we getting paid for that kind of word of mouth advertising - especially for a monopolist? ;-)
    – gwr
    3 hours ago















up vote
1
down vote

favorite












I am relatively new to Mathematica, so I am sure this is basic, but I lack the basic terminology to be able to google it.



How can I make this command work?



Plot[x / a, x, 0, a] /. a->10


I would expect this to plot the same line for all values of a. But I get



Plot::plln: Limiting value a in Charting`Private`pvar$10408,0,a is not a machine-sized real number.


I understand that Plot expects numbers not variables, but what is the correct syntax to resolve this since a is ultimately a number?










share|improve this question









New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    I wonder whether "to google sth" really should replace "to look sth up"? Are we getting paid for that kind of word of mouth advertising - especially for a monopolist? ;-)
    – gwr
    3 hours ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am relatively new to Mathematica, so I am sure this is basic, but I lack the basic terminology to be able to google it.



How can I make this command work?



Plot[x / a, x, 0, a] /. a->10


I would expect this to plot the same line for all values of a. But I get



Plot::plln: Limiting value a in Charting`Private`pvar$10408,0,a is not a machine-sized real number.


I understand that Plot expects numbers not variables, but what is the correct syntax to resolve this since a is ultimately a number?










share|improve this question









New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am relatively new to Mathematica, so I am sure this is basic, but I lack the basic terminology to be able to google it.



How can I make this command work?



Plot[x / a, x, 0, a] /. a->10


I would expect this to plot the same line for all values of a. But I get



Plot::plln: Limiting value a in Charting`Private`pvar$10408,0,a is not a machine-sized real number.


I understand that Plot expects numbers not variables, but what is the correct syntax to resolve this since a is ultimately a number?







evaluation replacement






share|improve this question









New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 hours ago









Michael E2

142k11192458




142k11192458






New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 4 hours ago









Tohiko

1083




1083




New contributor




Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Tohiko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    I wonder whether "to google sth" really should replace "to look sth up"? Are we getting paid for that kind of word of mouth advertising - especially for a monopolist? ;-)
    – gwr
    3 hours ago













  • 1




    I wonder whether "to google sth" really should replace "to look sth up"? Are we getting paid for that kind of word of mouth advertising - especially for a monopolist? ;-)
    – gwr
    3 hours ago








1




1




I wonder whether "to google sth" really should replace "to look sth up"? Are we getting paid for that kind of word of mouth advertising - especially for a monopolist? ;-)
– gwr
3 hours ago





I wonder whether "to google sth" really should replace "to look sth up"? Are we getting paid for that kind of word of mouth advertising - especially for a monopolist? ;-)
– gwr
3 hours ago











4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










So you need to get your substitution in before Plot evaluates its arguments. Next to defining the Plot inside a function with an argument a as shown by LouisB you can do the following:



Either



plotargs = x/a, x,0,a ;
Plot @@ ( plotargs /. a-> 10 )


or



inactive = Inactivate @ Plot[ x/a, x,0,a ];
inactive /. a -> 10 // Activate


To make this more general:



parameters = Association[
a -> 10
]; (* could be a whole bunch of parameters... *)

SetAttributes[ withParameters, HoldFirst];
withParameters[ func_ , pars_Association ] := Inactivate[ func ] /. pars // Activate

withParameters[ Plot[ x/a, x, 0, a] , parameters ]


Update:



If you want to pass your function as an argument to withParameters it is safest to use Inactivate also. So:



myParametricFunc = Inactivate @ Plot[ x/a, x,0,a];
withParameters[ myParametricFunc, parameters]


As a word of caution: As you can see in the documentation, the inactive form is different from the FullForm. So replacement will work for simple parameters and even options, but not for more complex substitution patters with regard to functional form.






share|improve this answer





























    up vote
    2
    down vote













    You could do this:



    Clear[p]
    p[a_] := Plot[x/a, x, 0, a]
    p[10]





    share|improve this answer



























      up vote
      2
      down vote













      With[a = 10,
      Plot[
      x/a,
      x, 0, a
      ]
      ]





      share|improve this answer



























        up vote
        2
        down vote













        You can delay the evaluation of Plot with Unevaluated:



        Unevaluated@Plot[x/a, x, 0, a] /. a -> 10


        If all occurrences of a are not literally present in the Plot code, Block can be used in place of ReplaceAll:



        function = x/a;
        x = 2; (* to show that this is ignored in the Plot command *)
        Block[a = 10,
        Plot[function, x, 0, a]
        ]


        The issue to keep in mind is that Plot is HoldAll, so that the value for function is not substituted (it is said to be "held") before Plot is called. This means, for instance, that is x also has a value, it won't be used because Plot localizes (via Block).



        A note on the second example:
        The definitions of function and x is pretty bad programming practice. The order of the definition is crucial, and it's bad to have such a dependency in Mathematica's notebook environment in which any cell can be evaluated at any time. Later if I decide I need to fix the definition of function, say function = 2 x/a, then the value of x will be substituted and function will be equal to 4/a and no longer depend on x. It's not as bad if it's a quick, one-off calculation, but it will still leave your current session with a latent definition of x. (Personally, my eyesight has trouble distinguishing Mathematica's undefined blue from the defined black on single-letter variables.) If you're writing code that will be reevaluated several times, you're better off making all dependencies functional:



        function[x_, a_] := x/a


        It doesn't help with the OP's substitution problem, in which a appears in more than just the function, so this or one of the other solutions will be needed for that.






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



          );






          Tohiko is a new contributor. Be nice, and check out our Code of Conduct.









           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184597%2fbasic-substitution%23new-answer', 'question_page');

          );

          Post as a guest






























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          So you need to get your substitution in before Plot evaluates its arguments. Next to defining the Plot inside a function with an argument a as shown by LouisB you can do the following:



          Either



          plotargs = x/a, x,0,a ;
          Plot @@ ( plotargs /. a-> 10 )


          or



          inactive = Inactivate @ Plot[ x/a, x,0,a ];
          inactive /. a -> 10 // Activate


          To make this more general:



          parameters = Association[
          a -> 10
          ]; (* could be a whole bunch of parameters... *)

          SetAttributes[ withParameters, HoldFirst];
          withParameters[ func_ , pars_Association ] := Inactivate[ func ] /. pars // Activate

          withParameters[ Plot[ x/a, x, 0, a] , parameters ]


          Update:



          If you want to pass your function as an argument to withParameters it is safest to use Inactivate also. So:



          myParametricFunc = Inactivate @ Plot[ x/a, x,0,a];
          withParameters[ myParametricFunc, parameters]


          As a word of caution: As you can see in the documentation, the inactive form is different from the FullForm. So replacement will work for simple parameters and even options, but not for more complex substitution patters with regard to functional form.






          share|improve this answer


























            up vote
            1
            down vote



            accepted










            So you need to get your substitution in before Plot evaluates its arguments. Next to defining the Plot inside a function with an argument a as shown by LouisB you can do the following:



            Either



            plotargs = x/a, x,0,a ;
            Plot @@ ( plotargs /. a-> 10 )


            or



            inactive = Inactivate @ Plot[ x/a, x,0,a ];
            inactive /. a -> 10 // Activate


            To make this more general:



            parameters = Association[
            a -> 10
            ]; (* could be a whole bunch of parameters... *)

            SetAttributes[ withParameters, HoldFirst];
            withParameters[ func_ , pars_Association ] := Inactivate[ func ] /. pars // Activate

            withParameters[ Plot[ x/a, x, 0, a] , parameters ]


            Update:



            If you want to pass your function as an argument to withParameters it is safest to use Inactivate also. So:



            myParametricFunc = Inactivate @ Plot[ x/a, x,0,a];
            withParameters[ myParametricFunc, parameters]


            As a word of caution: As you can see in the documentation, the inactive form is different from the FullForm. So replacement will work for simple parameters and even options, but not for more complex substitution patters with regard to functional form.






            share|improve this answer
























              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              So you need to get your substitution in before Plot evaluates its arguments. Next to defining the Plot inside a function with an argument a as shown by LouisB you can do the following:



              Either



              plotargs = x/a, x,0,a ;
              Plot @@ ( plotargs /. a-> 10 )


              or



              inactive = Inactivate @ Plot[ x/a, x,0,a ];
              inactive /. a -> 10 // Activate


              To make this more general:



              parameters = Association[
              a -> 10
              ]; (* could be a whole bunch of parameters... *)

              SetAttributes[ withParameters, HoldFirst];
              withParameters[ func_ , pars_Association ] := Inactivate[ func ] /. pars // Activate

              withParameters[ Plot[ x/a, x, 0, a] , parameters ]


              Update:



              If you want to pass your function as an argument to withParameters it is safest to use Inactivate also. So:



              myParametricFunc = Inactivate @ Plot[ x/a, x,0,a];
              withParameters[ myParametricFunc, parameters]


              As a word of caution: As you can see in the documentation, the inactive form is different from the FullForm. So replacement will work for simple parameters and even options, but not for more complex substitution patters with regard to functional form.






              share|improve this answer














              So you need to get your substitution in before Plot evaluates its arguments. Next to defining the Plot inside a function with an argument a as shown by LouisB you can do the following:



              Either



              plotargs = x/a, x,0,a ;
              Plot @@ ( plotargs /. a-> 10 )


              or



              inactive = Inactivate @ Plot[ x/a, x,0,a ];
              inactive /. a -> 10 // Activate


              To make this more general:



              parameters = Association[
              a -> 10
              ]; (* could be a whole bunch of parameters... *)

              SetAttributes[ withParameters, HoldFirst];
              withParameters[ func_ , pars_Association ] := Inactivate[ func ] /. pars // Activate

              withParameters[ Plot[ x/a, x, 0, a] , parameters ]


              Update:



              If you want to pass your function as an argument to withParameters it is safest to use Inactivate also. So:



              myParametricFunc = Inactivate @ Plot[ x/a, x,0,a];
              withParameters[ myParametricFunc, parameters]


              As a word of caution: As you can see in the documentation, the inactive form is different from the FullForm. So replacement will work for simple parameters and even options, but not for more complex substitution patters with regard to functional form.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 1 hour ago

























              answered 3 hours ago









              gwr

              6,89622457




              6,89622457




















                  up vote
                  2
                  down vote













                  You could do this:



                  Clear[p]
                  p[a_] := Plot[x/a, x, 0, a]
                  p[10]





                  share|improve this answer
























                    up vote
                    2
                    down vote













                    You could do this:



                    Clear[p]
                    p[a_] := Plot[x/a, x, 0, a]
                    p[10]





                    share|improve this answer






















                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      You could do this:



                      Clear[p]
                      p[a_] := Plot[x/a, x, 0, a]
                      p[10]





                      share|improve this answer












                      You could do this:



                      Clear[p]
                      p[a_] := Plot[x/a, x, 0, a]
                      p[10]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 4 hours ago









                      LouisB

                      4,2341616




                      4,2341616




















                          up vote
                          2
                          down vote













                          With[a = 10,
                          Plot[
                          x/a,
                          x, 0, a
                          ]
                          ]





                          share|improve this answer
























                            up vote
                            2
                            down vote













                            With[a = 10,
                            Plot[
                            x/a,
                            x, 0, a
                            ]
                            ]





                            share|improve this answer






















                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              With[a = 10,
                              Plot[
                              x/a,
                              x, 0, a
                              ]
                              ]





                              share|improve this answer












                              With[a = 10,
                              Plot[
                              x/a,
                              x, 0, a
                              ]
                              ]






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 2 hours ago









                              rhermans

                              21.8k439104




                              21.8k439104




















                                  up vote
                                  2
                                  down vote













                                  You can delay the evaluation of Plot with Unevaluated:



                                  Unevaluated@Plot[x/a, x, 0, a] /. a -> 10


                                  If all occurrences of a are not literally present in the Plot code, Block can be used in place of ReplaceAll:



                                  function = x/a;
                                  x = 2; (* to show that this is ignored in the Plot command *)
                                  Block[a = 10,
                                  Plot[function, x, 0, a]
                                  ]


                                  The issue to keep in mind is that Plot is HoldAll, so that the value for function is not substituted (it is said to be "held") before Plot is called. This means, for instance, that is x also has a value, it won't be used because Plot localizes (via Block).



                                  A note on the second example:
                                  The definitions of function and x is pretty bad programming practice. The order of the definition is crucial, and it's bad to have such a dependency in Mathematica's notebook environment in which any cell can be evaluated at any time. Later if I decide I need to fix the definition of function, say function = 2 x/a, then the value of x will be substituted and function will be equal to 4/a and no longer depend on x. It's not as bad if it's a quick, one-off calculation, but it will still leave your current session with a latent definition of x. (Personally, my eyesight has trouble distinguishing Mathematica's undefined blue from the defined black on single-letter variables.) If you're writing code that will be reevaluated several times, you're better off making all dependencies functional:



                                  function[x_, a_] := x/a


                                  It doesn't help with the OP's substitution problem, in which a appears in more than just the function, so this or one of the other solutions will be needed for that.






                                  share|improve this answer


























                                    up vote
                                    2
                                    down vote













                                    You can delay the evaluation of Plot with Unevaluated:



                                    Unevaluated@Plot[x/a, x, 0, a] /. a -> 10


                                    If all occurrences of a are not literally present in the Plot code, Block can be used in place of ReplaceAll:



                                    function = x/a;
                                    x = 2; (* to show that this is ignored in the Plot command *)
                                    Block[a = 10,
                                    Plot[function, x, 0, a]
                                    ]


                                    The issue to keep in mind is that Plot is HoldAll, so that the value for function is not substituted (it is said to be "held") before Plot is called. This means, for instance, that is x also has a value, it won't be used because Plot localizes (via Block).



                                    A note on the second example:
                                    The definitions of function and x is pretty bad programming practice. The order of the definition is crucial, and it's bad to have such a dependency in Mathematica's notebook environment in which any cell can be evaluated at any time. Later if I decide I need to fix the definition of function, say function = 2 x/a, then the value of x will be substituted and function will be equal to 4/a and no longer depend on x. It's not as bad if it's a quick, one-off calculation, but it will still leave your current session with a latent definition of x. (Personally, my eyesight has trouble distinguishing Mathematica's undefined blue from the defined black on single-letter variables.) If you're writing code that will be reevaluated several times, you're better off making all dependencies functional:



                                    function[x_, a_] := x/a


                                    It doesn't help with the OP's substitution problem, in which a appears in more than just the function, so this or one of the other solutions will be needed for that.






                                    share|improve this answer
























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                      You can delay the evaluation of Plot with Unevaluated:



                                      Unevaluated@Plot[x/a, x, 0, a] /. a -> 10


                                      If all occurrences of a are not literally present in the Plot code, Block can be used in place of ReplaceAll:



                                      function = x/a;
                                      x = 2; (* to show that this is ignored in the Plot command *)
                                      Block[a = 10,
                                      Plot[function, x, 0, a]
                                      ]


                                      The issue to keep in mind is that Plot is HoldAll, so that the value for function is not substituted (it is said to be "held") before Plot is called. This means, for instance, that is x also has a value, it won't be used because Plot localizes (via Block).



                                      A note on the second example:
                                      The definitions of function and x is pretty bad programming practice. The order of the definition is crucial, and it's bad to have such a dependency in Mathematica's notebook environment in which any cell can be evaluated at any time. Later if I decide I need to fix the definition of function, say function = 2 x/a, then the value of x will be substituted and function will be equal to 4/a and no longer depend on x. It's not as bad if it's a quick, one-off calculation, but it will still leave your current session with a latent definition of x. (Personally, my eyesight has trouble distinguishing Mathematica's undefined blue from the defined black on single-letter variables.) If you're writing code that will be reevaluated several times, you're better off making all dependencies functional:



                                      function[x_, a_] := x/a


                                      It doesn't help with the OP's substitution problem, in which a appears in more than just the function, so this or one of the other solutions will be needed for that.






                                      share|improve this answer














                                      You can delay the evaluation of Plot with Unevaluated:



                                      Unevaluated@Plot[x/a, x, 0, a] /. a -> 10


                                      If all occurrences of a are not literally present in the Plot code, Block can be used in place of ReplaceAll:



                                      function = x/a;
                                      x = 2; (* to show that this is ignored in the Plot command *)
                                      Block[a = 10,
                                      Plot[function, x, 0, a]
                                      ]


                                      The issue to keep in mind is that Plot is HoldAll, so that the value for function is not substituted (it is said to be "held") before Plot is called. This means, for instance, that is x also has a value, it won't be used because Plot localizes (via Block).



                                      A note on the second example:
                                      The definitions of function and x is pretty bad programming practice. The order of the definition is crucial, and it's bad to have such a dependency in Mathematica's notebook environment in which any cell can be evaluated at any time. Later if I decide I need to fix the definition of function, say function = 2 x/a, then the value of x will be substituted and function will be equal to 4/a and no longer depend on x. It's not as bad if it's a quick, one-off calculation, but it will still leave your current session with a latent definition of x. (Personally, my eyesight has trouble distinguishing Mathematica's undefined blue from the defined black on single-letter variables.) If you're writing code that will be reevaluated several times, you're better off making all dependencies functional:



                                      function[x_, a_] := x/a


                                      It doesn't help with the OP's substitution problem, in which a appears in more than just the function, so this or one of the other solutions will be needed for that.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited 2 hours ago

























                                      answered 2 hours ago









                                      Michael E2

                                      142k11192458




                                      142k11192458




















                                          Tohiko is a new contributor. Be nice, and check out our Code of Conduct.









                                           

                                          draft saved


                                          draft discarded


















                                          Tohiko is a new contributor. Be nice, and check out our Code of Conduct.












                                          Tohiko is a new contributor. Be nice, and check out our Code of Conduct.











                                          Tohiko is a new contributor. Be nice, and check out our Code of Conduct.













                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184597%2fbasic-substitution%23new-answer', 'question_page');

                                          );

                                          Post as a guest













































































                                          Comments

                                          Popular posts from this blog

                                          White Anglo-Saxon Protestant

                                          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                          One-line joke