Plot function over two intervals

9 views (last 30 days)
Anand Bhavsar
Anand Bhavsar on 18 Apr 2019
Answered: KSSV on 18 Apr 2019
Hi, I'm trying to plot an interval funciton, where f(x) = 1 from -1 <= x <= 0 and f(x) = -1 from 0 < x < 2, and f(x) = 0 otherwise. I'm not sure how to setup the intervals to do this.

Accepted Answer

KSSV
KSSV on 18 Apr 2019
m = 500 ;
x = linspace(-1,3,m) ;
f = zeros(size(x)) ;
f(x>=-1 & x <=0) = 1 ;
f(x>=0 & x <2) = -1 ;
plot(x,f)

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!