Clear Filters
Clear Filters

how to creat periodic function

1 view (last 30 days)
rahman sajadi
rahman sajadi on 24 Aug 2015
Commented: Star Strider on 24 Aug 2015
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

Answers (2)

James Tursa
James Tursa on 24 Aug 2015
Edited: James Tursa on 24 Aug 2015
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 Comments
rahman sajadi
rahman sajadi on 24 Aug 2015
the anger are inputs function
James Tursa
James Tursa on 24 Aug 2015
Edited: James Tursa on 24 Aug 2015
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);

Sign in to comment.


Star Strider
Star Strider on 24 Aug 2015
Edited: Star Strider on 24 Aug 2015
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 Comments
rahman sajadi
rahman sajadi on 24 Aug 2015
do you now my mean?
Star Strider
Star Strider on 24 Aug 2015
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!