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.

This question already has an answer here:

I have a problem with plotting a piecewise defined function with "Show". My code is

f1[u_] := Piecewise[
{{2/Sqrt[1 + u], u < 2},
{1/(Sqrt[1 + u*((u + Sqrt[-4 + u^2])/2)^3]) + 
 1/(Sqrt[1 + u*((u - Sqrt[-4 + u^2])/2)^3]), u >= 2}}]

p := Plot[f1[u], {u, 0, 10}]
enter code here

Show[p, PlotRange -> {{0, 10}, {0.8, 2}}]

And the result is

enter image description here

btw. I use Show to plot this function with some Listplots, but the problem occurs also without the Listplots.

Interesting is also that when I define u from 0 to 5 in the definition of p, the resulting plot shows the entire function from 0 to 5 without problems ? I hope u can help me finding the error

share|improve this question

marked as duplicate by Yves Klett, Sjoerd C. de Vries, Artes, rm -rf Nov 15 '13 at 17:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Try PlotRange-> All in the definition of p. –  Sjoerd C. de Vries Nov 15 '13 at 11:03

1 Answer 1

up vote 3 down vote accepted

So when you do the plot first, when invoking p, it automatically chooses the PlotRange.

f1[u_] := 
 Piecewise[{{2/Sqrt[1 + u], 
    u < 2}, {1/(Sqrt[1 + u*((u + Sqrt[-4 + u^2])/2)^3]) + 
     1/(Sqrt[1 + u*((u - Sqrt[-4 + u^2])/2)^3]), u >= 2}}]

p := Plot[f1[u], {u, 0, 10}]
p

you get this enter image description here

So later, when you do the Show command, it won't redraw the function where it didn't do so before. Even though you've defined p with the :=, p is still invoked without the PlotRange option you feed to `Show'.

This should work for you

f1[u_] := 
 Piecewise[{{2/Sqrt[1 + u], 
    u < 2}, {1/(Sqrt[1 + u*((u + Sqrt[-4 + u^2])/2)^3]) + 
     1/(Sqrt[1 + u*((u - Sqrt[-4 + u^2])/2)^3]), u >= 2}}]

p := Plot[f1[u], {u, 0, 10}, PlotRange -> Full]
(*enter code here*)

Show[p, PlotRange -> {{0, 10}, {0.8, 2}}]

enter image description here

share|improve this answer
    
wow thanks, i wouldn't have thought this could be a problem :) –  user104857 Nov 14 '13 at 23:37
    
Yeah, personally I wish that PlotRange->All was the default option for every kind of plot, since I find myself typing that all the time. –  Jason B Nov 15 '13 at 0:02
1  
@JasonB try SetOptions[#,PlotRange->All]&/@ {Plot, ListPlot, DateListPlot}, adding other plot functions to the list as required. –  Verbeia Nov 15 '13 at 1:25
    
@Verbeia, If I wanted that to happen all the time, should I add that line to the init.m file? –  Jason B Nov 15 '13 at 3:16
1  
@JasonB yes, or a package in your Applications/Autoload path –  Verbeia Nov 15 '13 at 3:30

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