how to creat periodic function

5 ビュー (過去 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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by