5

I want to be able to combine two piecewise functions. For example, suppose

Piecewise1 = f=3 for  1 <=x <=2  and
Piecewise2 = f=5 for 1.5<=x<=3, 

I want to insert Piecewise2 into Piecewise1 such that I get:

 f=3 for 1<x<1.5 and f=5 for 1.5<= x <3.

Notice that, for the overlap in [1.5,2], I prioritize the Piecewise I insert.

MarcoB
  • 67,153
  • 18
  • 91
  • 189
Giorgos
  • 51
  • 2

1 Answers1

1
f1[x_] := Piecewise[{{3, 1 <= x <= 2}}];
f2[x_] := Piecewise[{{5, 1.5 <= x <= 3}}];
f[x_] := PiecewiseExpand[Insert[f2[x], f1[x][[1, 1]], {1, 2}]]
(* or f[x_] := PiecewiseExpand[Insert[f1[x], f2[x][[1, 1]], {1, 1}]] *)
f[x]

Mathematica graphics

kglr
  • 394,356
  • 18
  • 477
  • 896