I have two functions f1 and f2. They have a square region in the $x-y$ plane as their domain. They might intersect, or they might not. Example:

(* {{-480., -180.}, {68., 268.}} *)
(* {{-729., -429.}, {68., 268.}} *)

I want to create a piecewise function that is defined by the following characteristics. For every point {x0, y0}:

  • if in exclusive f1 domain, return f1[$x_0,y_0$],
  • if in exclusive f2 domain, return f2[$x_0,y_0$],
  • if in intersection, return Max[f1[$x_0,y_0$],f2[$x_0,y_0$]],
  • if elsewhere, return 0.

How would I be able to do this?


1 Answer 1

f1domain = {{-480., -180.}, {68., 268.}};
f2domain = {{-729., -429.}, {68., 268.}};

reg1 = ImplicitRegion[
 LessEqual @@ Riffle[First@f1domain, x] && LessEqual @@ Riffle[Last@f1domain, y],
       {x, y}];
reg2 = ImplicitRegion[
 LessEqual @@ Riffle[First@f2domain, x] && LessEqual @@ Riffle[Last@f2domain, y],
       {x, y}];

RegionPlot@{reg1, reg2}

enter image description here

v[x0_, y0_] := 
  RegionMember[RegionDifference[reg1, reg2], {x0, y0}], 
   f1[x0, y0],
  RegionMember[RegionDifference[reg2, reg1], {x0, y0}], 
   f2[x0, y0],
  RegionMember[RegionIntersection[reg1, reg2], {x0, y0}], 
   Max@{f1[x0, y0], f2[x0, y0]}, 
  Not@RegionMember[RegionUnion[reg1, reg2], {x0, y0}],

v[-600, 150]
v[-300, 150]
v[-450, 150]
v[-450, 1500]

f2[-600, 150]

f1[-300, 150]

Max[f1[-450, 150], f2[-450, 150]]


  • $\begingroup$ Thanks for the answer. As a further question, if you do not mind, I would like to know the following: How can I write a module that accepts f1 and f2 and returns v? Having a hard time with this one. I want to keep "stitching" more functions using this module. $\endgroup$
    – triplebig
    Nov 23, 2016 at 0:50
  • $\begingroup$ You'd have to create the regions and Which beforehand. With f1domain replaced with f1["Domain"], sth like func[x0_,y0_,f1_,f2_,...]:=Module[{reg1,reg2,...,v},v[x0_,y0_]:=...,reg1=...;reg2=...;v[x0,y0]] $\endgroup$
    – corey979
    Nov 23, 2016 at 8:33

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.