3
$\begingroup$

This is what I want:

RegionPlot[
{
   y < x^2,
   y < 0.5 + 0.5 x
},
{x, 0, 2},
{y, 0, 4}
]

This code does not work (draws only one region):

rf[x_, y_] := {
   y < x^2,
   y < 0.5 + 0.5 x
};
RegionPlot[
 rf[x, y],
 {x, 0, 2},
 {y, 0, 4}
]

Actual function rf is very complex (CPU consuming), and I don't want to calculate it twice like here:

(* it works, but calculates rf twice *)
rf[x_, y_] := {
   y < x^2,
   y < 0.5 + 0.5 x
};
RegionPlot[
 {rf[x, y][[1]], rf[x, y][[2]]},
 {x, 0, 2},
 {y, 0, 4}
]

Using Map does not help:

rf[x_, y_] := {
   y < x^2,
   y < 0.5 + 0.5 x
};
RegionPlot[
 {#[[1]], #[[2]]}& /@ rf[x, y],
 {x, 0, 2},
 {y, 0, 4}
]
ImplicitRegion::bcond: {y,x^2}&&{y,0.5 +0.5 x} should be a Boolean combination of equations, inequalities, and Element statements. 
$\endgroup$
  • 1
    $\begingroup$ RegionPlot[Evaluate@rf[x, y], {x, 0, 2}, {y, 0, 4}] $\endgroup$ – Kuba Aug 22 '19 at 9:21
  • $\begingroup$ closely related: 113958 $\endgroup$ – Kuba Aug 22 '19 at 10:00
2
$\begingroup$

You can memoize the complex function so that it is only called once for a particular value of $x$ and $y$:

Clear[z];
z[x_, y_] := z[x,y] = {y<x^2, y<.5+.5x}

RegionPlot[
    {Indexed[z[x, y], 1], Indexed[z[x, y], 2]},
    {x, 0, 2},
    {y, 0, 4}
]

enter image description here

$\endgroup$

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.