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

Michael Van de Graaff
Michael Van de Graaff 2021년 4월 1일
well for one thing, in the second instance you are defining you x-coordinates as going from 1 to 1000, so no, you wont have anything plotted below x=1
Naor t
Naor t 2021년 4월 1일
I need 1000 points in the graph between -6<t<6
Michael Van de Graaff
Michael Van de Graaff 2021년 4월 1일
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
Oh, you need
t = linspace(-6,6,1000);

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2021년 4월 1일

1 개 추천

You were mislead by two factors:
  1. You were not using the same plotting range for both of them
  2. 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

Ok i understand thank you.
Nut now i have another problem becuase i changed the range.
For some reason i get this problem: Array indices must be positive integers or logical values.
at the plot line.
Y1=X1(t).*(t-1);
subplot(2,2,3)
plot(t,Y1(t))
axis([-6 6 -20 20]);
grid on
title('Subplot 1: Y1')
plot(t,Y1)
Naor t
Naor t 2021년 4월 1일
thanks you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2021년 4월 1일

댓글:

2021년 4월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by