Plot piecewise function on an interval

1 回表示 (過去 30 日間)
Vinci
Vinci 2017 年 5 月 15 日
コメント済み: Torsten 2017 年 5 月 16 日
I'm trying to plot the above periodic function on the interval -10 < t < 10
I can plot it once using:
pw = evalin(symengine,'piecewise([t > -2 and t <= 0, -t^2],[t >=0 and t < 2, t^2])');
fplot(pw)
How would I plot this function on the interval?

採用された回答

Torsten
Torsten 2017 年 5 月 15 日
function y=f(t)
y = zeros(size(t)); % Preallocating enough memory for y
tmod=mod(t,4)-2;
region1 = (tmod<0) & (tmod>=-2); % First interval
y(region1) = -tmod(region1).^2;
region2 = (tmod>=0) & (tmod<=2); % Second interval
y(region2) = tmod(region2).^2;
Now you can do this:
t = -10:0.1:10;
y = f(t);
plot(t, y)
Best wishes
Torsten.
  1 件のコメント
Torsten
Torsten 2017 年 5 月 16 日
I think it should read
tmod = mod(t+2,4)-2;
instead of
tmod = mod(t,4)-2;
Best wishes
Torsten.

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

その他の回答 (0 件)

カテゴリ

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

タグ


Translated by