plotting piecewise periodic function

4 ビュー (過去 30 日間)
omar amr
omar amr 2021 年 12 月 31 日
回答済み: Image Analyst 2021 年 12 月 31 日
How would I plot this function on the range 0<=t<=20?
x= 2(1-t), 0<=t<=1
x= t-1, 1<t<=3

回答 (2 件)

Jonas
Jonas 2021 年 12 月 31 日
you could generate the values for one cycle and then repeat the values using repmat()

Image Analyst
Image Analyst 2021 年 12 月 31 日
Your question did not specify what the value of x is for t > 3 so I'll assume it's zero.
Try this:
% How would I plot this function on the range 0<=t<=20?
% x= 2(1-t), 0<=t<=1
% x= t-1, 1<t<=3
numElements = 512; % Whatever resolution you want.
% Get t axis.
t = linspace(0, 20, numElements);
% Initialize x to all zeros.
x = zeros(1, numElements);
% Get the first range.
indexRange1 = t >= 0 & t <= 1;
x(indexRange1) = 2 * (1 - t(indexRange1));
% Get the second range.
indexRange2 = t > 1 & t <= 3;
x(indexRange2) = t(indexRange2) - 1;
% Plot x
plot(t, x, 'b-', 'LineWidth', 2);
grid on;
fontSize = 18;
title('x vs. t', 'FontSize', fontSize);
xlabel('t', 'FontSize', fontSize);
ylabel('x', 'FontSize', fontSize);

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by