Trying to plot function
2 views (last 30 days)
Show older comments
Hey, so ihave been asked to plot this function

I found a way to do this
syms x
y=piecewise(x<-4, 0, -4<=x<3, x+2, x>=3, x-2);
fplot(y)
and i get this graph

Now, when i try another way to do this
i get this graph

this is the code
t = linspace(1,1000,1000); %%time
X1=@(t) (t<=-4).*0+(t>=-4 & t<3).*(t+2)+(t>=3).*(t-2);
subplot(2,2,1)
plot (t,X1(t))
axis([-6 6 -20 20]);
Im trying to understand why this way (the second graph) doesnt work.
Thnx.
4 Comments
Michael Van de Graaff
on 1 Apr 2021
also, i'm confused, can you tell me which point on you plot is incorrect?
are you aware that you are plotting only integer X values and the line is just connecting the dot's
Accepted Answer
Walter Roberson
on 1 Apr 2021
You were mislead by two factors:
- You were not using the same plotting range for both of them
- You were sampling at a low number of points the conditionals but not for piecewise
t = -6:6; %%time
X1=@(t) (t<=-4).*0+(t>=-4 & t<3).*(t+2)+(t>=3).*(t-2);
subplot(2,2,1)
plot(t,X1(t))
axis([-6 6 -20 20]);
title('discrete using conditionals')
subplot(2,2,2)
fplot(X1, [-6 6]);
axis([-6 6 -20 20]);
title('continuous using conditionals')
syms x
y(x) = piecewise(x<-4, 0, -4<=x<3, x+2, x>=3, x-2);
subplot(2,2,3)
plot(t, y(t))
axis([-6 6 -20 20])
title('discrete using piecewise')
subplot(2,2,4)
fplot(y, [-6 6])
axis([-6 6 -20 20])
title('continuous using piecewise')
3 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!