フィルターのクリア

how to plot a periodic function fro negative to positive?

1 回表示 (過去 30 日間)
Daniel Niu
Daniel Niu 2022 年 10 月 20 日
コメント済み: Sam Chak 2022 年 10 月 20 日
Dear friend,
How to plot a periodic function for example 2*x from negative to positive?
I know a method to plot periodic function using mod but, It seems can not plot the negative range.
t = 10; % total simulation time
T = 3; % period
x = 0:0.1:t;
% lets say your original function is y=2*x which repeated every T second, then
y = 2*(mod(x,T));
plot(x,y)
However, because the mod of negative is a little strange, I don't know how to modify the code.
Your help would be highly appreciated
  2 件のコメント
V Sairam Reddy
V Sairam Reddy 2022 年 10 月 20 日
Please check this MATLAB Answer
Daniel Niu
Daniel Niu 2022 年 10 月 20 日
Dear Mr Reddy,
The answer is not what I want. for example. I want my function at -pi:pi periodic. But the method only can plot function in 0:2pi.
I am looking forward to your reply.
Best regards.

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

採用された回答

Torsten
Torsten 2022 年 10 月 20 日
T = pi; % period
x = -10:0.1:10;
% lets say your original function is y=2*x which repeated every T second, then
y = 2*(mod(x-pi,2*pi)-pi);
plot(x,y)
grid on

その他の回答 (1 件)

Sam Chak
Sam Chak 2022 年 10 月 20 日
編集済み: Sam Chak 2022 年 10 月 20 日
Are you looking for this?
Edit: Made changes as per shown in your diagram. Because the amplitude is unknown, then you need to multiply the function by a certain factor to get the desired amplitude.
t = 3*pi; % total simulation time
T = 2*pi; % period (from -π to +π) --> π - (-π) = 2π
x = -t:0.01:t;
y = 2*(mod((x - pi), T)) - 2*T/2; % also need to shift x by pi
plot(x/pi, y), grid on,
xlabel('{$\frac{x}{\pi}$}', 'interpreter', 'latex', 'fontsize', 16)
ylabel('{$y$}', 'interpreter', 'latex', 'fontsize', 16)
  3 件のコメント
Daniel Niu
Daniel Niu 2022 年 10 月 20 日
Dear Mr Chak,
I am looking something like this.
Thank you so much for your answers.
Sam Chak
Sam Chak 2022 年 10 月 20 日
Don't mention it @Daniel Niu. I have edited the code to display the sawtooh wave as per your sketched diagram. If you find the solution is helpful, please consider accepting ✔ and voting 👍 the Answer. Thanks!

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

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by