ReplaceAll not working as expected

Posted by Tim Kemp on Stack Overflow See other posts from Stack Overflow or by Tim Kemp
Published on 2010-12-23T16:36:47Z Indexed on 2010/12/25 9:54 UTC
Read the original article Hit count: 168

Filed under:

Still early days with Mathematica so please forgive what is probably a very obvious question. I am trying to generate some parametric plots. I have:

ParametricPlot[{
    (a + b) Cos[t] - h Cos[(a + b)/b t],
    (a + b) Sin[t] - h Sin[(a + b)/b t]},
    {t, 0, 2 \[Pi]}, PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}

No joy: the replacement rules are not applied and a, b and h remain undefined.

If I instead do:

Hold@ParametricPlot[{
    (a + b) Cos[t] - h Cos[(a + b)/b t],
    (a + b) Sin[t] - h Sin[(a + b)/b t]},
    {t, 0, 2 \[Pi]}, PlotRange -> All] /. {a -> 2, b -> 1, h -> 1}

it looks like the rules ARE working, as confirmed by the output:

Hold[ParametricPlot[{(2 + 1) Cos[t] - 
1 Cos[(2 + 1) t], (2 + 1) Sin[t] - 1 Sin[(2 + 1) t]}, {t, 0, 
2 \[Pi]}, PlotRange -> All]]

Which is what I'd expect. Take the Hold off, though, and the ParametricPlot doesn't work. There's nothing wrong with the equations or the ParametricPlot itself, though, because I tried setting values for a, b and h in a separate expression (a=2; b=1; h=1) and I get my pretty double cardoid out as expected.

So, what am I doing wrong with ReplaceAll and why are the transformation rules not working? This is another fundamentally important aspect of MMA that my OOP-ruined brain isn't understanding.

I tried reading up on ReplaceAll and ParametricPlot and the closest clue I found was that "ParametricPlot has attribute HoldAll and evaluates f only after assigning specific numerical values to variables" which didn't help much or I wouldn't be here.

Thanks.

© Stack Overflow or respective owner

Related posts about mathematica