# RegionPlot fails when given function with internally redefined variables

I've just upgraded to v10, and have noticed that some of my trusty old notebooks which used RegionPlot now fail to run. I've boiled down the problem I'm having to the following examples.

This first example works fine. This function outputs the value of a paraboloid polynomial:

parabolloid = Function[{input1, input2},
x = input1;
y = input2;
x^2 + y^2
]


Suppose I want to plot the region for which the function output is <1:

RegionPlot[parabolloid[x, y] < 1, {x, 0, 2}, {y, 0, 2}]


Result is as expected: Now let's rewrite the function like so, swapping where I write the exponent:

parabolloid = Function[{input1, input2},
x = input1^2;
y = input2;
x + y^2
]


This ought to give us identical results. First, let's test the function:

parabolloid[2, 2]


The returned value is 8, as expected. So the function still seems to work a-ok.

But when I evaluate the function in RegionPlot as above, my CPU churns away for a few seconds before getting a beep with kernal quit. No error messages or warnings, just a Kernel quit.

Surely we ought to be able to perform operations on input arguments within a function, and on v8, that seemed to work fine. (Admittedly, I don't have v8 handy at the moment, so I can't test my simple examples above, so I suppose I can't prove without doubt that this is a version-specific issue)

• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Nov 26 '14 at 19:44
• As far as I can see, everything runs as it should if you use a more compact definition of your functions, for example parabolloid = Function[{x, y}, x + y^2]. Otherwise localize the x and y in your definition: parabolloid = Function[{input1, input2}, Module[{x, y}, x = input1^2; y = input2; x + y^2]] – Fred Simons Nov 26 '14 at 19:58
• I think first you should try localizing your x and y. – Silvia Nov 26 '14 at 20:02
• Right you are Silvia and Fred, it was a localization issue. Rewriting the RegionPlot syntax as the following did the trick: RegionPlot[parabolloid[x1,y1],{x1,0,2},{y1,0,2}] I can only guess that it worked in the first example I gave because the x = input1; y = input2; lines didn't actually change the values of x and y from their assignments given by RegionPlot. – rocketfuel4dinner Nov 26 '14 at 20:03
• I can reproduce the crash in a fresh kernel on v10.0.1 Windows 7 64-bit. The plot is created successfully on versions v7.0.1, v8.0.1 and v9.0.1 on the same machine (despite the unlocalized use of x and y). – WReach Dec 28 '14 at 23:18

## 1 Answer

I can reproduce the crash on V10.0.1 Mac OSX 10.9.5.

parabolloid =
Function[{input1, input2},
x = input1^2;
y = input2;
x + y^2];


This crashes the kernel (uncomment or copy without the comment markers if you wish to run):

(*
RegionPlot[parabolloid[x, y] < 1, {x, 0, 2}, {y, 0, 2}]
*)


Note the function is not well-defined:

parabolloid[x, y]


$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

(*  Hold[x^2]  *)


Still, a more graceful way of letting the user know that Mathematica is dissatisfied than crashing is desirable. You should report it to WRI.

Another source of "dissatisfaction" by Mathematica may be that RegionPlot is trying to control the values of x and y and parabolloid is resetting them. This is not good practice even if it works on some versions of Mathematica. (It would be nice if Mathematica would warn you when you try do it.) Workarounds, involving localizing x and y in parabolloid, are offered in the comments.