How Can I Define a Stepwise Function in Mathematica for Atmospheric Modeling?

It seems like there are so many different ways to do the same thing. But I guess that's the beauty of it.
  • #1
laminatedevildoll
211
0
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.
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Which version of Mathematica are you using?

As George suggested, you can use the Heaviside function
Code:
( HeavisideTheta[x - 1] - HeavisideTheta[x - 3] ) x^2

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

You can use Which
Code:
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:
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:
  • #4
CompuChip said:
Which version of Mathematica are you using?
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
{{0, 0}, {0, 1}, {3.5`, 1}, {3.5`, 2}, {5.5`, 2}, {5.5`, 1.5`}, {9.5`,
1.5`}, {9.5`, 2}, {15, 2}, {15, 1}, {24, 1}}
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.
 

Attachments

  • stepwise.JPG
    stepwise.JPG
    4.4 KB · Views: 613
Last edited:
  • #5
Code:
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:
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}]
 
  • #6
For the vertical lines, you can also use the Exclusions option:

Code:
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]
 
  • #7
CompuChip said:
Code:
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:
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}]

Thanks for your help. :smile:
 
  • #8
Moo Of Doom said:
For the vertical lines, you can also use the Exclusions option:

Code:
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]

Thanks. :smile: I am still trying to get adjusted to Mathematica.
 

What is a stepwise function?

A stepwise function, also known as a piecewise function, is a mathematical function that is defined by different formulas or expressions for different parts of its domain. It typically consists of multiple "steps" or pieces that are connected at specific points.

How do I define a stepwise function in Mathematica?

To define a stepwise function in Mathematica, you can use the Piecewise function. The syntax is: f[x_]:=Piecewise[{{expression1, condition1}, {expression2, condition2}, ...}], where x is the variable, expression is the formula or expression for that part of the domain, and condition is the range of values for which the expression is valid.

Can I plot a stepwise function in Mathematica?

Yes, you can plot a stepwise function in Mathematica using the Plot function. The syntax is: Plot[f[x], {x, xmin, xmax}], where f[x] is the stepwise function and xmin and xmax are the minimum and maximum values for the x-axis.

How do I find the derivative of a stepwise function in Mathematica?

To find the derivative of a stepwise function in Mathematica, you can use the D function. The syntax is: D[f[x], x], where f[x] is the stepwise function and x is the variable. If the stepwise function has multiple pieces, the derivative will be different for each piece.

Can I integrate a stepwise function in Mathematica?

Yes, you can integrate a stepwise function in Mathematica using the Integrate function. The syntax is: Integrate[f[x], {x, xmin, xmax}], where f[x] is the stepwise function and xmin and xmax are the minimum and maximum values for the integration interval. However, the integration result may differ from the actual value due to the discontinuities in the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top