Legend inside a loop

2 ビュー (過去 30 日間)
Tejas Appaji
Tejas Appaji 2020 年 9 月 18 日
コメント済み: Tejas Appaji 2020 年 9 月 18 日
Hi,
I have made a for loop that plots 20 sets of values against time. Now I want to create a different legend name for all the 20 plots. In the code I calculate dampening co-efficient called zeta. I want the legend to say x(t) for zeta = (value). This is the code I have. I went through the forum and i understand that I have to use displayname. I am not sure how it works. Any help is greately appreciated.
% given
k = 2000;
m = 150;
x0 = 0;
v0 = 0.010;
c = (50:50:1000);
wn = sqrt(k/m);
t = (0:0.005:5);
xt = zeros(numel(c),numel(t));
zeta = zeros(1,numel(c));
wd = zeros(1,numel(c));
phi = zeros(1,numel(c));
A = zeros(1,numel(c));
for i = 1:numel(c)
zeta(i) = c(i)/(2*sqrt(m*k));
wd(i) = wn*(sqrt(1-zeta(i)^2));
phi(i) = atan((wd(i)*x0)/(v0+(zeta(i)*wn*x0)));
A(i) = (1/wd(i))*(sqrt((v0+zeta(i)*x0*wn)^2+(x0*wd(i))^2));
for j = 1:numel(t)
xt(i,j) = (A(i)*sin(wd(i)*t(j)+phi(i))*exp(-zeta(i)*wn*t(j)));
end
end
for k = 1:numel(c)
figure
% set(gcf, 'WindowState', 'maximized');
plot (t,xt(k,:))
title ('Assignment 2 Problem 1.61')
xlabel('Time')
ylabel('x(t)')
legend(' problem is here ')
end

採用された回答

Mario Malic
Mario Malic 2020 年 9 月 18 日
編集済み: Mario Malic 2020 年 9 月 18 日
Something like this should work.
for k=1:1:length(c)
legendstr{1,k} = sprintf('zeta = %f', zetavalue); %legendstr changes size
legend(legendstr)
end
  3 件のコメント
Mario Malic
Mario Malic 2020 年 9 月 18 日
In your loop with variable k, you're plotting each curve, and if you add this, it creates a legend cell that contains names of curves as they increase with each loop.
Tejas Appaji
Tejas Appaji 2020 年 9 月 18 日
Thank you so much. That really helps.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by