MATLAB: I am trying to make a half wave rectified sine wave plot. what am i doing wrong

plot

T=2;
t = 0:pi/100:2*pi;
w=2*pi/T;
if(sin(w*t)<0)
y=0;
else
y=sin(w*t);
end
plot(t, y);

Best Answer

't' is a vector and using it with if condition will not work correctly in your case. You can use logical indexing
T=2;
t = 0:pi/100:2*pi;
w=2*pi/T;
y = sin(w*t);
y(y<0) = 0
plot(t, y);
Related Question