Creating a unit step function in Matlab

Click For Summary

Discussion Overview

The discussion revolves around creating a unit step function in Matlab, specifically within the range of -5 to 5. Participants share various approaches, code snippets, and insights related to implementing this function, addressing both beginner-level queries and more advanced considerations.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant seeks guidance on creating a unit step function after defining an interval in Matlab.
  • Another participant shares a solution using a specific code snippet to create the unit step function.
  • A participant mentions that recent versions of Matlab include a built-in function called "heaviside" for the step function.
  • Another participant corrects the previous claim, noting that the "heaviside" function may not be available in older versions of Matlab, such as 2008b.
  • A different participant provides a custom m-file solution for the unit step function, explaining its behavior based on input values.
  • One participant asks about defining a step function for initial conditions in a fully implicit finite difference method.
  • A participant shares another code snippet for plotting the unit step function, using a different approach to define the x and y values.
  • Another participant mentions a shift in the unit step function and provides code to implement this shift.
  • One participant expresses confusion about a previously shared code snippet and seeks clarification on its functionality.
  • A later post humorously notes that a previous contributor has not been active for a long time.

Areas of Agreement / Disagreement

Participants present multiple approaches to creating a unit step function, with some advocating for the use of the "heaviside" function while others highlight its unavailability in older Matlab versions. The discussion remains unresolved regarding the best method to implement the function, as various solutions are proposed without consensus.

Contextual Notes

Some solutions depend on the version of Matlab being used, and there are unresolved questions regarding the applicability of certain functions in different contexts.

skybox
Messages
37
Reaction score
0
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!
 
Physics news on Phys.org
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.

Create your own m-file!
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)
 
  • #10
Necropost alert!

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

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K