9
$\begingroup$

I have a 2D Plot of 3 functions

Plot[ { f1[x], f2[x], f3[x] }, { x, 8, 18 } ]

where I want f3[x] only plotted for the range [10, 18] instead of [8, 18]. Is that possible?

$\endgroup$
15
$\begingroup$

You can use ConditionalExpression (new in version 8) e.g.

Plot[{f1[x], f2[x], ConditionalExpression[f3[x], x > 10]}, {x, 8, 18}]

For example, let's define :

f1[x_] := -4/5 Sin[x]
f2[x_] := Sin[2 x]^2 - 1/2
f3[x_] := Sin[3 x]^5

now

Plot[{ f1[x], f2[x], ConditionalExpression[f3[x], x > 10]}, {x, 8, 18},
       PlotStyle -> {Thick, Thick, Thickness[0.007]}]

enter image description here

The thickest curve is the graph of of f3 in the expected region though f3 is defined for all real (even complex) numbers.

$\endgroup$
  • $\begingroup$ Perfect. Thanks a bunch. $\endgroup$ – stevenvh Jun 20 '12 at 9:55
  • 2
    $\begingroup$ Alternatively, there's Plot[{f1[x], f2[x], Piecewise[{{f3[x], x > 10}}, Indeterminate]}, {x, 8, 18}]. $\endgroup$ – J. M. will be back soon Jun 20 '12 at 10:49
  • $\begingroup$ @stevenvh I'm glad I could help. $\endgroup$ – Artes Jun 20 '12 at 12:01
  • $\begingroup$ @J.M. that version also works on v7 whereas ConditionalExpression does not. Please post that as an answer, or I shall. $\endgroup$ – Mr.Wizard Jun 20 '12 at 17:43
9
$\begingroup$

A solution using individual plots combined using Show.

f1[x_] := 1/4 Sin[x]
f2[x_] := 1/2 Sin[2 x]^2
f3[x_] := Sin[3 x]^3

Define a function to plot some functions over some ranges:

Attributes@plotFuncs = {HoldFirst};
plotFuncs[{funcs_, ranges_, opts_}] := 
 Show[Block[Evaluate@Union@ranges[[All, 1]], 
   MapThread[
    Plot[#1[First@#2], #2, #3] &, {funcs, ranges, 
     opts}]], PlotRange -> All]

Plot the three functions:

plotFuncs[{{f1, f2, f3}, {{x, 8, 18}, {y, 8, 18}, {x, 10, 18}}, 
{PlotStyle -> Red, PlotStyle -> Blue, PlotStyle -> Darker@Green}}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks for your answer. I'm a beginner at Mathematica, and accepted Artes' answer because it's easier to understand. But thanks again. $\endgroup$ – stevenvh Jun 20 '12 at 16:21
  • $\begingroup$ #@stevenvh No problem, glad you got the answer you needed :) $\endgroup$ – image_doctor Jun 20 '12 at 16:49
7
$\begingroup$

At Mr. Wizard's urging: Plot[{f1[x], f2[x], Piecewise[{{f3[x], x > 10}}, Indeterminate]}, {x, 8, 18}] works nicely as an alternative to Artes's answer.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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