plotting step function with variable step length

7 visualizaciones (últimos 30 días)
sasha
sasha el 15 de Dic. de 2014
Comentada: sasha el 15 de Dic. de 2014
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 comentario
Henrik
Henrik el 15 de Dic. de 2014
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)

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 15 de Dic. de 2014
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 comentario
sasha
sasha el 15 de Dic. de 2014
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

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by