필터 지우기
필터 지우기

plotting step function with variable step length

조회 수: 10 (최근 30일)
sasha
sasha 2014년 12월 15일
댓글: sasha 2014년 12월 15일
I want to do a plot of y as a function of time. Where the values of y are either zero or one and they alternate. The time that y is either zero or one is given by t(n) where I solved for t(n) (a vector of different times). Basically I want to have y(t=0 -> t=t(1)) = 0, then y(t=t(1) -> t = t(2)) = 1, then y(t = t(2) -> t(3)) = 0, and so on. I have already solved for a distribution of the t's. I just don't know how to plot this.
  댓글 수: 1
Henrik
Henrik 2014년 12월 15일
Your notation is difficult to understand, so I don't know exactly what you're asking.
From what I understood, you could make a vector of the t-values, another vector with the y-values, and plot them using
plot(t,y)
example:
t=[1 2 4 6 7 9];
y=[1 1 -1 0 -1 1];
plot(t,y)

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

답변 (1개)

Guillaume
Guillaume 2014년 12월 15일
One possible way:
t=[2 7 10 15 20]; %for example
x = [0 reshape(repmat(t(1:end-1), 2, 1), 1, []) t(end)];
y = repmat([1 1 0 0], 1, ceil(numel(x)/4));
y = y(1:numel(x));
plot (x, y)
  댓글 수: 1
sasha
sasha 2014년 12월 15일
I actually messed around with it and it appears that defining
n = 1;
n1 = 1;
while n< nmax
x(n+1) = x(n) + t(n);
n = n+1;
end
while n1<nmax/2
t_odd = 2*n1-1;
t_even = 2*n1;
y(t_odd) = 0;
y(t_even) = 1;
n1 = n1+1
end
because t is just the time step, while now x is the absolute time. And should have the first value be at 0 because we start with a zero by the definition of my plot.
And then just using
stairs(x,y)
Gives the correct plot

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

카테고리

Help CenterFile Exchange에서 Descriptive Statistics and Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by