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

19 ビュー (過去 30 日間)
Ricard Escriva
Ricard Escriva 2022 年 9 月 6 日
コメント済み: Ricard Escriva 2022 年 9 月 6 日
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

採用された回答

Sam Chak
Sam Chak 2022 年 9 月 6 日
編集済み: Sam Chak 2022 年 9 月 6 日
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 件のコメント
Ricard Escriva
Ricard Escriva 2022 年 9 月 6 日
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 :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by