I'm plotting a tangent line to a region and, depending when I evaluate it, I get inconsistent results. I want to use the first way of doing things for flexibility but it doesn't seem to be working. What is going wrong?

The set up code is:

mu1 = -1.0; mu2 = 1.0; ss1 = 1; ss2 = 4; cov = 1;
bivModel = 
 MultinormalDistribution[{mu1, mu2}, {{ss1, cov}, {cov, ss2}}]
myFun[x_, y_] := PDF[bivModel, {x, y}];
a = -2.7; b = 1.6;
yVec = D[myFun[x, y], y];
xVec = D[myFun[x, y], x]

Then when I create a graph:

  PDF[bivModel, {x, y}] >= contourval, {x, -7, 3}, {y, -4, 7}], 
 Plot[-(xVec //. {x -> a, y -> b})/(yVec //. {x -> a, y -> b})*(x - 
      a) + b, {x, -7, 3}], Graphics[Point[{a, b}]]]

I get this weird thing:

enter image description here

But then if I perform the simplification that I expect to occur within the expression I get what I am expecting. Typing:

-(xVec //. {x -> a, y -> b})/(yVec //. {x -> a, y -> b})*(x - 
          a) + b


1.6 + 3.21739 (2.7 + x)

And substituting this back in the above gives:

  PDF[bivModel, {x, y}] >= contourval, {x, -7, 3}, {y, -4, 7}], 
 Plot[1.6` + 3.2173913043478266` (2.7` + x), {x, -7, 3}], 
 Graphics[Point[{a, b}]]]

Running this gives the following figure that I was expecting (and which is correct).

Why is the first expression (using //.) not working?

enter image description here

  • 2
    $\begingroup$ Because of how Plot evaluates its arguments. Try wrapping the first argument with Evaluate $\endgroup$ – mmeent Dec 5 '18 at 16:47
  • $\begingroup$ This works, but I have a more complicated instance where it does not seem to be working, so will edit my original question to include it. $\endgroup$ – GerardF123 Dec 5 '18 at 17:04

The use of "Evaluate" suggested by mmeent above works. I also realized that instead of using RepeatReplace, I could simply turn xVec and yVec into functions. Once these are substituted in

Plot[-(xVec[a,b])/(yVec[a,b])*(x - 
      a) + b, {x, -7, 3}], Graphics[Point[{a, b}]]]

the issue seems to disappear.


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.