Plotting a Step Function

조회 수: 7 (최근 30일)
Adam Makin
Adam Makin 2018년 1월 8일
편집: Jan 2018년 1월 8일
The following function f(t,T,x) returns the following. 1 if qT ≤ t < (q + x)T 0 if (q + x)T ≤ t < (q + 1)T
I know this is a step function and I think it involves using heaviside, however I have no idea really where to start in order to plot the function. q, x and T are to be defined by me. THis is the code I have so far:
function v=pwm(t,T,d)
d = 0.5;
T= 0.01;
k = 1;
t=linspace(0,0.2,100);
v(k*T<=t<k+d) = 1;
v((k + d)*T <= t < (k + 1)*T) = 0;
plot(t,v)
q I changed for k (q=k) and x = d.
  댓글 수: 3
Adam Makin
Adam Makin 2018년 1월 8일
Sorry you are right I should be much more specific, I wanted to plot the function.
Jan
Jan 2018년 1월 8일
Sorry, I didn't want to let you be sorry. Questions for clarifications are usual in the forum, because it is the nature of question, that some details are not clear to the asking person. :-)

댓글을 달려면 로그인하십시오.

답변 (2개)

Star Strider
Star Strider 2018년 1월 8일
Try this:
f = @(t,T,x,q) (1.*((q.*T <= t ) & (t < (q+x).*T))) + (0.*(((q+x).*T <= t) & (t < (q+1).*T)));
q = 1;
x = 0.5;
T = 0.01;
t = linspace(0,0.2,100);
figure(1)
plot(t, f(t,T,x,q), '-r')
axis([xlim 0 1.1])
grid

Jan
Jan 2018년 1월 8일
편집: Jan 2018년 1월 8일
What about this:
t = [q*T, (q + x) * T, (q + x) * T, (q + 1) * T]; % [EDITED, Typo fixed]
y = [1, 1, 0, 0];
plot(t, y)
This is a line through 4 points. Is this sufficient to plot the function, if you define q, T and x accordingly? Note that 0 < x < 1 seems to be required.
If you want something else, please explain the difference to this simple approach.
  댓글 수: 2
Adam Makin
Adam Makin 2018년 1월 8일
sorry trying to run this I receive the error:
Error using plot Vectors must be the same length.
Jan
Jan 2018년 1월 8일
편집: Jan 2018년 1월 8일
@Adam: Sorry, a typo.
t = [q*T, (q + x) * T, (q + x) * T, (q + 1) * T];
^ comma instead of colon
Is the idea of my suggestion clear?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by