# How to plot step functions in Matlab

44 visualizaciones (últimos 30 días)
Sangani Prithvi el 31 de Oct. de 2020
Comentada: VBBV el 31 de Oct. de 2020
I have a function involving
y=o for x<o;
y=exp(-x)*cos(x) for 0<x<2pi();
y=2*exp(-x)*cos(x) for x>2pi();
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

VBBV el 31 de Oct. de 2020
Editada: VBBV el 31 de Oct. de 2020
%if true
% code
%end
x = -2*pi:0.1:3*pi;
for i = 1:length(x);
if x(i)<0;
y(i)=0;
elseif x(i)<=2*pi & x(i)>=0 ;
y(i) = exp(-x(i))*cos(x(i)*pi/180);
elseif x(i) > 2*pi ;
y(i) = 100*exp(-x(i))*cos(x(i)*pi/180);
end;
end;
plot(x,y)
axis([-2*pi 3*pi -0.2 1])
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Sangani Prithvi el 31 de Oct. de 2020
y(i) = 100*exp(-x(i))*cos(x(i)*pi/180
can you please explain why did you mutliply with 100 in the above equation?
VBBV el 31 de Oct. de 2020
you can use a smaller number, say 2, but
y(i) = 2*exp(-x(i))*cos(x(i)*pi/180);
gives a very small step height compared to
y(i) = exp(-x(i))*cos(x(i)*pi/180)
So in the graph it is not noticeable clearly
Thats why i used to 100 which amplifies the step height.
Remember in both cases the step nature does not vary. i,e, decreasing exponential function according to your equations

Iniciar sesión para comentar.

### Más respuestas (1)

Vladimir Sovkov el 31 de Oct. de 2020
syms x;
y=piecewise(x<0,0, 0<=x<2*pi,exp(-x).*cos(x), x>=2*pi,2*exp(-x).*cos(x));
t=linspace(-pi,4*pi,1000);
plot(t,subs(y,x,t));
##### 2 comentariosMostrar NingunoOcultar Ninguno
Sangani Prithvi el 31 de Oct. de 2020
thank you
Vladimir Sovkov el 31 de Oct. de 2020
Welcome.
By the way, in the question, you did not specify what the function is equal to at the boundary points x=0 and x=2*pi; in the code I implied the right limit but you can easily alter this convention.

Iniciar sesión para comentar.

### Categorías

Más información sobre Line Plots en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by