I'm adding legend in the graph for the 4 plot in the same graph, please help me in this.

3 views (last 30 days)
ti = 0;
tf = 1E-3;
tspan=[ti tf];
y0 = [(10E-6).*rand(10,1);((-3.14).*rand(4,1) + (3.14).*rand(4,1))]; % intial conditions
o = sort(10e2*rand(1,4),'ascend'); %detuning frequency
t =1E-3;
[T,Y]= ode45(@(t,y) rate_eq(t,y,o),tspan,y0);
figure(1)
plot(T./t,(Y(:,11)),'linewidth',0.8);
hold on
for m = 12:14
plot(T./tf,(Y(:,m)),'linewidth',0.8);
end
hold off
grid on
xlabel("time")
ylabel("phase difference")
legend(Y(:,11),Y(:,12),Y(:,13),Y(:,14),"Location","northeast");
Error using legend
Invalid argument. Type 'help legend' for more information.
set(gca,'fontname','times New Roman','fontsize',18,'linewidth',1.8);
function dy = rate_eq(t,y,o)
dy = zeros(14,1);
P = 1.25;
a = 0.1;
tc = 230E-6;
tp = 5.4E-9;
k = 6E-3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dy(1) = (P - y(1).*((abs(y(6)))^2 +1))./tc;
dy(2) = (P - y(2).*((abs(y(7)))^2 +1))./tc;
dy(3) = (P - y(3).*((abs(y(8)))^2 +1))./tc;
dy(4) = (P - y(4).*((abs(y(9)))^2 +1))./tc;
dy(5) = (P - y(5).*((abs(y(10)))^2 +1))./tc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dy(6)= (y(1)-a).*((y(6))./tp) + (k./tp).*(y(7)).*cos(y(11));
dy(7)= (y(2)-a).*((y(7))./tp) + (k./tp).*(y(8)).*cos(y(12)) + (k./tp).*(y(6))*cos(y(11));
dy(8)= (y(3)-a).*((y(8))./tp) + (k./tp).*(y(9)).*cos(y(13)) + (k./tp).*(y(7))*cos(y(12));
dy(9)= (y(4)-a).*((y(9))./tp) + (k./tp).*(y(10)).*cos(y(14)) + (k./tp).*(y(8))*cos(y(13));
dy(10)= (y(5)-a).*((y(10))./tp) + (k./tp).*(y(9)).*cos(y(14));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
dy(11) = o(1,1) - (k./tp).*(y(6)/y(7)).*(sin(y(11))) - (k./tp).*(y(7)/y(6)).*(sin(y(11))) + (k./tp).*(y(8)./y(7))*sin(y(12));
dy(12) = o(1,2) - (k./tp).*(y(7)/y(8)).*(sin(y(12))) - (k./tp).*(y(8)/y(7)).*(sin(y(12))) + (k./tp).*(y(9)./y(8))*sin(y(13)) + (k./tp).*(y(6)/y(7)).*sin(y(11));
dy(13) = o(1,3) - (k./tp).*(y(8)/y(9)).*(sin(y(13))) - (k./tp).*(y(9)/y(8)).*(sin(y(13))) + (k./tp).*(y(10)./y(9))*sin(y(14)) + (k./tp).*(y(7)/y(8)).*sin(y(12));
dy(14) = o(1,4) - (k./tp).*(y(9)/y(10)).*(sin(y(14))) - (k./tp).*(y(10)/y(9)).*(sin(y(14))) + (k./tp).*(y(8)/y(9)).*sin(y(13));
end

Answers (1)

dpb
dpb on 12 Oct 2022
Edited: dpb on 12 Oct 2022
Use something like
legend('Y(11)','Y(12)','Y(13)','Y(14)',"Location","northeast");
instead.
The legend entries are strings describing the data, not references to the data itself.

Tags

Community Treasure Hunt

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

Start Hunting!