Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Stepwise Function Mathematica

  1. Aug 1, 2007 #1
    How do I define a stepwise function in Mathematica? I am trying to model the behavior of a detector up in the atmosphere. For instance, the detector might experience temperature drops in the atmosphere over a 24 hour period. I would like to know if there's any way of using a step function in Mathematica to do that. Thanks.
  2. jcsd
  3. Aug 2, 2007 #2

    George Jones

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Yes, you can use the Heavisde step function to do this. For exmple,

    [tex]\left( Heaviside \Heaviside \left( x-1 \right) - Heaviside \left( x-3 \right) \right) x^2[/tex]

    is the the function [itex]x^2[/itex] for [itex]1 < x < 3[/itex], and zero elsewhere.

    Heaviside is a Maple function, but Mathematica will have a similar function, with maybe a different name.
  4. Aug 2, 2007 #3


    User Avatar
    Science Advisor
    Homework Helper

    Which version of Mathematica are you using?

    As George suggested, you can use the Heaviside function
    Code (Text):

    ( HeavisideTheta[x - 1] - HeavisideTheta[x - 3] ) x^2
    You can do
    Code (Text):

    f[x_] := 0;
    f[x_] := x^2 /; (x > 1 && x < 3)
    which is ugly but works.

    You can use Which
    Code (Text):

    g[x_] := Which[x < 1, 0, x > 3, 0, True, x^2];
    which is better, but has the unfortunate property that it Hold[]s its arguments, so this won't do if you want to apply functions and replacements to this.

    The most elegant way, in my opinion, is using the Piecewise function
    Code (Text):

    h[x_] := Piecewise[{{x^2, 1 < x < 3}}, 0]
    but this function was implemented in 5.1 so that won't help you if you have an older version.
    Last edited: Aug 2, 2007
  5. Aug 3, 2007 #4
    I am using version 6.0.

    I actually tried to do a piecewise function but it didn't quite work out. Instead, I just plot the points and connected it so that it looks like a stepwise function. I have attached the plot I want to this post. However, I need to learn how to do achieve this shape the right way with a stepwise function and not just points, because in the future I will need to replace this function instead of a sine function into two differential equations to solve it.

    The points I am using are for the attached plot are
    The x values are fixed. The y values can change but the same shape needs to be achieved. If this is impossible to do with a step function, is it possible to model this using a cubic spline function with the same general shape as the step function?
    I would appreciate any help.

    Attached Files:

    Last edited: Aug 3, 2007
  6. Aug 4, 2007 #5


    User Avatar
    Science Advisor
    Homework Helper

    Code (Text):

    Plot[Piecewise[{{1, x < 3.5}, {2, x < 5.5}, {1.5, x < 9.5}, {2,
        x < 15}}, 1], {x, 0, 25}, PlotRange -> {0, 2}]
    worked fine here (Mathematica 6.0), without the vertical lines (they finally fixed that bug :smile:).

    But if you insist on the vertical lines, you can use
    Code (Text):

    Plot[Which[x < 3.5, 1, x < 5.5, 2, x < 9.5, 1.5, x < 15, 2, True,
      1], {x, 0, 25}, PlotRange -> {0, 2}]
  7. Aug 4, 2007 #6
    For the vertical lines, you can also use the Exclusions option:

    Code (Text):
    Plot[Piecewise[{{1, x < 3.5}, {2, x < 5.5}, {1.5, x < 9.5}, {2,
        x < 15}}, 1], {x, 0, 25}, PlotRange -> {0, 2}, Exclusions -> None]
  8. Aug 7, 2007 #7
    Thanks for your help. :smile:
  9. Aug 7, 2007 #8
    Thanks. :smile: I am still trying to get adjusted to Mathematica.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - Stepwise Function Mathematica Date
Mathematica Cannot do the integral of the Hyper-geometric function? Mar 11, 2018
Mathematica Cannot Plot This Function in Mathematica Feb 26, 2018
Mathematica While Loop in Mathematica Feb 25, 2018
Matlab Dotting autonomous functions Jan 24, 2018
Mathematica Piecewise Function Jan 18, 2018