when i am plotting this conditional for loop program no plot is showing only one dot is showing. Can anyone help me about how to display plot? i am using Matlab 2013..

조회 수: 1(최근 30일)
ashutosh negi
ashutosh negi 2016년 7월 31일
답변: VBBV 2021년 11월 28일
% conditional
x= -5:0.005:15;
for k=1:length(x)
if x(k)<0
y(k)=sqrt(x(k)^2+1);
elseif x(k)<=10
y(k)=3*x(k)+1;
elseif x(k)>=10
y(k)=9*sin(5*x(k)-50)+31;
end
end
plot(x(k),y(k),'*--r','linewidth',2,'markeredgecolor','k'),xlabel('x'),ylabel('y')

답변(2개)

Walter Roberson
Walter Roberson 2016년 7월 31일
plot(x,y,'*--r','linewidth',2,'markeredgecolor','k')
You were asking to plot only the very last point, x(k) and y(k)

VBBV
VBBV 2021년 11월 28일
% conditional
x= -5:0.005:15;
for k=1:length(x)
if x(k)<0
y(k)=sqrt(x(k)^2+1);
elseif x(k)<=10
y(k)=3*x(k)+1;
elseif x(k)>=10
y(k)=9*sin(5*x(k)-50)+31;
end
plot(x(1:k),y(1:k),'*--r','linewidth',1,'markeredgecolor','b'); % you can place the plot command inside loop to view
end
you are using the plot command outside the loop,
Instead you can place it inside the loop as shown and plot the graph

태그

Community Treasure Hunt

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

Start Hunting!

Translated by