1
$\begingroup$

Consider:

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

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:

enter image description here

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},
 ContourPlot[Evaluate[Thread[sys == {0, 0}]],
  {u, -0.1, 2}, {v, -0.1, 2}]]

I get a blank image with this code.

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

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...

$\endgroup$
  • 1
    $\begingroup$ 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). $\endgroup$ – 2012rcampion Apr 22 '15 at 1:55
  • $\begingroup$ @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? $\endgroup$ – David Apr 22 '15 at 2:02
  • 1
    $\begingroup$ @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. $\endgroup$ – 2012rcampion Apr 22 '15 at 2:05
  • $\begingroup$ @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? $\endgroup$ – David Apr 22 '15 at 2:17
  • $\begingroup$ @David Exactly right, that's what rasher's code shows. $\endgroup$ – 2012rcampion Apr 22 '15 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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