2
$\begingroup$

I have 3 functions below and I want to plot them on the same graph with different range as follows.

  • Plot f1 with x is in {2, 5} meaning that 2 ≤ x ≤ 5
  • Plot f2 with x is in {3, 8} meaning that 3 ≤ x ≤ 8
  • Plot f3 with x is in {1, 4} meaning that 1 ≤ x ≤ 4

I know that this can be done by plot each of them and then use Show function.
However, I'm trying to use only one Plot function to do it.
This is just an example as the number of functions and ranges are much larger than this.

f1 = 2 x;
f2 = x^2;
f3 = x^3 - 2 x^2 + 5;
functions = {f1,f2,f3};
ranges = {{2, 5}, {3, 8}, {1, 4}};
Plot[functions, ranges?]
$\endgroup$
2
  • $\begingroup$ "However, I'm trying to use only one Plot function to do it." - why? Nevertheless, you can try using ConditionalExpression[] for this. $\endgroup$ Feb 17, 2021 at 20:25
  • $\begingroup$ @J.M. I'm trying to apply this for another code so this method makes it easier for me apply it. $\endgroup$
    – emnha
    Feb 17, 2021 at 20:31

2 Answers 2

5
$\begingroup$
{xmin, xmax} = MinMax[ranges];

1. ConditionalExpression

conditionalfunctions = MapThread[
   ConditionalExpression[#, Between[#2] @ x] &, 
  {functions, ranges}];

Plot[conditionalfunctions, {x, xmin, xmax}]

enter image description here

You can also define conditionalfunctions as follows:

conditionalfunctions1 = MapThread[ConditionalExpression[#, #2 @ x] &] @ 
  {functions, Between /@ ranges}

conditionalfunctions2 = MapThread[ConditionalExpression] @
  {functions, Between[x, #] & /@ ranges}

conditionalfunctions3 = Thread @ 
  ConditionalExpression[functions, Between[x, #] & /@ ranges]

2. Piecewise

piecewisefunctions = MapThread[Piecewise[{{#, Between[x, #2]}}, Undefined] &] @
  {functions, ranges};

Plot[piecewisefunctions, {x, xmin, xmax}]

enter image description here

3. Clip

functionswithclippedarg = MapThread[# /. x -> Clip[x, #2, {Null, Null}] &] @
  {functions, ranges};

Plot[functionswithclippedarg, {x, xmin, xmax}]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ Using Between[]: Through[(Between /@ ranges)[x]], or MapThread[ConditionalExpression[#1, Between[x, #2]] &, {functions, ranges}]. $\endgroup$ Feb 17, 2021 at 20:28
  • $\begingroup$ Thank you @J.M.; that's much cleaner. $\endgroup$
    – kglr
    Feb 17, 2021 at 20:31
3
$\begingroup$
Clear["Global`*"]

f1 = 2 x;
f2 = x^2;
f3 = x^3 - 2 x^2 + 5;
functions = {f1, f2, f3};
ranges = {{2, 5}, {3, 8}, {1, 4}};

Show[Plot @@@ Transpose[
   {functions, Insert[#, x, 1] & /@ ranges}],
 PlotRange -> All]

enter image description here

To use different PlotStyles

colors = {Red, Blue, Green};

Show[Plot[#[[1]], #[[2]], PlotStyle -> #[[3]]] & /@ Transpose[
   {functions, Insert[#, x, 1] & /@ ranges, colors}],
 PlotRange -> All]

enter image description here

$\endgroup$

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.