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.
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 $\endgroup$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}]
$\endgroup$