Duplicating data name when using legend in a for loop

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 Commenti

You should never eval() a symbolic expression. Symbolic expressions are in a language which is close to MATLAB but not exactly the same. You should subs() if necessary to give values to any variables, and you should double() what is left if you need a floating point numeric value.
Hey Walter, thanks for that! I have changed my evals to doubles. When is the best case to use eval?
The best case to use eval is... basically, never.

Accedi per commentare.

 Risposta accettata

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 Commenti

Hey thanks for your answer! I'm trying to get it to plot only one of the data sets in each of the runs of the for loop, so that there is no duplicate in the legend, but I'm stuck. Do you have any suggestions how to solve the problems of duplication in the legend?
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);
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.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by