How can I plot a square wave centered around the Y-axis in MATLAB?

K
Kathryn · Mar 16, 2021 · 2.2K views
Question
I want to  plot a square wave centered around the Y-axis.  
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 12, 2026
The following code sample  plots a square wave with an amplitude of 1 between -0.5 to +0.5 and 0 else where.
 
 
t = -0.5:0.01:0.5;

y = square(2*pi*.25*t,100);

plot(t,y)

tl = -1:0.01:-0.5;

yl = square(0*pi*.25*tl,0);

tr = 0.5:0.01:1;

yr = square(0*pi*.25*tl,0);

tg = [tl t tr];

yg = [yl y yr];

plot(tg,yg)
100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!