# Piecewise function

**URL:** <https://community.wolfram.com/t/piecewise-function/4153>\
**Category:** Questions\
**Tags:** control-systems, engineering, finance, material-sciences, physics\
**Created:** [November 28, 2014, 11:39am UTC](https://community.wolfram.com/t/piecewise-function/4153 "2014-11-28T11:39:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmajnooni](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/pmajnooni/32/17243_2.png) [@pmajnooni](https://community.wolfram.com/u/pmajnooni)\
**Post date:** [November 28, 2014, 11:39am UTC](https://community.wolfram.com/t/piecewise-function/4153/1 "2014-11-28T11:39:53Z")

</div>

Dear friends

I need to define and plot a 6-piecewise function but the software only accept 3 and when i try to add more, the font color changes to red and the program does not execute. Can anyone help me?  
All the best

---

<div class="post-metadata">

**Author:** ![sblinder](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sblinder/32/17861_2.png) [@sblinder](https://community.wolfram.com/u/sblinder)\
**Post date:** [November 28, 2014, 5:46pm UTC](https://community.wolfram.com/t/piecewise-function/4153/2 "2014-11-28T17:46:33Z")

</div>

Try defining a 2-part piecewise function, each piece itself being a 3-part piecewise function. Or, alternatively, 3 then 2.

---

<div class="post-metadata">

**Author:** ![dlkeith](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dlkeith/32/31494_2.png) [@dlkeith](https://community.wolfram.com/u/dlkeith)\
**Post date:** [November 28, 2014, 5:49pm UTC](https://community.wolfram.com/t/piecewise-function/4153/3 "2014-11-28T17:49:27Z")

</div>

The region specifications are a list of lists. You forgot the outer brackets.

Best, David

```
(* this *)
Piecewise[{0, x < 1}, {1, x < 2}, {3, x < 3}, {4, x < 4}, {5, True}]

(* should be this *)
Piecewise[{{0, x < 1}, {1, x < 2}, {3, x < 3}, {4, x < 4}, {5, True}}]

```

---

<div class="post-metadata">

**Author:** ![pmajnooni](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/pmajnooni/32/17243_2.png) [@pmajnooni](https://community.wolfram.com/u/pmajnooni)\
**Post date:** [November 30, 2014, 2:58pm UTC](https://community.wolfram.com/t/piecewise-function/4153/4 "2014-11-30T14:58:32Z")

</div>

Thanks to Blinder and David

Via your instructions, I plotted the 6-piecewise function (Blue) and a 6th order polynomial function (Pink) at the same time. but the piecewise function has a discontinuity at x=-2 and a bit right while it shouldn’t be like that since it is a 3rd order polynomial!!! Any reason?

 ![NewPlot.jpg](https://global.discourse-cdn.com/wolfram/original/3X/b/1/b171ac2e2195f8e8e3ea25fab515ee6af7e64cee.jpeg)

Enclosed find the code. Any comment will be welcome.

---

<div class="post-metadata">

**Author:** ![dlkeith](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/dlkeith/32/31494_2.png) [@dlkeith](https://community.wolfram.com/u/dlkeith)\
**Post date:** [November 30, 2014, 6:09pm UTC](https://community.wolfram.com/t/piecewise-function/4153/5 "2014-11-30T18:09:24Z")

</div>

Hi Philip,

I see the same thing. It can be eliminated by increasing the number of points plotted. But I do think it would be better if Plot knew to plot to the endpoints of segments.

```
Plot[{Piecewise[{{0.6 - 0.3692*(x + 3) - 0.0308*(x + 3)^3, -3 < 
      x <= -2}, {0.2 - 0.46154*(x + 2) - 0.0923*(x + 2)^2 + 
      0.95385*(x + 2)^3, -2 < x <= -1}, {0.6 + 2.2154*(x + 1) + 
      2.7692*(x + 1)^2 - 2.5846*(x + 1)^3, -1 < x <= 0}, {3 - 
      4.9846*x^2 + 2.58462*x^3, 
     0 < x <= 1}, {0.6 - 2.21538*(x - 1) + 2.76923*(x - 1)^2 - 
      0.95384*(x - 1)^3, 
     1 < x <= 2}, {0.2 + 0.46154*(x - 2) - 0.0923*(x - 2)^2 + 
      0.0308*(x - 2)^3, 2 < x <= 3}}], 
  3 - 3.20667*x^2 + 0.866667*x^4 - 0.06*x^6}, {x, -3, 3}, 
 PlotPoints -> 1000]

```
