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..

2 views (last 30 days)
ashutosh negi
ashutosh negi on 31 Jul 2016
Answered: VBBV on 28 Nov 2021
% 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')

Answers (2)

Walter Roberson
Walter Roberson on 31 Jul 2016
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 on 28 Nov 2021
% 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

Tags

Community Treasure Hunt

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

Start Hunting!