What is the issue with my step function code and how can I fix it?

In summary, the conversation discusses a code written for a step function that is supposed to produce a function with a range from a to b, and is 1 within that range and 0 otherwise. The conversation also mentions an attached plot that shows some odd results with the smooth approximation using the erf function. The expert suggests using MATLAB to debug the code and localizing the error to a single line. They also mention that the boundaries are being checked using <= and >=, which includes both extremities. The expert suggests deciding which of step_fn(x,a,b) or step_fn(x,b,c) should equal 1 when x is exactly b, or setting step_fn(x,a,b) to return 0.5 when x is exactly a or
  • #1
hunt_mat
Homework Helper
1,782
32
I wrote some code that is supposed to give me the step function:
Matlab:
function y=step_fn(x,a,b)
%This is the step function, given a range from a to b, it wil produce a
%function which is 1 from a to b and zero otherwise.
L=length(x);
y=zeros(1,L);
n=find(x<=a,1,'last');
if (b==max(x))
    m=L;
else
    m=find(x>=b,1,'first');
end
if (n==1)
    y(n:m-1)=1;
else
    y(n+1:m)=1;
end

This all looks good, however when I do the following:
Matlab:
x=linspace(0,5,500);
D=1.5*step_fn(x,0,2)+2*step_fn(x,2,4)+0.5*step_fn(x,4,5);

I get the attached plot. I don't quite understand . Does anyone understand where I am going wrong?
I get similar odd results with the smooth approximation using the erf function.
 

Attachments

  • step.jpg
    step.jpg
    13.1 KB · Views: 444
Physics news on Phys.org
  • #2
Why not use Matlab to debug it by looking at the output of each line of code?

This way you can localize your error to perhaps a single line or more and research that line.
 
  • #3
You are checking the boundaries using <= and >=, meaning that both extremities get included. In other words step_fn(x,2,4) returns 1 for x=4, and step_fn(x,4,5) also returns 1 for x=4, so the two get added up. The only reason it doesn't happen at x=2 is that , for some reason, you decided to exclude the last point if the interval starts at the first point in x, so step_fn(x,0,2) returns 0 for x=2.

You need to decide which of step_fn(x,a,b) or step_fn(x,b,c) equals 1 when x is exactly b (or set step_fn(x,a,b) to return 0.5 when x is exactly a or b).
 
  • Like
Likes jedishrfu and hunt_mat
  • #4
BTW, an easy MATLAB step function looks like this:step_fn = @(x,a,b) x>=a & x<=b;
 

1. What is a step function in coding?

A step function in coding is a type of function that returns a specific output for a given input. It is also known as a Heaviside step function or unit step function. It is commonly used in programming to conditionally execute a specific code block based on a certain condition.

2. How do you code up a step function in a programming language?

The exact syntax for coding up a step function may vary depending on the programming language, but the general concept is the same. It involves defining a function that takes in an input, and using conditional statements to determine the output based on the input. It is important to consider all possible inputs and have a default output for any other cases.

3. What are some real-world applications of a step function?

Step functions have various practical applications in fields such as finance, economics, and engineering. For example, in finance, step functions can be used to model stock prices and predict market movements. In economics, they can be used to analyze consumer behavior and make predictions about demand. In engineering, step functions can be used to control systems and determine the response to different inputs.

4. Can a step function have more than two outputs?

Yes, a step function can have more than two outputs. While the simplest form of a step function has only two outputs (0 and 1), it is possible to have a step function with multiple steps, each representing a different output. This type of step function is commonly used in machine learning and artificial intelligence algorithms to classify data into multiple categories.

5. Are there any limitations to using step functions in coding?

One limitation of using step functions in coding is that they can only handle discrete inputs, meaning the input values must be distinct and separate from each other. They also cannot handle continuous or infinite inputs. Additionally, the complexity of a step function can increase significantly with multiple conditions and outputs, making it difficult to maintain and debug.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top