- #1

hunt_mat

Homework Helper

- 1,760

- 27

I wrote some code that is supposed to give me the step function:

This all looks good, however when I do the following:

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.

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.