Not getting the range I want when plotting a function with Show [duplicate]

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 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

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

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

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 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}}] • 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
• @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
• @JasonB yes, or a package in your Applications/Autoload path – Verbeia Nov 15 '13 at 3:30