add legend after a loop

12 ビュー (過去 30 日間)
Yaron Perez
Yaron Perez 2022 年 4 月 8 日
回答済み: KSSV 2022 年 4 月 8 日
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')

回答 (1 件)

KSSV
KSSV 2022 年 4 月 8 日
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

カテゴリ

Help Center および File ExchangeGraphics Performance についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by