MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Plotting a table of piecewise functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105427] Re: [mg105389] Plotting a table of piecewise functions
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Wed, 2 Dec 2009 06:26:13 -0500 (EST)
  • Reply-to: hanlonr at cox.net

Convert cells to InputForm prior to copy and paste to e-mail.

pdf[x_] = {
   Piecewise[{{x^2/9, 0 <= x <= 3}}],
   Piecewise[{{x/4, 0 < x < 2}, {1/2, 2 <= x < 3}}],
   Piecewise[{{4 x Cos[x]^2/Pi^2, 0 < x < Pi}}]};

Attributes[Plot]

{HoldAll,Protected}

Since Plot has attribute HoldAll, until pdf[x] is evaluated pdf[x] is a single entity not a list, so Plot allocates it a single color. 

Use Evaluate to force its evaluation early. With Evaluate it is equivalent to your manually entering the three terms.

Plot[Evaluate[pdf[x]], {x, -1, 4},
 PlotStyle -> {Red, Green, Blue}]

cdf[x_] = Assuming[{Element[x, Reals]},
   Integrate[pdf[t], {t, -Infinity, x}]];

Plot[Evaluate[cdf[x]], {x, -1, 4},
 PlotStyle -> {Red, Green, Blue}]


Bob Hanlon

---- michael partensky <partensky at gmail.com> wrote: 

=============
Hi!
I have a table of three piecewise functions:

pdf[x]= {\[Piecewise] {
   {(x^2/9), 0 <= x <= 3},
   {0, \!\(\*
      TagBox["True",
       "PiecewiseDefault",
       AutoDelete->False,
       DeletionWarning->True]\)}
  }, \[Piecewise] {
   {0.25 x, 0 < x < 2},
   {0.5, 2 <= x < 3},
   {0, \!\(\*
      TagBox["True",
       "PiecewiseDefault",
       AutoDelete->False,
       DeletionWarning->True]\)}
  }, \[Piecewise] {
   {((4 x Cos[x]^2)/\[Pi]^2), 0 < x < \[Pi]},
   {0, \!\(\*
      TagBox["True",
       "PiecewiseDefault",
       AutoDelete->False,
       DeletionWarning->True]\)}
  }}

I would like to make a plot of it.

Plot[pdf[x],{x,-1,4},PlotStyle->{Red,Green,Blue}] ignores the colors and
interrupts some of the curves .


Plot[{pdf[x][[1]],pdf[x][[2]],pdf[x][[3]]},{x,-1,4},PlotStyle->{Red,Green,Blue}]
does the job as expected.

Why are the results different? What is wrong with the first approach?

Thanks.
Michael.




  • Prev by Date: Re: Simple Spreadsheet within Mathematica?
  • Next by Date: Re: Combine images, Show[] and its effect on
  • Previous by thread: Re: Plotting a table of piecewise functions
  • Next by thread: Re: Replacement rules that contain function arguments (evaluation