1
$\begingroup$

This is a continuation of a question I posed at: Examining the function $f(x,y)=xy(x^2-y^2)/(x^2+y^2)$.

The quest is to analyze the partial derivative $$ f_x(x,y)=\begin{cases} \dfrac{y(x^4+4x^2y^2-y^4)}{(x^2+y^2)^2},&(x,y)\ne(0,0)\\ 0,& (x,y)=(0,0) \end{cases}$$ to see if it is continuous at (0,0). I've tried a little manipulate activity:

DynamicModule[{f, max, min},
 fx[x_, y_] := 
  Piecewise[{{(y (x^4 + 4 x^2 y^2 - y^4))/(x^2 + y^2)^2, 
     x != 0 && y != 0}}, 0];
 Manipulate[
  max = NMaximize[{fx[x, y], Sqrt[x^2 + y^2] < \[Delta]}, {x, y}][[1]];
  min = NMinimize[{fx[x, y], Sqrt[x^2 + y^2] < \[Delta]}, {x, y}][[1]];
  Column[{
    Row[{"Min = " <> ToString[min], ", Max = " <> ToString[max]}],
    Plot3D[{fx[x, y], 
      0}, {x, -\[Delta], \[Delta]}, {y, -\[Delta], \[Delta]},
     PlotStyle -> {Directive[Red], 
       Directive[LightBlue, Opacity[.8]]},
     RegionFunction -> Function[{x, y, z}, Sqrt[x^2 + y^2] < \[Delta]],
     PlotRange -> All,
     BoxRatios -> Automatic,
     PerformanceGoal -> "Quality"]
    }],
  {{\[Epsilon], .1}, .001, .15, Appearance -> "Labeled"},
  {{\[Delta], .5}, .01, 1, Appearance -> "Labeled"}
  ]
 ]

There are a couple of problems. First, it's very slow as I am calculating min, max, and redrawing the image each time the slide moves. Second, when the delta slider gets tiny, things go bad.

Any thoughts?

Example of something that happens:

NMinimize[{fx[x, y], Sqrt[x^2 + y^2] < .1}, {x, y}]

During evaluation of In[54]:= NMinimize::incst: NMinimize was unable to generate any initial points satisfying the inequality constraints {-0.1+Sqrt[x^2+y^2]<=0}. The initial region specified may not contain any feasible points. Changing the initial region or specifying explicit initial points may provide a better solution. >>

Out[54]= {-0.1, {x -> -7.45003*10^-9, y -> 0.1}}

$\endgroup$
  • 2
    $\begingroup$ In polar coordinates it's fx[x, y] == -(1/2) r (-1 - 2 Cos[2 t] + Cos[4 t]) Sin[t] and fairly easy to understand. In particular you can separate variables, optimize over t -- min/max are always at t equals Pi/2 and -Pi/2. But as I recall from your other questions, you might want to leave it in terms of cartesian coordinates for the sake of your students. But I would teach my students to analyze this particular function in polar coordinates because of the denominator. (I mean they should take one look and say, "Oh, of course, let's use polar.") $\endgroup$ – Michael E2 Jul 14 '15 at 22:18
  • $\begingroup$ @MichaelE2 This comment caused me to do some searching and reading which wound up being really helpful. However, still not sure how to repair my Manipulate. See an update addition to original post. $\endgroup$ – David Jul 15 '15 at 4:25
2
$\begingroup$

In the current version of the question, the second parameter $\epsilon$ isn't needed. If you can do without it, I would suggest pre-computing the list of relevant $\delta$ values and making a ListAnimate to speed up the rendering. But I will assume that you can't go that route, maybe because you'll add some dependence on $\epsilon$ later.

Then the numerical problems can be avoided by throwing out NMinimize and NMaximize altogether:

