Warning "Ignoring extra legend entries"

115 visualizaciones (últimos 30 días)
Khang Nguyen
Khang Nguyen el 23 de Mayo de 2019
Comentada: Shreen El-Sapa el 2 de Abr. de 2021
Hi everyone,
so this is my code, sorry if it distracts you, please let me know.Just put the code so that everyone knows excactly what is my question. Please just scroll down to read my questions, and use the code for solving it.
%% variable declaration
colours = 'rbk';
%% Solve ODEs using Euler's method
figure(4) %figure 4
%Input
y0 = 1;
z0 = 1;
u = 1;
tspan = [0 30];
%two first order ODE
dydt = @(t,z) z;
dzdt = @(y,z) u*(1-y^2)*z - y;
% solving ODE for each h value
%Creating vector h
h = [0.25,0.125,0.0625];
%forming a matrice of cells since each variables has different size
t_e=cell(1,length(h));
y_e=cell(1,length(h));
z_e=cell(1,length(h));
%Solving ODE for each h value
for i = 1:length(h)
[t_e{i},y_e{i},z_e{i}] = euler2(dydt,dzdt,tspan,y0,z0,h(i));
end
%plotting
for i = 1:length(h)
subplot(2,1,1)
hold on
plot(t_e{1,i},y_e{i},colours(i));
xlabel('t')
ylabel('y')
title('y against t')
legend('step size = 0.25','step size = 0.125','step size = 0.0625','Location','nw')
subplot(2,1,2)
plot(t_e{1,i},dydt(t_e{1,i},z_e{1,i}),colours(i));
hold on
xlabel('t')
ylabel('dydt')
title('dydt against x')
legend('step size = 0.25','step size = 0.125','step size = 0.0625','Location','nw')
end
%print the method that increase the accuracy
fprintf('The accuracy of the solutions can be improved by \nreducing the step size of h, as it can be seen in the graph')
%function file
if ~(tspan(2)>tspan(1))
error('upper limit must be greater than lower')
end
% Create all independent values, t
%use tspan to create a vector with spacing = h;
t = [tspan(1):h:tspan(2)]';
n = length(t);
if t(end) < tspan(2)
t(n+1) = tspan(2);
n = n+1;
end
% add extra t-value if needed
%check if final t-value can be achieved using the current h
%if not, add a new t-value to the end of t
% Implement Euler's method
%use a for loop
%pre allocate the solution vector
%y = zeros(1,n)
%z = zeros(1,n)
%y(1) = y0
%z(1) = z0
y = y0*ones(n,1);
z = z0*ones(n,1); % 1 line
for i = 1:n-1 %stop at n-1 because dont need to calculate the deriative for final interation
%singeline for euler's method
z(i+1) = z(i) + h*dzdt(y(i),z(i));
y(i+1) = y(i) + h*z(i);
end
%%%% Question
When i run this code, it gives me the error
61281388_425247231389910_3749113343936299008_n.png
So I do not know why it gives me that .Can someone explain, and any methods to solve it ?
Thanks in advance,
Khang

Respuesta aceptada

Alex Mcaulley
Alex Mcaulley el 23 de Mayo de 2019
You are calling legend in each iteration of the loop. In the first 2 iterations your plots have less than 3 lines and you are calling legend with three labels, then the warning is generated.
To supress this warning just call legend out of the loop.
  3 comentarios
Alex McCracken
Alex McCracken el 23 de Oct. de 2020
I did a similar thing, I called legend() before I had input all of my variables, so just put legend() at the end of the figure's code helps
Shreyas Taware
Shreyas Taware el 11 de Mzo. de 2021
It can be avoided if you type 'hold on' below the legend in each loop

Iniciar sesión para comentar.

Más respuestas (3)

neelu pareek
neelu pareek el 9 de Ag. de 2020
thanks a lot

Pierre MORETTO
Pierre MORETTO el 5 de Dic. de 2020
What to do if we want a specific legend per figure ?

Cameron J Reimer
Cameron J Reimer el 1 de Mzo. de 2021
thanks
  1 comentario
Shreen El-Sapa
Shreen El-Sapa el 2 de Abr. de 2021
Can you help me?
%========================================================
plot(kappa,drag1,'-K',kappa,drag2,'--K');
%plot(kappaappa,kappa1,'-kappa',kappaappa,kappa2,'--kappa',kappaappa,kappa3,'-.kappa',kappaappa,kappa4,':kappa',kappaappa,kappa5,'-kappa',kappaappa,kappa6,'--kappa',kappaappa,kappa7,'-.kappa',kappaappa,kappa8,':kappa');
ylabel('$F/F_{0}$','Interpreter','latex','FontSize',12,'FontName','TiemsNewRoman','FontWeight','Normal')
xlabel('$\kappa$','Interpreter','latex','FontSize',12,'FontName','TiemsNewRoman','FontWeight','Normal')
hold on
set(legend('\phi=1.0','\phi=10.0','NorthEast'),'FontSize',12,'FontName','TiemsNewRoman')
set(gcf,'PaperPosition',[1.5 3.25 5.5 4.5]);
axis([0 10 0 10])
then this message appears
Warning: Ignoring extra legend entries.
> In legend>process_inputs (line 587)
In legend>make_legend (line 315)
In legend (line 259)
In Force_1 (line 47)
curves donot appear also.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by