Warning "Ignoring extra legend entries"
    조회 수: 24 (최근 30일)
  
       이전 댓글 표시
    
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 

So I do not know why it gives me that .Can someone explain, and any methods to solve it ? 
Thanks in advance,
Khang
댓글 수: 0
채택된 답변
  Alex Mcaulley
      
 2019년 5월 23일
        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
  Alex McCracken
 2020년 10월 23일
				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
추가 답변 (3개)
  Cameron J Reimer
 2021년 3월 1일
        thanks
댓글 수: 1
  Shreen El-Sapa
 2021년 4월 2일
				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.
참고 항목
카테고리
				Help Center 및 File Exchange에서 Legend에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







