1

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
user104857
  • 55
  • 3

1 Answers1

3

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

Jason B.
  • 68,381
  • 3
  • 139
  • 286