Borrar filtros
Borrar filtros

setting two legend for two function

4 visualizaciones (últimos 30 días)
Moslem Uddin
Moslem Uddin el 9 de En. de 2019
Editada: Luna el 12 de En. de 2019
Please help me in setting two legend for two functions in the attached code.
  1 comentario
Luna
Luna el 9 de En. de 2019
What is your desired output will look like?

Iniciar sesión para comentar.

Respuesta aceptada

Luna
Luna el 9 de En. de 2019
Editada: Luna el 9 de En. de 2019
Moslem,
You can try this below, it is a little bit tricky to add legend to axis handle as parent. Then add other legend.
See my comments in green below:
syms y
p=.965; m1=.28;b=[2 5 7];c=0.7;
q=.585e-3; m2=.0125; x=pi/4; h1=10; h2=5;
g1=p*sin(x);g2=q*sin(x);a1=(g1.*h1+g2.*h2)/h1;
%for i=1:length(b)
%y = 0:0.5:10;
hfig = figure;
for i=1:length(b)
f=y.*(g1/(2*m1)).*(2.*h1-y)+(g2*h2/m1).*y+b(i);
hPlotf(i)= fplot(f,y,[0,h1]); % hPlot is handle of function plot f
hold on;
%y = 10:0.5:15;
f1=y.*(g2/(2*m2)).*(2.*(h1+h2)-y)+((g1/(2*m1))-(g2/(2*m2))).*(h1^2)+(g2.*h1.*h2).*((1/m1)-(1/m2))+b(i);
%feval(f1,y)
%figure
hPlotf1(i) = fplot(f1,y,[h1,15]); % hPlot is handle of function plot f1
hold on;
end
a=axes('position',get(gca,'position'),'visible','off'); % a is the current axis handle
hLf = legend(a,hPlotf,'b = 2','b = 5','b=7','Location','SouthWest'); %hLf - legend handle for f
text(a, hLf.Position(1)-0.1,hLf.Position(2)+0.02,'f'); % adding text for f according to hLf position
hLf1 = legend(hPlotf1,'b = 2','b = 5','b=7','Location','NorthWest'); %hLf1 - legend handle for f1
text(a, hLf1.Position(1)-0.1,hLf1.Position(2)+0.02,'f1'); % adding text for f1 according to hLf1 position
xlabel('y','FontSize', 20);
ylabel('u_2','FontSize', 20);
%end
  3 comentarios
Luna
Luna el 11 de En. de 2019
Which version of Matlab you are using?
Luna
Luna el 11 de En. de 2019
Editada: Luna el 12 de En. de 2019
OK now I understand it is because the axis we are trying to put labels is not currently focused axis handle object.
Move xlabel and ylabel lines right below the end of the for loop as follows, then it will fix your problem.
hPlotf1(i) = fplot(f1,y,[h1,15]); % hPlot is handle of function plot f1
hold on;
end
xlabel('y','FontSize', 20);
ylabel('u_2','FontSize', 20);
Please accept answer if it is working :)

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by