Need helps with my unit step function
Show older comments
So I wanted to create a unit step function that contain a time vector that specifies the finite range of the signal and a time shift value, which is equivalent to u(t + ts). But every time I tried to run the function, it did not work as I expected. The reason I believe it did not work was because of time vector in a function. I do not know how to make a time vector that can set a finite range by inputing any integer. I would be much thankful if anyone can help me. Here is my code I have problem with.
function [y] = unitstep(t, ts)
for i = -t:t
y = zeros(size(i));
if i >= ts
y = 1;
elseif i < ts
y = 0;
end
end
Answers (1)
Your current code overwrites your output on every iteration. It looks like this is what you want:
function [y] = unitstep(t, ts)
t=-t:t;%only works if t is a scalar
y = zeros(size(t));
for ind = 1:numel(t)
if ind >= ts
y(ind) = 1;
else
y(ind) = 0;
end
end
Which can be simplified to this:
function [y] = unitstep(t, ts)
t=-t:t;%only works if t is a scalar
y=zeros(size(t));
y(y>=ts)=1;
Categories
Find more on Grid Lines, Tick Values, and Labels in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!