Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to plot f1[x] over 0 < x < L/2 and f2[x] over L/2 < x < L (i.e. so that f1[x] isn't displayed overL/2 < x < L, etc.). How do I do this?

share|improve this question
3  
You could use Piecewise for instance. –  b.gatessucks May 21 '12 at 9:45
    
    
Strongly related: "Can I limit PlotRange for 1 function in a Plot?" The presented solution with ConditionalExpression is even more succinct than with Piecewise. –  Alexey Popkov Jun 20 '12 at 14:58
add comment

4 Answers

up vote 9 down vote accepted

As b.gatessucks commented, use Piecewise. For example:

f1[x_] := Sin[x]
f2[x_] := Cos[x]

L = 7;

pw = Piecewise[{{f1@#, 0 < # < L/2}, {f2@#, L/2 < # < L}}, Indeterminate] &;

Plot[pw[x], {x, 0, L}]

Mathematica graphics

Generally you should not use capital letters for variable names (L) but I kept your notation in this case.

share|improve this answer
add comment

Piecewise would work but displays a spurious line at 0 vertical coordinate:

f[x_] := Sin[x];
Plot[Piecewise[{{f[x], x < 2 Pi}}], {x, 0, 4 Pi}]

Mathematica graphics

(note the horizontal line for $x>2\pi$).

One can avoid this by simply displaying a bigger range than has been plotted, as follows:

Show[
 Plot[f[x], {x, 0, 2 Pi}],
 PlotRange -> {{0, 4 Pi}, {-1, 1}}
 ]

Mathematica graphics

There is now nothing at $x>2\pi$.

share|improve this answer
    
Or just use Indeterminate. :-) –  Mr.Wizard May 21 '12 at 9:56
    
@Mr.Wizard yes I saw it in your answer. didn't think of it! –  acl May 21 '12 at 9:59
    
It is not spurious either, because the default value if none of the conditions apply, is 0. –  rm -rf May 21 '12 at 16:26
    
@R.M Right, I meant "undesirable". Sorry for the confusion –  acl May 21 '12 at 16:29
add comment

Or without the use of piecewise:

gg1 = Plot[Cos[x], {x, \[Pi]/2 + $MachineEpsilon, \[Pi]}];
gg2 = Plot[Sin[x], {x, 0, \[Pi]/2}];

Show[gg2, gg1, PlotRange -> {{0, \[Pi]}, {-1, 1}}]

Mathematica graphics

Here is a more integrated version with it bundled up into a function which takes a list of functions to plot and an arbitrary number of ranges:

Clear[DisjointRangePlot2D];
DisjointRangePlot2D[fs_List, xRanges_List] := 
 Module[{x}, 
  Show[MapThread[
    Plot[#1[x], Evaluate@Flatten@{x, #2}] &, {fs, xRanges}], 
   PlotRange -> {{Min@First@(xRanges\[Transpose]), 
      Max@Last@(xRanges\[Transpose])}, Automatic}]]
DisjointRangePlot2D[f_, xRanges_List] := 
 DisjointRangePlot2D[Table[f, {Length@xRanges}], xRanges]

A selected plot with a single function:

DisjointRangePlot2D[Sin, {{0, \[Pi]}, {2 \[Pi], 4 \[Pi]}, {7 \[Pi], 10 \[Pi]}}]

Mathematica graphics

A plot with multiple functions and ranges:

DisjointRangePlot2D[{Sin, Cos, Cos}, {{0, \[Pi]}, {2 \[Pi], 4 \[Pi]}, {7\[Pi], 10 \[Pi]}}]

Mathematica graphics

I did play with passing opts:OtpionsPattern[] to Plot or Show to give more control over the final output, but I didn't succeed in making that work.

share|improve this answer
    
This works in this case, but one should be aware that the option settings of the first plot are used, so this may lead to unintended results sometimes. –  Sjoerd C. de Vries May 21 '12 at 10:48
add comment

We needn't use Piecewise, alternatively one can use Condition (/;) and/or ConditionalExpression (new in version 8).

f1[x_] := Sin[x]
f2[x_] := Cos[x]

Here are respective definitions :

f[x_] /; 0 <= x <= Pi := f1[x]
f[x_] /; Pi < x <= 2 Pi := f2[x]

or a sligtly different way :

ff[x_] /; x <= L/2 := f1[x]
ff[x_] /; L/2 < x  := f2[x]

we can do a similar construction in a more flexible way assuming e.g. dependence of the function on the parameter L :

g[x_, L_] /; 0 <= x <= L    := ConditionalExpression[f1[x], 0 <= x <= L]
g[x_, L_] /; L <  x <= 2 Pi := ConditionalExpression[f2[x], L < x <= 2 Pi]

to plot these functions we can make use of e.g. Exclusions option, let's demonstrate how it works :

GraphicsRow[{ Plot[f[x], {x, 0, 2 Pi}, Exclusions -> {x == Pi}],
              Plot[f[x], {x, 0, 2 Pi}] }]

enter image description here

In case of ff function we can use RegionFunction option, to plot an appropriately restricted range, e.g.

L = 2 Pi;
GraphicsRow[{ Plot[ ff[x], {x, 0, L}, Exclusions -> {x == Pi}, PlotStyle -> Thick], 
              Plot[ ff[x], {x, -Pi/2, 5/2 Pi}, Exclusions -> {x == Pi}, 
                    PlotStyle -> Thick, RegionFunction -> Function[{x, y}, L > x > 0]] }]

enter image description here

We show here something more customized :

Animate[ Plot[ g[x, L], {x, 0, 2 Pi}, PlotRange -> {{0, 2 Pi}, {-1.05, 1.05}},
              Exclusions -> {x == L}, PlotStyle -> Thickness[0.01],
              ColorFunction -> "DeepSeaColors", ImageSize -> {500, 500}], 
         {L, 0, 2 Pi}]

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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