-2
$\begingroup$

The code as following and the code runs in Mathematica version 14.1.0.

Clear["`*"];
Manipulate[
 Module[{circle, center, radius, pointP, pointB, tangentPoint}, 
  circle = Disk[{0, 4}, 1]; center = {0, 4}; radius = 1; 
  pointP = {a^2/4, a}; pointB = {-1, a}; 
  tangentPoint = 
   NSolve[EuclideanDistance[{x, y}, center] == radius && 
     EuclideanDistance[{x, y}, pointP] == 
      Sqrt[radius^2 + EuclideanDistance[pointP, center]^2], {x, y}, 
    Reals];
  tangentPoint = {x, y} /. tangentPoint;
  Show[ContourPlot[{y^2 == 4 x, x^2 + (y - 4)^2 == 1, 
     x == -1}, {x, -3, 6}, {y, -8, 8}, 
    ContourStyle -> {Blue, Thick, Dashed, Black}, 
    PerformanceGoal -> "Quality", Frame -> False, Axes -> True, 
    AxesOrigin -> {0, 0}, AxesLabel -> {"x", "y"}, 
    AspectRatio -> Automatic], 
   Graphics[{Red, PointSize[0.02], Point[pointP], 
     Text[Style["P", 12, Italic, FontFamily -> "Times"], 
      pointP, {-1, -1}], Black, PointSize[0.02], Point[pointB], 
     Text[Style["B", 12, Italic, FontFamily -> "Times"], 
      pointB, {1, 0}], Black, Dashed, Line[{pointP, pointB}], Black, 
     PointSize[0.02], Point[center], 
     Text[Style["A", 12, Italic, FontFamily -> "Times"], 
      center, {1, 0}], Black, Line[{pointP, #}] & /@ tangentPoint}], 
   PlotRange -> {{-3, 6}, {-8, 8}}]], {a, 0, 6}]

After running the code, the image appears normal when stationary, but when I click the button to make the moving point P start moving, an error message is displayed.

NSolve::ratnz: NSolve was unable to solve the system with inexact coefficients. The \
answer was obtained by solving a corresponding exact system and \
numericizing the result.

enter image description here


After adding Rationalize, why is the problem still there when I run the code in version 14.1.0?

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ (-1) for never searching before asking. $\endgroup$
    – xzczd
    Commented Sep 5 at 0:36
  • 1
    $\begingroup$ i.sstatic.net/DaQO3tu4.png Clearly you've modified Daniel's code in some way, but you've only showed us an incomplete screenshot. Sadly I cannot downvote twice. $\endgroup$
    – xzczd
    Commented Sep 5 at 1:00
  • $\begingroup$ 1. Can't reproduce in version 14.1: wolframcloud.com/obj/491fbe13-604f-4e93-8d08-15cbfc4f5cd4 2. You should always mention your Mathematica version. 3. I don't see In[] symbol in the screenshot, have you really executed the code? $\endgroup$
    – xzczd
    Commented Sep 5 at 5:17

3 Answers 3

8
$\begingroup$

Nothing to worry about, this is only a warning. But the error message is cumbersome. To get rid of it, rationalize pointP and pointB:

Clear["`*"];
Manipulate[
 Module[{circle, center, radius, pointP, pointB, tangentPoint}, 
  circle = Disk[{0, 4}, 1]; center = {0, 4}; radius = 1;
  pointP = {a^2/4, a} // Rationalize; 
  pointB = {-1, a} // Rationalize;
  tangentPoint = 
   NSolve[EuclideanDistance[{x, y}, center] == radius && 
     EuclideanDistance[{x, y}, pointP] == 
      Sqrt[radius^2 + EuclideanDistance[pointP, center]^2], {x, y}, 
    Reals];
  tangentPoint = {x, y} /. tangentPoint;
  Show[ContourPlot[{y^2 == 4 x, x^2 + (y - 4)^2 == 1, 
     x == -1}, {x, -3, 6}, {y, -8, 8}, 
    ContourStyle -> {Blue, Thick, Dashed, Black}, 
    PerformanceGoal -> "Quality", Frame -> False, Axes -> True, 
    AxesOrigin -> {0, 0}, AxesLabel -> {"x", "y"}, 
    AspectRatio -> Automatic], 
   Graphics[{Red, PointSize[0.02], Point[pointP], 
     Text[Style["P", 12, Italic, FontFamily -> "Times"], 
      pointP, {-1, -1}], Black, PointSize[0.02], Point[pointB], 
     Text[Style["B", 12, Italic, FontFamily -> "Times"], 
      pointB, {1, 0}], Black, Dashed, Line[{pointP, pointB}], Black, 
     PointSize[0.02], Point[center], 
     Text[Style["A", 12, Italic, FontFamily -> "Times"], 
      center, {1, 0}], Black, Line[{pointP, #}] & /@ tangentPoint}], 
   PlotRange -> {{-3, 6}, {-8, 8}}]], {a, 0, 6}]

enter image description here

$\endgroup$
7
  • $\begingroup$ After adding it, why is the problem still there when I run the code? $\endgroup$
    – csn899
    Commented Sep 5 at 0:39
  • $\begingroup$ I have version 14.1 and this code runs without problems. $\endgroup$ Commented Sep 5 at 7:00
  • $\begingroup$ With version 14.1, I find that the problem still exists for the code in this answer. $\endgroup$
    – bbgodfrey
    Commented Sep 6 at 9:20
  • $\begingroup$ Strange, I tried it again and it runs without problems. $\endgroup$ Commented Sep 6 at 9:50
  • $\begingroup$ I agree that this is strange. Did you test your code by copying it from your answer above? Also, on what operating system did you run the code? @xzczd suggests in a comment that different operating systems may affect when this warning message occurs. $\endgroup$
    – bbgodfrey
    Commented Sep 6 at 14:53
0
$\begingroup$

Because EuclideanDistance and Sqrt themselves produce numbers that are not rationalized, it sometimes is necessary to Rationalize their output to eliminate the warning message. Specifically, modify the NSolve call in the question to

NSolve[Rationalize[EuclideanDistance[{x, y}, center] - radius, 0] == 0 
    && Rationalize[EuclideanDistance[{x, y}, pointP] - 
       Sqrt[radius^2 + EuclideanDistance[pointP, center]^2], 0] == 0, 
{x, y}, Reals];

I have tested this result with version 14.1.0 for Microsoft Windows (64-bit) (July 16, 2024).

$\endgroup$
2
  • $\begingroup$ Wait, do you mean that in 14.1 windows, EuclideanDistance gives inexact output e.g. EuclideanDistance[{1, 2, 3}, {2, 4, 6}] gives 3.74166? This isn't the behavior of Linux version: i.sstatic.net/GNSinWQE.png $\endgroup$
    – xzczd
    Commented Sep 6 at 12:10
  • $\begingroup$ @xzczd I wrote "inexact' in my comment, because that is the word that the warning message used. Perhaps, I should have used "not rationalized" or some other term instead. I shall change it to avoid confusion. Thanks. $\endgroup$
    – bbgodfrey
    Commented Sep 6 at 14:54
-1
$\begingroup$
Clear["`*"];
Manipulate[
 Module[{circle, center, radius, pointP, pointB, tangentPoint}, 
  circle = Disk[{0, 4}, 1];
  center = {0, 4};
  radius = 1;
  pointP = {a^2/4, a} // Rationalize;
  pointB = {-1, a} // Rationalize;
  (*Distance formula setup*)
  tangentPoint = 
   NSolve[{Sqrt[(x - center[[1]])^2 + (y - center[[2]])^2] == radius, 
     Sqrt[(x - pointP[[1]])^2 + (y - pointP[[2]])^2] == 
      Sqrt[radius^2 + EuclideanDistance[pointP, center]^2]}, {x, y}, 
    Reals];
  tangentPoint = {x, y} /. tangentPoint;
  (*Display the plot*)
  Show[ContourPlot[{y^2 == 4 x, x^2 + (y - 4)^2 == 1, 
     x == -1}, {x, -3, 6}, {y, -8, 8}, 
    ContourStyle -> {Blue, Thick, Dashed, Black}, 
    PerformanceGoal -> "Quality", Frame -> False, Axes -> True, 
    AxesOrigin -> {0, 0}, AxesLabel -> {"x", "y"}, 
    AspectRatio -> Automatic], 
   Graphics[{Red, PointSize[0.02], Point[pointP], 
     Text[Style["P", 12, Italic, FontFamily -> "Times"], 
      pointP, {-1, -1}], Black, PointSize[0.02], Point[pointB], 
     Text[Style["B", 12, Italic, FontFamily -> "Times"], 
      pointB, {1, 0}], Black, Dashed, Line[{pointP, pointB}], Black, 
     PointSize[0.02], Point[center], 
     Text[Style["A", 12, Italic, FontFamily -> "Times"], 
      center, {1, 0}], Black, Line[{pointP, #}] & /@ tangentPoint}], 
   PlotRange -> {{-3, 6}, {-8, 8}}]], {a, 0, 6}]
$\endgroup$

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