# Creating a unit step function in Matlab

• MATLAB
• skybox
In summary: There's little chance he'll be answering your question.You could try emailing him. That may still be current.
skybox
Hi Guys,

I am trying to create a basic unit step function in Matlab that needs to be in the range of"
-5 <= x <= 5

I need this to be done via a function and not piece together using different intervals and it needs to show the whole -5 to 5 interval. I am just beginning in Matlab and am stuck on where to start after creating the interval, which I made by using the following command:

%create the interval
x = -5 : 1 : 5

If anyone can give me guidance on how to start after this, it would be greatly appreciated. I tried using an if function with the following logic:

if x > 0, plot the graph of the unit step of magntiude 1
if x < 0, just plot 0's from the interval -5 to 0

This does not seem to work. Any help would be greatly appreciated!

Figured it out.

Used the following code:

n = -5 : 1 : -5

y = (n >= 0)

stem(n,y)

I think recent versions of Matlab come with the function under the name heaviside (i.e. Heaviside step function.)

Just in case anyone reads the last post, it is incorrect with regards to certain versions - the function heaviside is not defined in MATLAB 2008b, however i can't comment on any later versions than this

Hello,
Please allow me to share a solution.

Code:
function [x]=unitstep(x)
%This is a unit step "function". The vector keeping track of time is the
%input. If time is negative then a zero is returned. If time is zero than
%0.5 is returned. If time is positive then 1 is returned.

if nargin==0 %demo the use of the function if no input is given
x=-10:10;
end

x=x./abs(x); %this performs the same operation as the MATLAB "sign"
x(isnan(x))=0;

x=0.5*(x+1);

With regards to the other posts, to echo Tokipin, "heaviside" is indeed a defined function in Matlab, at least as recent as R2009a. My code performs exactly as the "heaviside", so I am being redundant if you have a recent version of Matlab. My last note: the Matlab "heaviside" function uses the same solution skybox suggests in its operation. Go skybox!

hi
how to define step function for initial condition in fully implicit finite diference methode?

x=[-5:0 0:5];
y=[zeros(1,6) ones(1,6)];
plot(x,y)

t=input('please enter the shift you want in unit step function\n');
x=[t-5:t t:t+5];
y=[zeros(1,6) ones(1,6)];
plot(x,y)
axis([-5 5 -1 2])
grid on
:) :) :) :) :) :) :) :) :) :)

skybox said:
Figured it out.

Used the following code:

n = -5 : 1 : -5

y = (n >= 0)

stem(n,y)
sir can u tell me what you have done??
what is y=(n>=0)

Skybox's post is more than three years old, and he hasn't posted on PF at all in almost that long.

## 1. How do I create a unit step function in Matlab?

To create a unit step function in Matlab, you can use the "heaviside" function. This function takes in a single argument, which is the point at which the step occurs. For example, if you want the step to occur at x=2, you would use "heaviside(x-2)".

## 2. Can I plot a unit step function in Matlab?

Yes, you can plot a unit step function in Matlab by using the "plot" function. However, since a unit step function is a discontinuous function, it will appear as a vertical line at the point of the step. You can use the "hold on" and "hold off" commands to add multiple functions to the same plot.

## 3. How do I change the location of the step in a unit step function?

You can change the location of the step in a unit step function by changing the argument of the "heaviside" function. For example, if you want the step to occur at x=5, you would use "heaviside(x-5)".

## 4. Can I use a variable as the argument for a unit step function in Matlab?

Yes, you can use a variable as the argument for a unit step function in Matlab. This variable can be defined beforehand or within the function itself. You can also use mathematical expressions as the argument, such as "heaviside(2*x)".

## 5. Can I create a unit step function with multiple steps in Matlab?

Yes, you can create a unit step function with multiple steps in Matlab by using the "piecewise" function. This function takes in multiple arguments, each representing a different step. For example, "piecewise(x<0, 0, 0<=x<2, 1, 2<=x, 2)" would create a unit step function with steps at x=0 and x=2.

• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K