1
$\begingroup$

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

$\endgroup$

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.

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

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

$\endgroup$
  • $\begingroup$ wow thanks, i wouldn't have thought this could be a problem :) $\endgroup$ – user104857 Nov 14 '13 at 23:37
  • $\begingroup$ 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. $\endgroup$ – Jason B. Nov 15 '13 at 0:02
  • 1
    $\begingroup$ @JasonB try SetOptions[#,PlotRange->All]&/@ {Plot, ListPlot, DateListPlot}, adding other plot functions to the list as required. $\endgroup$ – Verbeia Nov 15 '13 at 1:25
  • $\begingroup$ @Verbeia, If I wanted that to happen all the time, should I add that line to the init.m file? $\endgroup$ – Jason B. Nov 15 '13 at 3:16
  • 1
    $\begingroup$ @JasonB yes, or a package in your Applications/Autoload path $\endgroup$ – Verbeia Nov 15 '13 at 3:30

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