19 views (last 30 days)

Cris LaPierre
on 2 Nov 2020

No need to create the same values 3 times. Your code can be simplified to the following. You also don't need nearly as many points to create the visualization.

t0 = -pi:0.01:pi;

T0 = length(t0);

for i = 1:T0

if t0(i)< 0

z0(i)=t0(i)+4;

else

z0(i)=exp(1);

end

end

plot(t0, z0, 'r',t0+2*pi, z0, 'b',t0+4*pi,z0, 'g');

Walter Roberson
on 2 Nov 2020

If you use this kind of code structure, then be careful about endpoints. If the period is exactly divisible by the increment, then you end up with a data point at initial + period due to the end point of the first period, and you end up with with a data point at the same location due to the starting point of the second period.

This particular code has a period of 2*pi which is not exactly divisible by 0.01 so the endpoint of the first period is slightly before the start point of the second period so you are saved.

Walter Roberson
on 3 Nov 2020

syms t

Mod = @(A,B) A-floor(A/B)*B

z0 = piecewise(Mod(t, 2*pi)<=pi, exp(1), Mod(t, 2*pi)+4-2*pi)

fourier(z0)

Z0 = double(subs(z0, t, linspace(-pi,5*pi,257))) ;

Z0(end) = [] ;

FZ = fft(Z0) ;

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/633289-plotting-3-cycles-of-a-periodic-signal#comment_1102689

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/633289-plotting-3-cycles-of-a-periodic-signal#comment_1102689

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/633289-plotting-3-cycles-of-a-periodic-signal#comment_1105039

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/633289-plotting-3-cycles-of-a-periodic-signal#comment_1105039

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/633289-plotting-3-cycles-of-a-periodic-signal#comment_1105509

⋮## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/633289-plotting-3-cycles-of-a-periodic-signal#comment_1105509

Sign in to comment.