# RegionPlot Calling User-Defined Function Not Working

This is following up on this post: Function not evaluating as expected

I am trying to make a RegionPlot with Manipulate in which some user-defined function is called. It's not working, and I don't know why. Here is the code:

U[x__?NumericQ, p__?NumericQ] := RegionMeasure[ImplicitRegion[
(v - p - x >= 0 && 0 <= v <= 1 ) , {v}]]

q[p_?NumericQ] := (x /. FindRoot[x - U[x, p] == 0, {x, 0.5, 0, 1}])

Manipulate[
RegionPlot[{
(v - p - x >  0 ) /. {x -> q[p]}
}, {v, 0.000000001, 1}, {c, 0.001, 1} (*
Just to see it as a 2D RegionPlot *),
FrameLabel -> Automatic], {p, .001, 1}]


Here, c is just a variable not in the model, so that I can visualize a 2D plot. I am expecting a vertical band of blue over the range of v that satisfies the inequality (so for all c, the inequality holds for that range of v). However, I get an error: Throw::sysexc: Uncaught SystemException returned to top level.

What is the issue here?

I should mention that this code here works:

Plot[x /. {x -> q[p]}, {p, 0, 1}]


So it's something about the Manipulate, RegionPlot, or interaction between the two that is causing the issue.

• If you carefully study the documentation for Manipulate then down in "Details and Options" you may find "Manipulate is a scoping construct that implements lexical scoping." and that may mean more than you think. Does this work for you? Manipulate[ x=x/.FindRoot[x-RegionMeasure[ImplicitRegion[ (v-p-x>=0&&0<=v<=1),{v}]]==0,{x,0.5,0,1}]; RegionPlot[{v-p-x>0}, {v,10^-9,1},{c,0.001,1}],{p,.001,1}] That tries to avoid the scoping issues. Please check this very carefully in a newly opened Mathematica notebook. Thanks
– Bill
Jul 2, 2021 at 20:56
• Yes, that works! Thanks, Bill! I'll take a look at the Manipulate scoping details in a bit. Admittedly, I don't know what lexical scoping means, but I'll be able to figure it out now that I see that's the term to look for. Jul 2, 2021 at 21:01
• Parts of Mathematica LOOK like an ordinary programming language, but there are things hidden under the surface that are doing things that are very different from what you might imagine. Wiki has a page on lexical scoping, but I don't feel that is very helpful. I tend to put everything that Manipulate depends on inside of the Manipulate and not bury things inside layer after layer of := functions, etc. I'm glad my code worked for you. I've always wanted to find an excellent book on the details of the evaluation process, which is the heart of everything in Mathematica, but never found one.
– Bill
Jul 2, 2021 at 21:08
• Interesting, thanks for the help! Jul 2, 2021 at 21:15
• Manipulate[ RegionPlot[{(v - p - x > 0) /. {x -> q[p]}} // Evaluate, {v, 0.000000001, 1}, {c, 0.001, 1} (*Just to see it as a 2D RegionPlot*), FrameLabel -> Automatic], {p, .001, 1}] Jul 3, 2021 at 1:29