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

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB implementation of a step function and the issues encountered with its output. Participants are examining the code's logic and its implications for the function's behavior across specified intervals.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents their step function code and describes unexpected results when combining multiple calls to the function.
  • Another participant suggests using MATLAB's debugging tools to isolate the source of the error in the code.
  • A different participant points out that the use of inclusive boundary checks (<= and >=) leads to overlapping intervals, which may cause unintended summation of outputs from the step function.
  • There is a proposal to modify the function's behavior at the boundaries to avoid ambiguity, such as returning a specific value when x equals the boundaries.
  • A simpler alternative for defining a step function in MATLAB is provided, which uses logical operations to determine the output.

Areas of Agreement / Disagreement

Participants express differing views on how to handle boundary conditions in the step function, indicating a lack of consensus on the best approach to resolve the issue.

Contextual Notes

There are unresolved questions regarding the handling of boundary values and the implications of the current implementation on the output of the step function.

hunt_mat
Homework Helper
Messages
1,816
Reaction score
33
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: 531
Physics news on Phys.org
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.
 
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   Reactions: jedishrfu and hunt_mat
BTW, an easy MATLAB step function looks like this:step_fn = @(x,a,b) x>=a & x<=b;
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K