MATLAB Answers

Plotting discrete piecewise function

51 views (last 30 days)
Gaurav Rudravaram
Gaurav Rudravaram on 1 Feb 2021
Edited: VBBV on 1 Feb 2021
Need to plot a discrete piecewise function.But the problem is between 6 and 9 the value should be (2n+1).Can somebody please help me out
x = @(n) 3*((0<=n) & (n<=5)) + 2*n.*((6<=n) & (n<=9)) + 0*((n<0) & (n>9));
n = -12:12;
figure
stem(n, x(n))
grid

Answers (2)

Walter Roberson
Walter Roberson on 1 Feb 2021
x = @(n) 3*((0<=n) & (n<=5)) + (2*n+1).*((6<=n) & (n<=9)) + 0*((n<0) | (n>9));
n = -12:12;
figure
stem(n, x(n))
grid

VBBV
VBBV on 1 Feb 2021
Edited: VBBV on 1 Feb 2021
x = @(n) 3*((0<=n) & (n<=5)) + (2*n).*(n==6 | n==9) + (2*n+1).*(n > 6 & n< 9) + 0*((n<0) & (n>9));
n = -12:12;
figure
stem(n, x(n))
grid

Community Treasure Hunt

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

Start Hunting!