DynamicModule[{f, max, min, g}, 
 fx[x_, y_] := 
  Piecewise[{{(y (x^4 + 4 x^2 y^2 - y^4))/(x^2 + y^2)^2, 
     x != 0 && y != 0}}, 0];
 Manipulate[
  g = Plot3D[{fx[x, y], 
     0}, {x, -δ, δ}, {y, -δ, δ}, 
    PlotStyle -> {Directive[Red], Directive[LightBlue, Opacity[.8]]}, 
    RegionFunction -> Function[{x, y, z}, Sqrt[x^2 + y^2] < δ],
     PlotRange -> All, BoxRatios -> Automatic, 
    PerformanceGoal -> "Quality"];
  {min, max} = 
   First@Cases[g, GraphicsComplex[pts_, __] :> MinMax[pts[[All,3]]]];
  Column[{Row[{"Min = " <> ToString[min], 
      ", Max = " <> ToString[max]}], 
    g}], {{ϵ, .1}, .001, .15, 
   Appearance -> "Labeled"}, {{δ, .5}, .01, 1, 
   Appearance -> "Labeled"}]]

Here, I used the fact that in order to plot the function, Mathematica already did the calculation of the minimum and maximum function value for you. In particular with the setting PerformanceGoal -> "Quality", this should be sufficiently accurate for the purposes of a Manipulate to display as the numerical minima and maxima. I extract these values using MinMax in a Cases statement applied to the plot which is generated beforehand and called g.

More explanations:

Within Cases, I make use of the fact that the 3D plot is by default given as a GraphicsComplex, in which all the 3D points are listed as one big list, followed by instructions of what to do with those points (i.e., how to assemble them into polygons etc.) The advantage of GraphicsComplex is that it allows references to 3D points by a single index (giving its position in the point list) - and another advantage is that you don't have to look for the plot coordinates anywhere else, only in the first argument of GraphicsComplex, which is what I do using the pattern GraphicsComplex[pts_,__]. Naming this list pts, I can then subject it to transformations. Cases outputs only the result of those transformations, done with :>. What I want is the largest and smallest z component among all those points. This is what MinMax does. To look only at the z components of the list, I use the part specification [[All,3]].

$\endgroup$
  • $\begingroup$ I need some help with this part: Cases[g, GraphicsComplex[pts_, __] :> MinMax[pts]]. I looked up Cases in the documentation and it says that Cases[{e1,e2,...},pattern} gives a list of the e_i that match the pattern. I also looked up MinMax and it said it finds the minimum and maximum of a list. Then I tried Cases[g,GraphicsComplex[pts_,___] and got a list of points of the form {x,y,z}. Then I tried MinMax[{{1,-3,-2},{2,5,1}}] and got the answer {-3,5}, which is not the minimum and maximum z-values that I want. Are things OK here? Can you explain? $\endgroup$ – David Jul 15 '15 at 16:24
  • 1
    $\begingroup$ Oops, silly me - I forgot to select the z components only. Thanks for actually testing my code! I'll edit the answer. $\endgroup$ – Jens Jul 15 '15 at 17:22
  • $\begingroup$ So the one I really need is: Cases[{Subscript[e, 1],\[Ellipsis]},pattern->rhs] gives a list of the values of rhs corresponding to the Subscript[e, i] that match the pattern. So it starts with g, finds the stuff the matches the pattern GraphicsComplex[pts,___], and now all of the points are stored in pots? Next, pts[[All,3]] select all the z-values of the points, then MaxMin selects the max and min of all of these z-values, then the max and min are what are returned. Think I have it. $\endgroup$ – David Jul 15 '15 at 18:12
  • $\begingroup$ Yes, that's right: except the name pts_ must appear with an underscore in the pattern (that's the left hand side of the :>): that underscore marks it as a Blank pattern and pts as the (temporary) name we want to use for that pattern when referring to it on the right hand side of :>. $\endgroup$ – Jens Jul 15 '15 at 18:36
  • $\begingroup$ Agan, great effort on your part to offer such wonderful assistance. $\endgroup$ – David Jul 15 '15 at 21:39

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.