Problem in plotting periodic waves
Show older comments
Hello, I want to plot simple one period ( square wave whose duty cycle is 25% )that's it with the help of simple for and if statements but its so annoyingly hard in MATLAB. please help me, i shall be grateful. This is my code. WHen i run it i cannot get anything. Yes i am trying and i am relatively new to MATLAB so this matrices thing is so confusing.Thank you.
T=1;
Tp = 0.25;
one = [1 1 1 1 1];
zero = [0 0 0 0 0 ];
x = 0:0.1:0.25;
t=0;
for t= 0:0.25:1
if t < 0.50
A = one;
else
A = zero;
end
end plot (t,A)
Answers (1)
Star Strider
on 10 Nov 2017
Otherwise, try this:
p = 20; % Period
d = 0.25; % Duty Cycle
sqwv = @(p,d) [ones(1,ceil(p*d)) zeros(1,p-ceil(p*d))] .* ((d > 0) & (d < 1)); % Square Wave Anonymous Function
pulsetrain = @(n) repmat(sqwv(p,d), 1, n); % Create Pulse Train Of ‘n’ Periods
pt = pulsetrain(5); % Pulse Train
t = 1:length(pt); % Time Vector
figure(1)
plot(t, pt)
set(gca, 'YLim',[-0.1 1.1])
grid
2 Comments
Hani Syed
on 15 Nov 2017
Star Strider
on 15 Nov 2017
I don’t see that requirement anywhere in your original Question.
It’s straightforward to adapt my code to do that:
p = 20; % Period
d = 0.25; % Duty Cycle
sqwv = @(p,d) [ones(1,ceil(p*d)) zeros(1,p-ceil(p*d))] .* ((d > 0) & (d < 1)); % Square Wave Anonymous Function
pulsetrain = @(n) repmat(sqwv(p,d), 1, n); % Create Pulse Train Of ‘n’ Periods
pt = pulsetrain(5); % Pulse Train
t = linspace(0, 5, length(pt)); % Time Vector
figure(1)
plot(t, pt)
set(gca, 'YLim',[-0.1 1.1])
grid
Categories
Find more on Waveform Generation 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!