2
$\begingroup$

I am plotting a 3D graph as you can see:

Plot3D[Sqrt[((x^2 + y^2)/((x*y) + 1))], {x, -20, 20}, {y, -20, 20}]

Then I display the integer values of it for the same range (thanks to members here who helped me with it on this board here!) like so:

sol = SortBy[{x, y, s} /.Solve[s == Sqrt[((x^2 + y^2)/((x*y) + 1))] && -20 <= x <= 20 && -20 <= y <= 20, {x, y, s}, Integers], Last]

enter image description here

enter image description here

Now if you notice the graph only goes up to a value of 4 max (on the output values - running UP on the left side) whereas the integer solution for that range easily is going up to a value of 20 : {20, 0, 20} . So why is it that the graph is not showing anything larger than 4 here?

Also: for example the value of output of 1,20 is 4.36 but if you look at the graph, it spikes down (right in the middle, lower part of the chart near 0), instead of being at 4.36. What are all those SPIKES exactly on the chart? Why don't they map to some actual values? I wonder they are the imaginary roots?

Can we show values on the graph like local min and max?

How can I make this graph more clear to show all values or in a better readable format?

In addition, can I just plot a 3D graph of ONLY the integer values?

Thanks in Advance! Steve.

$\endgroup$
5
  • $\begingroup$ Try Plot3D[Sqrt[((x^2 + y^2)/((x*y) + 1))], {x, -20, 20}, {y, -20, 20}, PlotRange -> All] $\endgroup$
    – Mark R
    Commented Jan 26, 2020 at 20:35
  • $\begingroup$ One of the features of MMA is that it "auto-scales" to give the region of interest. Very useful but confusing when you first encounter it. And you may set any PlotRange you wish. The "All" parameter just says to include all. And to your question about values, it prints more than integer values as you can see by the points on the surface between integers. $\endgroup$
    – Mark R
    Commented Jan 26, 2020 at 20:36
  • $\begingroup$ Ah Thx! looks better, but now i see 2 spikes (0,20) and at (-20,0) where they spike high up to 8000? The actual output for both is actually 20. Strange. The flat sheet on the bottom makes sense since those values all equate to ~1.41 level. So are all the imaginary parts not shown at all then? Again: I also like to display integer-outputs only if possible $\endgroup$
    – Steve237
    Commented Jan 26, 2020 at 20:45
  • $\begingroup$ I think some of these values are much larger (infinite) since (xy+1)==0 for x=1, y=-1 or x=-1 and y=1. And as you say, some of the values are imaginary since xy+1 may be negative for some choices of x and y. Regarding integers, I don't know how to do that with the continuous functions. You can easily generate all integer points and plot those. foo = Table[ Sqrt[((x^2 + y^2)/((x*y) + 1))], {x, -20, 20, 1}, {y, -20, 20, 1}]; ListPlot3D[foo] $\endgroup$
    – Mark R
    Commented Jan 26, 2020 at 20:57
  • $\begingroup$ ListPointPlot3D[Flatten[ Table[{x, y, Sqrt[((x^2 + y^2)/((x*y) + 1))]}, {x, -20, 20, 1}, {y, -20, 20, 1}], 1]]? $\endgroup$
    – Michael E2
    Commented Jan 26, 2020 at 23:02

2 Answers 2

3
$\begingroup$

Here's the modification to @MarkR's solution I alluded to in the comments (note the Re@Sqrt[..] and the RegionFunction, both of which work together to give the right plot):

