0
$\begingroup$

I have a discrete function (finite set of values) defined on an area $\Omega$ in $R^2$. Each value of the function is defined on a subarea $\Omega_i$(triangle in my case). These triangles account for the whole area $\Omega=\bigcup \Omega_i$. I need create a new piecewise function which would be constant on each triangle, but defined on the $\Omega$. How one can do it?

I can define a piecewise function for distinct triangles. Then I have to union all of them into the single function. So, turning an array of piecewise functions into the single piecewise function would solve my problem.

For example:

n = 4;
tr1 = Triangle[{{0, 0}, {0.25, 0}, {0.125, 0.25}}];
tr2 = Triangle[{{0.25, 0}, {0.375, 0.25}, {0.125, 0.25}}];
tr3 = Triangle[{{0.25, 0}, {0.5, 0}, {0.375, 0.25}}];
tr4 = Triangle[{{0.125, 0.25}, {0.375, 0.25}, {0.25, 0.5}}];
tr = {tr1, tr2, tr3, tr4};
val = {1, 2, 3, 4};
f[x_, y_] = 
  Table[Piecewise[ {{val[[i]], {x, y} \[Element] 
       tr[[i]]} , {0, {x, y} ! \[Element] tr[[i]]}} ], {i, 1, n}];

I need the sole function, defined on $\bigcup tr_i$, which should be the sum of that four functions defined on $tr_i$:

g[x_, y_] = 
 Piecewise[ { {val[[1]], {x, y} \[Element] 
     tr[[1]]}, {val[[2]], {x, y} \[Element] 
     tr[[2]]}, {val[[3]], {x, y} \[Element] 
     tr[[3]]}, {val[[4]], {x, y} \[Element] tr[[4]]}  }]

In my problem $n$ is much bigger. How can I wrap it into a loop? Moreover, is it an effective way to create such a function? The command Plot3D[g[x, y], {x, 0, 0.25}, {y, 0, 0.25}] for example, works for too long.

$\endgroup$
4
  • $\begingroup$ So what is the question? How to union them? How about adding them together? Or define all the different triangles in one piecewise? $\endgroup$
    – Ruud3.1415
    Jan 29, 2018 at 15:15
  • $\begingroup$ I've updated the question. I just need define a new function as a sum of other functions. $\endgroup$ Jan 29, 2018 at 16:02
  • $\begingroup$ The question is how to define them into the one piecewise function $\endgroup$ Jan 29, 2018 at 16:20
  • $\begingroup$ how about defining f like this:f[x_, y_] = Piecewise[Table[{val[[i]], {x, y} \[Element] tr[[i]]}, {i, 1, n}]]; $\endgroup$
    – Ruud3.1415
    Jan 29, 2018 at 17:08

1 Answer 1

1
$\begingroup$

If we define the table within the piecewise (inferring 0 unless otherwise specified) we get the following:

n = 4;
tr1 = Triangle[{{0, 0}, {0.25, 0}, {0.125, 0.25}}];
tr2 = Triangle[{{0.25, 0}, {0.375, 0.25}, {0.125, 0.25}}];
tr3 = Triangle[{{0.25, 0}, {0.5, 0}, {0.375, 0.25}}];
tr4 = Triangle[{{0.125, 0.25}, {0.375, 0.25}, {0.25, 0.5}}];
tr = {tr1, tr2, tr3, tr4};
val = {1, 2, 3, 4};
f[x_, y_] = 
Piecewise[Table[{val[[i]], {x, y} \[Element] tr[[i]]}, {i, 1, n}]];
DensityPlot[f[x, y], {x, 0, 0.25}, {y, 0, 0.25}, PlotRange -> All, 
PlotPoints -> 20, MaxRecursion -> 5]

to get:

enter image description here

or to lower computing time:

DensityPlot[f[x, y], {x, 0, 0.25}, {y, 0, 0.25}, PlotRange -> All, 
 PlotPoints -> 2, MaxRecursion -> 1]

enter image description here

$\endgroup$
7
  • $\begingroup$ Thanks. That's what I need. Is there any way to speed up the plotting? Or it is a price I should pay for working with such a function? $\endgroup$ Jan 29, 2018 at 17:33
  • $\begingroup$ Could you advice how to get red of that gaps among regions? ContourPlot[f[x, y], {x, y} [Element] = RegionUnion[tr1, tr2, tr3, tr4], PlotLegends -> Automatic, ColorFunction -> "TemperatureMap"] $\endgroup$ Jan 29, 2018 at 17:44
  • $\begingroup$ Would 'DensityPlot' work in your case? It would be a lot faster $\endgroup$
    – Ruud3.1415
    Jan 29, 2018 at 17:45
  • $\begingroup$ I've tried it. A bit faster. Gaps are what really worry me. I think, that performance will increase when I get red of them. $\endgroup$ Jan 29, 2018 at 17:50
  • $\begingroup$ If your only interested in a graph I would look into Graphics and Triangle,plotting would not be the way to go for large n $\endgroup$
    – Ruud3.1415
    Jan 29, 2018 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.