I want to draw this step function by script code.

3 views (last 30 days)
Sol Elec on 27 Aug 2022
Commented: Chunru on 29 Aug 2022

Chunru on 27 Aug 2022
Edited: Chunru on 29 Aug 2022
t = linspace(0,11)';
x = square(t*0.25*2*pi);
min(x)
ans = -1
plot(t,x)
ylim([-1.1 1.1])
% Manually specify the points
t = [0 5 5 7 7 9 9 10];
x = [0 0 1 1 -2 -2 1 1];
figure
plot(t, x);
box on
grid on
ylim([-2.2 1.2])
3 CommentsShow 1 older commentHide 1 older comment
Chunru on 27 Aug 2022
Then you specify time and amplitude as two vectors.
Chunru on 29 Aug 2022
See the update.

Sam Chak on 27 Aug 2022
I'll show you the basic concept of how to construct this 5-sec delayed square wave.
t = linspace(0, 6, 601); % 11 minus 5-sec delayed, need to generate data for nett 6 seconds
f = (2*pi)/4 % angular frequency with a period of 4 seconds
x = sin(f*t); % base function for the signum function to switch
y = 3/2*sign(x) - 1/2; % square wave function
plot(t+5, x, t+5, y) % 5-sec delayed
grid on, xlim([0 12]), ylim([-3 2]), xlabel('t'), ylabel('y(t)')