add legend after a loop

12 views (last 30 days)
Yaron Perez
Yaron Perez on 8 Apr 2022
Answered: KSSV on 8 Apr 2022
im bulding a program that finds the roots' the last number change from 0 to 20. after that im plot the result' and i want to add legend for each result. im probably getting complex numbers.
clear all;
close all;
Legend=cell(19,1)
for k=0:20
p=[1 2 4 k]
r=roots(p);
figure(1)
hold on;
plot(r,'*')
hold on;
end
axis ([-3.5 0.7, -3 3])
grid on;
plot( 0, [-3:0.01:3],'k.-')
hold off;
xlabel('Re(s)')
ylabel('Im(s)')
title('system poles with diffeent k')

Answers (1)

KSSV
KSSV on 8 Apr 2022
May be something like below:
figure(1)
hold on
for k=0:20
p=[1 2 4 k] ;
r=roots(p);
plot(r,'*','DisplayName',num2str(k))
end
axis ([-3.5 0.7, -3 3])
grid on;
plot( 0, [-3:0.01:3],'k.-','DisplayName','out of loop')
hold off;
xlabel('Re(s)')
ylabel('Im(s)')
title('system poles with diffeent k')
legend show

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!