Basic substitution

Clash 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?
evaluation replacement
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.
add a comment |Â
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?
evaluation replacement
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
add a comment |Â
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?
evaluation replacement
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
evaluation replacement
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.
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
add a comment |Â
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
add a comment |Â
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.
add a comment |Â
up vote
2
down vote
You could do this:
Clear[p]
p[a_] := Plot[x/a, x, 0, a]
p[10]
add a comment |Â
up vote
2
down vote
With[a = 10,
Plot[
x/a,
x, 0, a
]
]
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 1 hour ago
answered 3 hours ago
gwr
6,89622457
6,89622457
add a comment |Â
add a comment |Â
up vote
2
down vote
You could do this:
Clear[p]
p[a_] := Plot[x/a, x, 0, a]
p[10]
add a comment |Â
up vote
2
down vote
You could do this:
Clear[p]
p[a_] := Plot[x/a, x, 0, a]
p[10]
add a comment |Â
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]
You could do this:
Clear[p]
p[a_] := Plot[x/a, x, 0, a]
p[10]
answered 4 hours ago
LouisB
4,2341616
4,2341616
add a comment |Â
add a comment |Â
up vote
2
down vote
With[a = 10,
Plot[
x/a,
x, 0, a
]
]
add a comment |Â
up vote
2
down vote
With[a = 10,
Plot[
x/a,
x, 0, a
]
]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With[a = 10,
Plot[
x/a,
x, 0, a
]
]
With[a = 10,
Plot[
x/a,
x, 0, a
]
]
answered 2 hours ago
rhermans
21.8k439104
21.8k439104
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited 2 hours ago
answered 2 hours ago
Michael E2
142k11192458
142k11192458
add a comment |Â
add a comment |Â
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.
Tohiko is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184597%2fbasic-substitution%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

1
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