# Fill within curves

p[x_, left_, right_] := HeavisideTheta[x - left] HeavisideTheta[right - x]

Plot[{(1400 + 200 x) p[x, 0, 0.77],
3460 x p[x, 0, 0.36],
(1172 + 214 x) p[x, 0.36, 0.77]},
{x, 0, 0.77}]


I have used above code to create the plot. I tried filling curve but I require it to be fill as marked in figure.

## 3 Answers

Here is one way to do it.

p[x_, left_, right_] :=  HeavisideTheta[x - left] HeavisideTheta[right - x]

Show[Plot[{(1400 + 200 x) p[x, 0, 0.77],
3460 x p[x, 0, 0.36], (1172 + 214 x) p[x, 0.36, 0.77]}, {x, 0,
0.36}, Filling -> {1 -> {2}}, PlotRange -> All],
Plot[{(1400 + 200 x) p[x, 0, 0.77],
3460 x p[x, 0, 0.36], (1172 + 214 x) p[x, 0.36, 0.77]}, {x, 0.36,
0.77}, Filling -> {1 -> {3}}, PlotRange -> All]] With

p[x_, left_, right_] := UnitStep[x - left] UnitStep[right - x]

f1 = (1400 + 200 #) p[#, 0, 0.77] &;
f2 = 3460 # p[#, 0, 0.36] &;
f3 = (1172 + 214 #) p[#, 0.36, 0.77] &;


How about

Plot[{f1@x, If[f2@x >= f3@x, f2@x, f3@x]}, {x, 0, 0.77}, Filling -> {1 -> {2}}] Or

Plot[{f1@x, If[f2@x >= f3@x, f2@x, f3@x], f2@x, f3@x}, {x, 0, 0.77}, Filling -> {1 -> {2}}, Exclusions -> All] It's better to use UnitStep instead of HeavisideTheta, becasue with the former one gets a simple derivative (and the discountinuity can be easily located): while one get a messy output for the latter: FunctionDomain[the f functions, t] also don't work with HeavisideTheta, but yield True for the UnitStep case.

• Does anyone have an idea how to get rid of the vertical line in the second plot? – corey979 Jun 9 '18 at 22:53

You can also wrap the second and third functions with ConditionalExpression[#, # > 0]&

p[x_, left_, right_] := HeavisideTheta[x - left] HeavisideTheta[right - x]

Plot[{(1400 + 200 x) p[x, 0, 0.77],
ConditionalExpression[#, # > 0] &[3460 x p[x, 0, 0.36]],
ConditionalExpression[#, # > 0] &[(1172 + 214 x) p[x, 0.36, 0.77]]},
{x, 0, 0.77},
PlotStyle -> {Red, Blue, Orange},
Frame -> True, Axes -> False,
Filling -> {1 -> {{2}, LightBlue}, 1 -> {{3}, LightBlue}}] 