필터 지우기
필터 지우기

setting two legend for two function

조회 수: 4 (최근 30일)
Moslem Uddin
Moslem Uddin 2019년 1월 9일
편집: Luna 2019년 1월 12일
Please help me in setting two legend for two functions in the attached code.
  댓글 수: 1
Luna
Luna 2019년 1월 9일
What is your desired output will look like?

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

채택된 답변

Luna
Luna 2019년 1월 9일
편집: Luna 2019년 1월 9일
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
Luna
Luna 2019년 1월 11일
Which version of Matlab you are using?
Luna
Luna 2019년 1월 11일
편집: Luna 2019년 1월 12일
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 :)

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

추가 답변 (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