allowedValue = 
  Select[Flatten[Table[{x, y}, {x, -20, 20, 1}, {y, -20, 20, 1}], 1], 
   Times @@ # + 1 != 0 &];
allowedPoints = 
  With[{x = #[[1]], y = #[[2]]}, {x, y, 
      Re@Sqrt[((x^2 + y^2)/((x*y) + 1))]}] & /@ allowedValue;
ListPlot3D[allowedPoints, 
 RegionFunction -> Function[{x, y, z}, x y + 1. > 0.], 
 ClippingStyle -> None]

enter image description here

Although, the OP in a comment indicates interest in "integer-outputs only," which is interpreted to mean outputs at integer {x, y} below:

ListPointPlot3D[
 Flatten[Table[{x, y, Sqrt[((x^2 + y^2)/((x*y) + 1))]},
 {x, -20, 20, 1}, {y, -20, 20, 1}], 1]]
(* Power::infy errors which are ignored by ListPointPlot3D *)

enter image description here

As for my convex hull remark about ListPlot3D, which I made under @MarkR's answer, ListPlot3D interpolates a surface through the points. To do that, it constructs a domain for the interpolation. The domain is the ConvexHullMesh of the xy coordinates of the data. Compare:

allowedPoints =  (* MarkR's version *)
  With[{x = #[[1]], y = #[[2]]}, {x, y, 
      Sqrt[((x^2 + y^2)/((x*y) + 1))]}] & /@ allowedValue;

realPoints = 
  Cases[allowedPoints, {x_, y_, z_ /; MatchQ[N@z, _Real]} :> {x, y}];

GraphicsRow[{
  Show[
   ConvexHullMesh[realPoints],
   Graphics[{Red, Point@realPoints}],
   Frame -> True
   ],
  ListPlot3D[allowedPoints, 
   RegionFunction -> Function[{x, y, z}, x y + 1. > 0.], 
   ClippingStyle -> None, ViewPoint -> {0, 0, Infinity}]
  }]

enter image description here

For some reason, adding RegionFunction -> Function[{x, y, z}, x y + 1. > 0.] to @MarkR's ListPlot3D does not exclude the sheets as it does in my code that began this answer. I guess it's because none of the real points lie in the excluded region, but I would think Mathematica could do better.

$\endgroup$
2
  • $\begingroup$ Thanks all! These are amazing. Once I get a minute I will play around with them. I thought about something else which I wonder Mathematica can depict: To show the contribution of the +1 in the denominator, what if we were to graph it with just the xy in the denominator like (x^2+y^2)/xy then do another one with a -1 like (x^2+y^2)/(xy-1), then have the resulting graphs interlaced/transposed on top of one another to see the contribution. Is there a function to allow this visibility? Thx $\endgroup$
    – Steve237
    Commented Jan 30, 2020 at 4:05
  • $\begingroup$ forget this question above - it deserves its own new thread - will do that now $\endgroup$
    – Steve237
    Commented Jan 31, 2020 at 3:41
1
$\begingroup$

Use PlotRange to constrain what is plotted. Mathematica scales automatically and this may be confusing at first. You correctly checked the value to see the range making the auto-scaling more obvious.

This gives you all of the plot:

Plot3D[Sqrt[((x^2 + y^2)/((x*y) + 1))], {x, -20, 20}, {y, -20, 20}, PlotRange -> All] 

Later you said you want integers. You may get those with the following:

foo = Table[
   Sqrt[((x^2 + y^2)/((x*y) + 1))], {x, -20, 20, 1}, {y, -20, 20, 1}];
ListPlot3D[foo,PlotRange->All]

You may further wish to restrict the values of Infinity or ComplexInfinity with something like this:

allowedValue = 
  Select[Flatten[Table[{x, y}, {x, -20, 20, 1}, {y, -20, 20, 1}], 1], 
   Times @@ # + 1 != 0 &];
allowedPoints = 
  With[{x = #[[1]], y = #[[2]]}, {x, y, 
      Sqrt[((x^2 + y^2)/((x*y) + 1))]}] & /@ allowedValue;
ListPlot3D[allowedPoints]

You'll notice that you don't need the PlotRange on this last plot (identical plot if you do that).

The resulting plot:

$\endgroup$
7
  • $\begingroup$ Thx sooo much! plz give me a couple hours to go thru this when I get back! $\endgroup$
    – Steve237
    Commented Jan 26, 2020 at 22:17
  • 2
    $\begingroup$ The two planes over quadrants II and IV, where the function is undefined, should not be there. That's ListPlot3D taking the convex hull of the domain and interpolating across missing/non-real values. $\endgroup$
    – Michael E2
    Commented Jan 26, 2020 at 23:06
  • $\begingroup$ Thanks Mark - but my graph looks different for some reason using even exact same expressions as yours! Like it's chopped off at the two corner-sections and colored grey there. I am still not that familiar with the Mathematica Language syntax. For example what does a Pound # refer to. or an @. I can gather as much as "@@ # + 1 != 0 " means do not let some denominator be zero. But why double-@.... Or "Times"? or /@ ? Lol... $\endgroup$
    – Steve237
    Commented Jan 27, 2020 at 4:46
  • $\begingroup$ Thanks Michael. "convex hull of the domain"? What do you mean by this? I realize the plot is maneuvering over the non-real values, but that's what we wanted initially. Like the graph was supposed to be for integer outputs only, was it not? Then why would there even be non-real given that fact. Thanks $\endgroup$
    – Steve237
    Commented Jan 27, 2020 at 4:49
  • $\begingroup$ @MichaelE2, thanks for the comment. I changed the equation to allowedValue = Select[Flatten[Table[{x, y}, {x, -20, 20, 1}, {y, -20, 20, 1}], 1], (Times @@ # + 1 != 0) && (Times @@ Sign[#] != -1) &]; to exclude the quadrants you mention and the graph looks the same. May I know how to fix this? $\endgroup$
    – Mark R
    Commented Jan 27, 2020 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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