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

조회 수: 27 (최근 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 CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by