3
$\begingroup$

Let us assume that we have two real functions $f(x_1,x_2)$ and $g(x_1,x_2)$.

I want to plot a 2D region of $\{(x_1, x_2)| f(x_1, x_2) \geq g(x_1, x_2)\}$.

The Region function seems cannot work well if f and g are complicated. Is there any suggested function in Mathematica to do this job?

$\endgroup$

2 Answers 2

6
$\begingroup$

It is quite difficult to understand what problems you faced, since you have not provided explicit code, so I am giving a very simple command just to get you started.

Assume the following functions

f[x1_][x2_] := x1^2 + 2*x2
g[x1_][x2_] := x1 + 2/x2

and then we can use

RegionPlot[f[x1][x2] >= g[x1][x2], {x1, 0, 4}, {x2, 0, 11}]

which returns a nice plot.

Another workaround that comes to mind is to use the Plot3D command. As an example

Plot3D[{f[x1][x2], g[x1][x2]}, {x1, -5, 5}, {x2, -10, 10}, 
 PlotLegends -> "Expressions"]
$\endgroup$
1
  • $\begingroup$ Thanks for your answer. But when f and g are rather complicated and computationally demanding, RegionPlot sometimes will generate unsatisfactory results. For example, some area which should be colored might become blank. $\endgroup$
    – xingyu fu
    May 5, 2021 at 0:57
6
$\begingroup$
f = x1 + x2*Sin[x1];
g = x1*x2 + Cos[x2]*x1;
ContourPlot[f - g, {x1, -10, 10}, {x2, -10, 10}, Contours -> {0}, 
 ContourShading -> {None, Red}, PlotPoints -> 80]

enter image description here

$\endgroup$
3
  • 2
    $\begingroup$ +1 Note that you get better resolution also using MaxRecursion rather than just using PlotPoints, e.g., PlotPoints -> 50, MaxRecursion -> 8 $\endgroup$
    – Bob Hanlon
    May 4, 2021 at 13:10
  • $\begingroup$ That's a nice, nifty trick $\endgroup$
    – kcr
    May 4, 2021 at 13:17
  • $\begingroup$ @BobHanlon Thanks! MaxRecursion work. $\endgroup$
    – cvgmt
    May 4, 2021 at 13:17

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.