discontinuity missing.

Hello
This might seem a really easy question for some of you but I can't fix it.
I need to create a function M-file for the function
f(x) = 1-x if 0<=x<=1 [1] and
f(x) = -1-x if -1<=x<0 [2]
I've tried to do this with
if (x >= 0) & (x <= 1) % x is [1]
y = +1 -x;
else
y = -1 -x; % x is [2] or outside of the domain (y doesn't matter so it can be -1-x
end
However when I try to plot this function it remains just one straight line y=-1-x It is probably something really obvious that I'm missing here but I'm not really experienced with matlab.

1 件のコメント

Sean de Wolski
Sean de Wolski 2011 年 4 月 18 日
Well written question; you get a vote!

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

 採用された回答

Paulo Silva
Paulo Silva 2011 年 4 月 18 日

1 投票

Connected lines
x=-1:0.01:1;
y(x>=-1 & x<0)=-1-x(x>=-1 & x<0);
y(x>=0 & x<=1)=1-x(x>=0 & x<=1);
plot(x,y)
Alternative, not connected lines
x1=-1:0.01:0-0.01;
x2=0:0.01:1;
y1(x1>=-1 & x1<0)=-1-x1(x1>=-1 & x1<0);
y2(x2>=0 & x2<=1)=1-x2(x2>=0 & x2<=1);
plot(x1,y1)
hold on
plot(x2,y2,'r')

1 件のコメント

Sean de Wolski
Sean de Wolski 2011 年 4 月 18 日
Good example.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by