Duplicating data name when using legend in a for loop

조회 수: 8 (최근 30일)
Avishek  Mondal
Avishek Mondal 2017년 7월 8일
댓글: Walter Roberson 2017년 7월 9일
I've got the following code- it is very rough, because I was trying to compare the effect of different Qs.
close all;
clear all;
syms omega L C M Q theta f_0;
f_0 = 1e6;
Q = [10 100 1000];
omega_0 = 2*pi*f_0;
M = -0.1;
L = 2;
C = 1/(L*omega_0^2);
kap = 2*M/L;
% R=omega_0*L./Q;
% theta = beta - 1*alpha;
figure;
lineCol = ['r', 'b', 'g'];
om = linspace(0.01,1.5,1000)*omega_0;
l = strings(size(Q));
hold on;
for i = 1:3
eqn1 = 1 - (omega_0/omega)^2-(1i*omega_0/(omega*Q(i)))+kap*cos(theta) == 0;
w = solve(eqn1, theta);
solTheta = eval(subs(w,omega,om));
% s = strcat('Q = ', num2str(Q(i)));
s = ['Q= ' num2str(Q(i))];
plot(real(solTheta)/pi,om/omega_0, lineCol(i),'DisplayName',s);
end
legend('show');
xlim([0 1]);
ylabel('\omega/\omega_0');
xlabel('\betaa/\pi');
t =['Real part, \kappa = ', num2str(kap)];
title(t);
figure;
for i = 1:3
eqn1 = 1 - (omega_0/omega)^2-(1i*omega_0/(omega*Q(i)))+kap*cos(theta) == 0;
w = solve(eqn1, theta);
solTheta = eval(subs(w,omega,om));
s = strcat('Q = ', num2str(Q(i)));
plot(imag(solTheta)/pi,om/omega_0, lineCol(i),'DisplayName',s);
hold on;
end
legend('show');
xlim([0 0.5]);
ylabel('\omega/\omega_0');
xlabel('\alphaa/\pi');
t =['Imaginary part, \kappa = ', num2str(kap)];
title(t);
The legend is duplicating the datanames, and I do not know how to solve it. I've attached the plot I'm getting. When I use simple data, like in the following standalone code, I get the legend to perform just fine!
hold on
for p = 1:5
name = ['Line Number ' num2str(p)];
plot(1:10,(1:10)+p, 'DisplayName', name)
end
legend show
Can someone tell me what is wrong?
  댓글 수: 3
Avishek  Mondal
Avishek Mondal 2017년 7월 9일
Hey Walter, thanks for that! I have changed my evals to doubles. When is the best case to use eval?
Walter Roberson
Walter Roberson 2017년 7월 9일
The best case to use eval is... basically, never.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 8일
The result of w = solve(eqn1, theta) is a pair of solutions. When you eval(subs(w,omega,om)) you get a result which is 2 rows by 1000 columns. Then when you
plot(imag(solTheta)/pi,om/omega_0, lineCol(i),'DisplayName',s);
you are plotting with two rows of x and a single row of y. That is going to give you two output lines.
The two solutions, w, are negatives of each other.
When you plot, one of the two of them is not visible because you used an xlim() starting from 0. But the line is still there so it is still present in the legend.
  댓글 수: 3
Avishek  Mondal
Avishek Mondal 2017년 7월 9일
No worries, I believe I have managed to solve it. Here is basically what I did-
figure;
for i = 1:3
eqn1 = 1 - (omega_0/omega)^2-(1i*omega_0/(omega*Q(i)))+kap*cos(theta) == 0;
w = solve(eqn1, theta);
solTheta = eval(subs(w,omega,om));
s = strcat('Q = ', num2str(Q(i)));
l{i} = strcat('Q= ', num2str(Q(i)));
p{i} = plot(imag(solTheta)/pi,om/omega_0, lineCol(i));
hold on;
end
p_l = [p{1}(1) p{2}(1) p{3}(1)];
leg = legend(p_l, l);
xlim([0 0.5]);
ylabel('\omega/\omega_0');
xlabel('\alphaa/\pi');
t =['Imaginary part, \kappa = ', num2str(kap)];
title(t);
Walter Roberson
Walter Roberson 2017년 7월 9일
Instead of
solTheta = eval(subs(w,omega,om));
You can use
solTheta = abs( double(subs(w(1),omega,om)) );
The abs() is to get around the question of whether it happens to be w(1) or w(2) which is the positive branch.
Anyhow with this you would not need to record the handles and so on.
Recording the handles and selective legend() is a good tool, very useful sometimes... it just happens you do not need it this time.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by