periodic function with n cycles

16 visualizaciones (últimos 30 días)
Rashmil Dahanayake
Rashmil Dahanayake el 8 de Dic. de 2013
Comentada: Behrang Hoseini el 22 de Mayo de 2022
Hi, I need to create a periodic function and plot it.
F(x)=sqrt(3) + *Sin(t -2*pi/3) --> 0<t<pi/3
F(x)=Sin(t) --> pi/3 <t<2*pi/3
repeat the signal 0<t<3*pi with the period 2*pi/3 Then plot(t,Fx)
------
At the moment I use the following code
>> t1=0:.01:pi/3;
>> t2=pi/3:.01:2*pi/3;
A=sqrt(3) + sin(t1*2*pi- 2*pi/3);
B=sin(t2);
plot(t1,A,t2,B)
This method is produce the answer a one cycle. However it is quite difficult to repeat the pattern for multiple times.
Can any one n please suggest way of doing this

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 8 de Dic. de 2013
Editada: Andrei Bobrov el 10 de Dic. de 2013
t = 0:pi/100:6*pi;
t1 = rem(t,2*pi/3);
l = t1 < pi/3 ;
F = @(t,l)sqrt(3)*l + sin((2*pi*l + ~l).*t -2*pi/3*l);
out = F(t1,l);
plot(t,out)
ADD
t = 2*pi*(0:.0005:1).';
t1 = rem(t,2*pi/3);
l1 = t1 < pi/3;
l0 = ~l1;
y = zeros(numel(t),2);
y(l1,1) = sqrt(3) + sin(t1(l1) - 2*pi/3);
y(l0,1) = sin(t1(l0));
y(l1,2) = sin(t1(l1) - 2*pi/3);
y(l0,2) = sin(t1(l0)) - sqrt(3);
yy = sin([t,bsxfun(@plus,t,[1, -1]*2*pi/3)]);
plot(t,[y,yy]);
  2 comentarios
Rashmil Dahanayake
Rashmil Dahanayake el 10 de Dic. de 2013
Editada: Rashmil Dahanayake el 10 de Dic. de 2013
Thanks. I modified further so that I can vary the frequency of the generated wave. Fyi. updated Code
f= 2; %frequency in Hz
x=linspace(0,1,1001);
t=x.';
w=2*pi*f;
T=1/f;
t1 = rem(t,T/3);
l1 = t1 < T/6;
l0 = ~l1;
y = zeros(numel(t),2);
y(l1,1) = sqrt(3) + sin(w*t1(l1) - 2*pi/3);
y(l0,1) = sin(w*t1(l0));
y(l1,2) = sin(w*t1(l1) - 2*pi/3);
y(l0,2) = sin(w*t1(l0)) - sqrt(3);
yy = sin([w*t,bsxfun(@plus,w*t,[1, -1]*2*pi/3)]);
plot(t,[y,yy]); grid on;
Behrang Hoseini
Behrang Hoseini el 22 de Mayo de 2022
Hi,
I want to use this method to develop a periodic window to apply to a time function. The thing I could't understand is the second added part:
t = 2*pi*(0:.0005:1).';
t1 = rem(t,2*pi/3);
l1 = t1 < pi/3;
l0 = ~l1;
y = zeros(numel(t),2);
y(l1,1) = sqrt(3) + sin(t1(l1) - 2*pi/3);
y(l0,1) = sin(t1(l0));
y(l1,2) = sin(t1(l1) - 2*pi/3);
y(l0,2) = sin(t1(l0)) - sqrt(3);
yy = sin([t,bsxfun(@plus,t,[1, -1]*2*pi/3)]);
plot(t,[y,yy]);
do we need to add it?

Iniciar sesión para comentar.

Más respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 8 de Dic. de 2013
t1=0:.01:pi/3;
t2=pi/3:.01:2*pi/3;
A=sqrt(3) + sin(t1*2*pi- 2*pi/3);
B=sin(t2);
t=[t1 t2],
y=[A,B]
plot(t,y)
m=5 % Repetition
n=numel(t);
tt=0:0.01:n*m*0.01-0.01
yy=repmat(y,1,m)
plot(tt,yy)
  4 comentarios
Andrei Bobrov
Andrei Bobrov el 10 de Dic. de 2013
Editada: Andrei Bobrov el 10 de Dic. de 2013
Hi Rashmil! See my variant of your problem (after ADD in my answer)
zhenning li
zhenning li el 1 de Nov. de 2020
truely thanks,it helps a lot!

Iniciar sesión para comentar.


sixwwwwww
sixwwwwww el 8 de Dic. de 2013
Editada: sixwwwwww el 8 de Dic. de 2013
you can do it as follow:
count = 1;
for t = 0:pi/3:pi - pi/3
if mod(count, 2) == 1
x = linspace(t, t + pi/3);
y = sqrt(3) + sin(x * 2 * pi - 2 * pi/3);
plot(x, y), hold on
count = count + 1;
else
x = linspace(t, t + pi/3);
y = sin(x);
plot(x, y), hold on
count = count + 1;
end
end
Maybe following link is also helpful for you:
  2 comentarios
Rashmil Dahanayake
Rashmil Dahanayake el 9 de Dic. de 2013
It seems like the variable count does not have any effect on the output.
ie If I want to have 5 cycles of the, count=5? but the output remains unchanged.
sixwwwwww
sixwwwwww el 9 de Dic. de 2013
It was selected to choose between the plots curve should be plotted. It doesn't have effect on output actually. The output is controlled by the range in the for loop:
for t = 0:pi/3:pi - pi/3
changing pi - pi/3 to pi - pi/3 will give more periods of the plot

Iniciar sesión para comentar.

Categorías

Más información sobre Spectral Measurements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by