6
$\begingroup$

According to the documentation AppendTo[s,elem] is equivalent to s = Append[s,elem]. But then

y = {}; Plot[y = Append[y, x]; Sin[x], {x, Pi/4, Pi/2}]

works as intended, while

y = {}; Plot[AppendTo[y, x]; Sin[x], {x, Pi/4, Pi/2}]

produces the error message

AppendTo::rvalue: {[Pi]/4} is not a variable with a value, so its value cannot be changed.

Can someone explain what is going on?

$\endgroup$

1 Answer 1

8
$\begingroup$

It is an evaluation issues. I can't pin the exact sequence now, but here is a workaround

y = {}; 
Plot[AppendTo[y, x]; Sin[x], {x, Pi/4, Pi/2}, Evaluated -> False]
y

Mathematica graphics

Which now gives the same exact output as

y = {}; 
Plot[y = Append[y, x]; Sin[x], {x, Pi/4, Pi/2}]

You can see this answer using-evaluate-and-evaluated-true-in-plot for more information about what Evaluated do.

$\endgroup$
1
  • 2
    $\begingroup$ You can also do Plot[AppendTo[Unevaluated@y,x];...] $\endgroup$
    – N.J.Evans
    Commented Jul 2, 2021 at 15:34

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.