2
$\begingroup$

As you can see in the following photo, there are 4 regions in the photo. Each photo has a different density function $T_i$.

enter image description here

For ① $T_1=223+380 x+385y$

For ② $T_2=446+1080x+450y$

For ③ $T_3=108+4460x+450y;$

For ④ $T_4=198+4010x$

Now I want to plot the density plot of the entire image?

The method I am using is to merge $T_i$ to one $T$ with bool operator. Here is the code:

T1=223+380 x+385y;
T2=446+1080x+450y;
T3=108+4460x+450y;
T4=198+4010x;

DensityPlot[
    T1*Boole[x>=0&&y<=0.1&&(x-y<=0)]+
    T2*Boole[x<=0.10&&y>=0&&(x-y>=0)]+
    T3*Boole[x>=0.1&&y>=0&&(x+y<0.2)]+
    T4*Boole[x<=0.2&&y<=0.1&&x+y>=0.2],{x,0,0.2},
    {y,0,0.1},
    PlotPoints->100,AspectRatio->1/2,ImageSize->Medium]

The result is

enter image description here

I am a Mathematica newbie and I confirm there is better code for this problem. I've tried PlotRange & RegionFunction but had no idea how to do.

Could you please help me with more elegant way to solve this?

$\endgroup$
3
$\begingroup$

Basically equivalent but more elegant than your solution:

f1[x_, y_] := 223 + 380 x + 385 y;
f2[x_, y_] := 446 + 1080 x + 450 y;
f3[x_, y_] := 108 + 4460 x + 450 y;
f4[x_, y_] := 198 + 4010 x;

DensityPlot[
 Which[
     y > x, f1[x, y],
     y < x < .1, f2[x, y],
    .1 < x < .2 - y, f3[x, y],
     y > .2 - x, f4[x, y]],
 {x, 0, .2}, {y, 0, .1},
 PlotLegends ->Automatic,
 AspectRatio -> 1/2]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks, I think using Which is much better. $\endgroup$ – voldikss Jun 24 '19 at 0:26
2
$\begingroup$

An alternative approach: (1) Construct a list of functions (functions) from your {T1, ..., T4}, (2) make a boolean list of from your conditions (boole), (3) use Dot[boole, Through @ functions[x,y]] as the first argument of DensityPlot:

functions = Function[{x, y}, #] & /@ {T1, T2, T3, T4};
boole = Boole @ {y > x, y < x < .1, .1 < x < .2 - y, y > .2 - x};

DensityPlot[boole.Through[functions[x, y]],
   {x, 0, .2}, {y, 0, .1},
   PlotLegends -> Automatic, AspectRatio -> 1/2]

enter image description here

Add the option ExclusionsStyle -> Red to emphasize the region boundaries:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank for your solution! I don't know about Through and I am going to check it in the help. $\endgroup$ – voldikss Jun 24 '19 at 0:28
1
$\begingroup$

I prefer using Piecewise instead of Which, Boole, etc. because Piecewise has better support in mathematical operations.

T1 = 223 + 380 x + 385 y;
T2 = 446 + 1080 x + 450 y;
T3 = 108 + 4460 x + 450 y;
T4 = 198 + 4010 x;

P = DensityPlot[
  Piecewise[{{T1, x >= 0 && y <= 0.1 && (x - y <= 0)},
             {T2, x <= 0.10 && y >= 0 && (x - y >= 0)},
             {T3, x >= 0.1 && y >= 0 && (x + y < 0.2)},
             {T4, x <= 0.2 && y <= 0.1 && x + y >= 0.2}}],
    {x, 0, 0.2}, {y, 0, 0.1},
    PlotPoints -> 100, AspectRatio -> 1/2, ImageSize -> Medium, 
    PlotLegends -> Automatic, PlotRange -> All]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks, Piecewise is indeed better. I always use it in two variables problem, your solution is enlightening. $\endgroup$ – voldikss Jun 24 '19 at 23:07

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.