How to create a periodic function?

The function at [0,2] is y=x for [0,1] and y=2-x for [1,2], I want the above function repeated at [2,10], so I need a periodic funtion in the whole [0,10], who can help me code it ,thank you.

 採用された回答

James Tursa
James Tursa 2020 年 11 月 25 日

0 投票

Not sure what you mean by "repeated at [2,10]". Maybe this:
y = mod(x,2);
ix = y > 1;
y(ix) = 2 - y(ix);

9 件のコメント

huazai2020
huazai2020 2020 年 11 月 25 日
Just like this
James Tursa
James Tursa 2020 年 11 月 25 日
Plot what I gave you ...
huazai2020
huazai2020 2020 年 11 月 25 日
Thank you, you give me the right code, but I cannot understand the code "y = mod(x,2);", could you please discribe it for me?
In addition, I want to use this one, could you help me revise it
James Tursa
James Tursa 2020 年 11 月 25 日
編集済み: James Tursa 2020 年 11 月 25 日
Since you are making a periodic function, the mod(x,2) simply puts all of the input values into the range of one cycle. From there it is just a matter of forming the proper outputs of one cycle. The y=x part for the [0,1] input didn't need any adjustment so no extra code for that. Only the [1,2] part needed the extra code.
For this new function, you simply need to scale the x input properly and then use the same algorithm. E.g.,
y = mod(x/3,2);
ix = y > 1;
y(ix) = 2 - y(ix);
huazai2020
huazai2020 2020 年 11 月 25 日
Thank you are right,but where can find the define of “mod”.
In addition, how can I revise the amplitude to 0.2, how can I revise it?
James Tursa
James Tursa 2020 年 11 月 25 日
編集済み: James Tursa 2020 年 11 月 25 日
To revise the amplitude, simply add one more line to the algorithm:
y = 0.2 * y;
The mod( ) function description can be found here:
huazai2020
huazai2020 2020 年 11 月 25 日
As I can see in the help document, mod has the function similar to division ,but not as you said. What is the reason?
mod
Modulus after division
Syntax
M = mod(X,Y)
Description
M = mod(X,Y) if Y ~= 0, returns X - n.*Y where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars.
The following are true by convention:
  • mod(X,0) is X
  • mod(X,X) is 0
  • mod(X,Y) for X~=Y and Y~=0 has the same sign as Y.
James Tursa
James Tursa 2020 年 11 月 25 日
Generic code could be:
% Periodic triangle wave
amplitude = whatever;
period = whatever;
y = mod(x,period);
ix = y > period/2;
y(ix) = period - y(ix);
y = (amplitude * 2 / period) * y;
huazai2020
huazai2020 2020 年 11 月 25 日
Yes, you are so great,thank you so much.

サインインしてコメントする。

その他の回答 (2 件)

David Hill
David Hill 2020 年 11 月 25 日

0 投票

y=zeros(size(x));
for k=1:5
y(x>=(k-1)*2&x<(k-1)*2+1)=x(x>=(k-1)*2&x<(k-1)*2+1);
y(x>=2*(k-1)+1&x<2*k)=2-x(x>=2*(k-1)+1&x<2*k);
end

4 件のコメント

huazai2020
huazai2020 2020 年 11 月 25 日
Hi, thank you for your answer,but I find it is not the results that I want as you see in the image,
clc;
clear all;
x=linspace(0,10,100);
y=zeros(size(x));
for k=1:5
y(x>=(k-1)*2&x<(k-1)*2+1)=x(x>=(k-1)*2&x<(k-1)*2+1);
y(x>=2*(k-1)+1&x<2*k)=2-x(x>=2*(k-1)+1&x<2*k);
end
plot(x,y)
David Hill
David Hill 2020 年 11 月 25 日
Are your functions changing? Recommend showing us the input and output you want.
Image Analyst
Image Analyst 2020 年 11 月 25 日
Then just use the code that you used to create the figure. It's what you want isn't it?
huazai2020
huazai2020 2020 年 11 月 25 日
Please see the below image I upload,it is what I want.

サインインしてコメントする。

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 25 日

0 投票

x = [0:3:36];
y = [0 1 0 1 0 1 0 1 0 1 0 1 0];
sig = pwfun(x,y);
and create the waveform only with the intersection points.

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2020 年 11 月 25 日

回答済み:

2020 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by