MATLAB: Try to plot the multiple of rectangular ( or triangle) function with sin function.

matlab functionplotting

I tried to plot the multiple of rectangular (or triangle) function and sin function. This is my code for plotting. But I didn't know what is the error. Can you please help me to fix it.
Thanks so much.

Best Answer

Your t is a vector. You have
if abs(t) <= D_tri
In MATLAB, this is equivalent to
if all(abs(t) <= D_tri)
which is probably not the case.
You should use logical indexing:
tri_sin = zeros(size(t));
mask = abs(t) <= D_tri;
tri_sin(mask) = (1 - abs(t(mask))/D_tri) .* sin(w_tri.*t(mask));