3
$\begingroup$

I have the following code:

vars = {\[Alpha][1], \[Alpha][2], \[Alpha][3], \[Alpha][4]};
f = (vars[[1]] vars[[4]]^1 + vars[[2]] vars[[4]]^2 + 
vars[[3]] vars[[4]]^3) /. {vars[[1]] -> 1, vars[[2]] -> 2, 
vars[[3]] -> 3};
Plot[f, {Last[vars], 0, 10}]

Mathematica 10.2 returns the error

Plot::write: Tag Last in Last[vars] is Protected. >>

What do I need to do to fix this?

$\endgroup$
1
  • 2
    $\begingroup$ Do this: Plot[f,Evaluate@{Last[vars],0,10}]. The reason is that Plot has attribute HoldAll. $\endgroup$
    – Jens
    Commented Sep 1, 2015 at 21:31

1 Answer 1

7
$\begingroup$

Since Plot has attribute HoldAll, it doesn't execute the Last command with which you intended to extract the name of the plot variable. Instead, it continues to evaluate the plot expression as if the plot variable were named Last[vars], which is not allowed according to what the error message correctly states.

To get around this issue, you therefore have to explicitly force the Last[vars] to be evaluated before the enclosing Plot is called. This can be done with Evaluate, but only if it is placed correctly. You can't put Evaluate directly in front of Last because then it too will be part of the argument that is held. That argument is the entire list {Last[vars],0,10}, and it is therefore what has to be preceded by Evaluate.

This is stated in the documentation for Evaluate:

... only overrides ... attributes when it appears directly as the head of the function argument that would otherwise be held

In summary, what you can do is this:

Plot[f,Evaluate@{Last[vars],0,10}]
$\endgroup$

Your Answer

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

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