# A With and ContourPlot difficulty

Consider:

sys = {u (1 - u + a v), r v (1 - v + b u)};
With[{a = 2, b = 3, r = 1},


Which gives the output:

(* {u (1 - u + 2 v) == 0, (1 + 3 u - v) v == 0} *)


Then, this works:

ContourPlot[{u (1 - u + 2 v) == 0, (1 + 3 u - v) v == 0},
{u, -0.1, 2}, {v, -0.1, 2}]


Giving this image:

But why doesn't this work?

sys = {u (1 - u + a v), r v (1 - v + b u)};
With[{a = 2, b = 3, r = 1},
{u, -0.1, 2}, {v, -0.1, 2}]]


I get a blank image with this code.

• s = a x; With[{a = 1}, s] – Dr. belisarius Apr 22 '15 at 1:16
• s = a x; Block[{a = 1}, s] – Dr. belisarius Apr 22 '15 at 1:17
• You may want to read this : mathematica.stackexchange.com/q/559/193 – Dr. belisarius Apr 22 '15 at 1:20
• @belisarius: If you post as answer, happily will delete mine... – ciao Apr 22 '15 at 1:34
• @rasher Nah, I'm going to upvote yours. But only if you spell my name right :) – Dr. belisarius Apr 22 '15 at 1:36

As noted by belisarius in the comment, you're running into scoping issues:

ClearAll["Global*"]
sys = {u (1 - u + a v), r v (1 - v + b u)};
With[{a = 2, b = 3, r = 1},
ContourPlot[
Sow@Evaluate[Thread[sys == {0, 0}]], {u, -0.1, 2}, {v, -0.1,
2}]] // Reap // Last // Short[#, 5] &

ClearAll["Global*"]
sys = {u (1 - u + a v), r v (1 - v + b u)};
Block[{a = 2, b = 3, r = 1},
ContourPlot[
Sow@Evaluate[Thread[sys == {0, 0}]], {u, -0.1, 2}, {v, -0.1,
2}]] // Reap // Last // Short[#, 5] &

(*

{{{-0.09985 (1.09985 -0.09985 a)==0,-0.09985 (1.09985 -0.09985 b) r==0},<<223>>,{2. (-1.+2. a)==0,2. (-1.+2. b) r==0}}}

{{False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,<<181>>,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False,False}}

*)


Note that with the With, your a,b, and r are never getting "in" there...

• In plain English: basically, With and Module change the names of variables before the statements are evaluated. When With starts, it only sees sys, not a, b, and r, so they are not replaced with their values. You want Block, which changes the values of variables everywhere during execution (see PrintTemporary[Dynamic[x]];Pause[1];Block[{x=1},Pause[1]];x, for example). – 2012rcampion Apr 22 '15 at 1:55
• @2012rcampion Using Block works, but how come when I look up the With command in the documentation, it says exactly this: "With[{x=x0, y=y0, ...}, expr] specifies that in expr occurrences of the symbols x, y, ... should be replaced with x0, y0, ...." Is it because what I have after the With starting point is not an expression? – David Apr 22 '15 at 2:02
• @David Let's look at this minimal example: a = b; With[{b = 1}, a]. When With does the replacement b -> 1, it looks at expr, which at this point is just a. It says, "nope, I don't see any bs there, all done." Then, after the replacement takes place, expr starts getting evaluated, at which point a is evaluated to b and returned. – 2012rcampion Apr 22 '15 at 2:05
• @2012rcampion Ah, so it is because I have With[{a=2,b=3,r=1}, followed later by sys, which has no occurrences of a, b, or r, that nothing gets inserted and then things begin? – David Apr 22 '15 at 2:17
• @David Exactly right, that's what rasher's code shows. – 2012rcampion Apr 22 '15 at 2:17