how to creat periodic function

4 次查看(过去 30 天)
rahman sajadi
rahman sajadi 2015-8-24
y=f(x)
y=0 for 0<x<pi/8
y=1 for pi/8<x<pi/4
y=0 for pi/4<x<pi/3
y=1 for pi/3<x<pi/2
and also this function is even ratio to pi/2 and odd ratio to pi and this function is periodic with 2*pi Periodicity

回答(2 个)

James Tursa
James Tursa 2015-8-24
编辑:James Tursa 2015-8-24
Mod the input x with 2*pi and then do your testing above to generate the output, making sure to cover all cases for x between 0 and 2*pi. If you know x isn't going to be too large you could use x = mod(x,2*pi). But if you want to be robust you can use the following function to do the mod(,2*pi) operation (forces the result to match what MATLAB does in the background for its trig functions):
function y = mod2pi(x) % Result is in range 0 to 2*pi
y = atan2(sin(x),cos(x));
g = y < 0;
y(g) = y(g) + 2*pi;
end
  3 个评论
rahman sajadi
rahman sajadi 2015-8-24
the anger are inputs function
James Tursa
James Tursa 2015-8-24
编辑:James Tursa 2015-8-24
Does this work for you? (CAUTION, untested)
x = mod2pi(x);
g = x > pi;
x(g) = 2*pi - x(g);
h = x > pi/2;
x(h) = pi - x(h);
y = double((x > pi/8 & x < pi/4) | (x > pi/3));
y(g) = -y(g);

请先登录,再进行评论。


Star Strider
Star Strider 2015-8-24
编辑:Star Strider 2015-8-24
I don’t know what you mean by: ‘this function is even ratio to pi/2 and odd ratio to pi and this function is periodic with 2*pi Periodicity.’
Otherwise, see if this does what you want:
x = linspace(0, 5*pi, 200);
y = [((mod(x,pi)>pi/8) & (mod(x,pi)<pi/4)) + ((mod(x,pi)>pi/3) & (mod(x,pi)<pi/2))].*(-1).^fix(x/pi);
figure(1)
plot(x, y)
grid
EDIT — Minor alteration in ‘y’ to reflect to ‘even-odd’ clarification.
EDIT #2 — Added plot figure.
  3 个评论
Star Strider
Star Strider 2015-8-24
The function in my edited Answer (1) creates the function in your Question, (2) has the appropriate ‘even-odd’ behaviour, and (3) will work for all ‘x’ greater than or equal to 0.
I added the plot figure to my original Answer.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by