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

26 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Sam Chak
Sam Chak il 6 Set 2022
Modificato: Sam Chak il 6 Set 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 Commento
Ricard Escriva
Ricard Escriva il 6 Set 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 :)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by