Step Function: Numerical Precision Matters

In summary, the conversation discussed the use of step functions in numerical calculations and the challenges that arise due to precision and small values. Different suggestions were given, including using a package or routine to handle arbitrary floating point numbers, scaling the numbers, or choosing a different system of units. The possibility of comparing the floats memory specific value to a hardcoded value of 0 was also mentioned. Overall, constructing step functions in Fortran or other computation language can be done using an explicit branch condition or the analytical version, but both have their limitations and require knowledge of numeric analysis.
  • #1
Goose
5
0
Hi, Friends

How to numerically do with step function? I mean, this function is very sensitive to the precision. For example, f(x) is a step function. When x is greater than some very small positive number, \delta, the numerical answer is 1, which agrees with the expected value. But when x < \delta, the computer cannot tell it from 0, therefore the numerical result, f(x), equals to ZERO, which is not what I want.

Thanks for help

Goose
 
Physics news on Phys.org
  • #2
Hi Goose.

I don't really see what kind of step function you are using and what it is good for. Usually a step function can be defined as [itex]\Theta_a(x) \equiv \Theta(x - a)[/itex] where
[tex]\Theta(x) = \begin{cases} 0 & \text{ if } x < 0 \\ \frac12 & \text{ if } x = 0 \\ 1 & \text{ otherwise} \end{cases}[/tex]
In numerical calculations this also works fine... where does delta come in?
 
  • #3
If you want a continuous analytic solution you can try looking up articles on Fourier transforms dealing with the signum function. If you get a book dealing with various types of waveforms like sawtooth waves it will have a chance of containing that function (I can't remember it off the top of my head).

If you don't care about an analytic solution, just do what the poster described above and use some hard and fast boundary rules. The continuous analog exhibits the Gibbs phenomenon so you'll see weird effects around the boundaries but that's to be expected.
 
  • #4
Thanks. My step function is the same as what CompuChip described. I know the exact analytical meaning of this function. But my question is how to define this function by Fortran or other computation language. In my research, I simply wrote subroutine using if statement as CoompuChip said. But the result is not desirable because computer doesn't know a very small number located in the neighbor of ZERO. For example, if [itex] x = 10^{-50} [/itex], the result is 0, not the expected value, 1. This difference affects my research a lot, and then my result always doesn't agree with my adviser's. Is there another way to construct step function?

Thanks again.
 
  • #5
Ah, I see the problem now.
In that case I must leave answering your question to more qualified people. The only suggestions I can make are to a) find a package / routine / whatever it's called in Fortran which allows you to handle arbitrary floating point numbers and/or scientific notation; b) if you are only interested in values close to zero, scale everything by a factor 10^60 such that 10^{-50} is mapped to 10^{10}; c) check how your advisor did it.

But I hope people more experienced in numerical calculations will offer a good solution.
 
  • #6
I agree with CompuChip's suggestion to scale the numbers, if you can do so. Or choose a different system of units, so that you are not dealing with such small values.

That being said, here is a question for Goose: are you using double precision variables? They would handle numbers as small as 10-307, or somewhere thereabouts, before treating the number as zero.
 
  • #7
Good suggestions. Thank you all. I'll try it again.
 
  • #8
Goose said:
Thanks. My step function is the same as what CompuChip described. I know the exact analytical meaning of this function. But my question is how to define this function by Fortran or other computation language. In my research, I simply wrote subroutine using if statement as CoompuChip said. But the result is not desirable because computer doesn't know a very small number located in the neighbor of ZERO. For example, if [itex] x = 10^{-50} [/itex], the result is 0, not the expected value, 1. This difference affects my research a lot, and then my result always doesn't agree with my adviser's. Is there another way to construct step function?

Thanks again.

If your using the explicit branching definition then you could probably do one of two things:

a) Define an absolute minimum delta value and always compare against that. Note that anything between the delta and zero will naturally using this definition be defined as zero and not as the number with regards to function behaviour.

b) Compare the floats memory specific value to a hardcoded value of 0. This is a few extra lines because of the intrinsics of how floating point numbers are represented with the mantissa and with things such as NaN and so forth. One hint I can say from experience is that -0 and +0 are both zero so you should always add +0 to the number before checking the footprint to avoid that ambiguity.

You asked if there was any other way to do this. The best way is to do what you've done and use an explicit branch condition. Another way is to use the analytical version. It however is prone to so much error that you would need to know a bit about numeric analysis to make the output stable at certain values.

Anyway you've probably got it by now so i'll leave it there
 

1. What is a step function?

A step function is a mathematical function that increases or decreases abruptly at specific points, resulting in a series of flat, vertical lines. It is also known as a staircase function or a Heaviside function.

2. How is numerical precision important in step functions?

Numerical precision refers to the level of accuracy in representing numbers in a computer. In step functions, small changes in the numerical precision can result in significant changes in the output, leading to errors in calculations and predictions.

3. What are the potential consequences of numerical precision errors in step functions?

Numerical precision errors can lead to incorrect results and predictions in scientific experiments or simulations that use step functions. This can have serious consequences, especially in fields such as physics and engineering where precise calculations are crucial.

4. How can we ensure numerical precision in step functions?

To ensure numerical precision in step functions, it is important to use high-precision data types and algorithms in computer programs. Additionally, regularly checking and adjusting the precision levels can help minimize errors.

5. Are there any alternatives to using step functions?

Yes, there are alternative mathematical functions that can be used instead of step functions. For example, sigmoid functions or piecewise linear functions can also model abrupt changes, but with smoother transitions and less sensitivity to numerical precision errors.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
Replies
3
Views
333
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Replies
3
Views
1K
Replies
18
Views
2K
  • Programming and Computer Science
2
Replies
63
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top