Create a graph with square function of period different to 2π

19 views (last 30 days)
Hello,
I am trying to create a function like attached.
I created a square wave but apparently the period is fixed and equal to 2π. My approach has been to create three different functions, one for charge, one for idle and one for discharging.
Thank you very much, I attach the code I have so far:
t = linspace(0,23,24);
P1 = 100*square(t,96);
P2 = 0*square(t,4);
P3 = 220*square(t,4);
plot(t,P1)
hold on
plot(t,P2)
plot(t,P3)
xlim([0,24])
xlabel('time [h]')
ylabel('Power [MW]')
grid on

Accepted Answer

Sam Chak
Sam Chak on 6 Sep 2022
Edited: Sam Chak on 6 Sep 2022
Maybe like this?
t = linspace(0, 24, 2401);
duty1 = 100*10/24; % 10 hours in percentage
delay1 = 3; % 3 hours
P1 = (100/2)*square((t - delay1)*2*pi/24, duty1) + 100/2;
P2 = zeros(length(t));
duty3 = 100*4/24; % 4 hours in percentage
delay3 = 18; % 18 hours
P3 = (220/2)*square((t - delay3)*2*pi/24, duty3) + 220/2;
plot(t, P1, 'k-', 'linewidth', 1.5), hold on
plot(t, P3, 'g-', 'linewidth', 1.5), hold on
plot(t, P2, 'b--', 'linewidth', 1.5)
xlim([0, 24])
xticks([0 6 12 18 24])
ylim([-50 250])
xlabel('time [h]')
ylabel('Power [MW]')
grid on
legend('Charging', 'Discharging', 'Idle', 'location', 'northwest')
  1 Comment
Ricard Escriva
Ricard Escriva on 6 Sep 2022
Thank you very much for helping me @Sam Chak! It works wonderfully
As always, the Matlab comunity showing why it is the best in the world :)

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!