필터 지우기
필터 지우기

how to creat periodic function

조회 수: 1 (최근 30일)
rahman sajadi
rahman sajadi 2015년 8월 24일
댓글: Star Strider 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
rahman sajadi
rahman sajadi 2015년 8월 24일
do you now my mean?
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에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by