1
$\begingroup$

I would like to make easy to change the variable for which I am making a set of plots. Say I want to plot Sin, Tan, Cos and other functions of the same argument. To quickly switch from plotting it as function of x or y I wanted to use a "dummy" plot variable var and assing it to y or x depending on the need.

x = 1; var = y;
Plot[Sin[x*y], {var, 1, 2}]
Plot[Tan[x*y], {var, 1, 2}]
Plot[Cos[x*y], {var, 1, 2}]
Unset[{x, y, var}];

Unfortunately this doesn't quite works because var in the range specification {var,1,2} is not evaluated.

Any suggestions?

$\endgroup$
3
  • 2
    $\begingroup$ Evaluate@{var, 1, 2}... $\endgroup$
    – ciao
    May 5, 2014 at 7:53
  • $\begingroup$ great! it works, but what is the difference with respect to Evaluate[var] which was my trial before posting here? $\endgroup$
    – Rho Phi
    May 5, 2014 at 10:16
  • 1
    $\begingroup$ I can't check it but as far as I remember Evaluate works only on first level. $\endgroup$
    – Kuba
    May 5, 2014 at 13:37

1 Answer 1

2
$\begingroup$

You need to force evaluation of the x interval list:

x = 8; var = y;
Plot[Sin[x*y], Evaluate@{var, 1, 2}]
Plot[Tan[x*y], Evaluate@{var, 1, 2}]
Plot[Cos[x*y], Evaluate@{var, 1, 2}]
Unset[{x, y, var}];

plots

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.