How do i set up a legend for a graph with variable parameters?

13 次查看(过去 30 天)
Hey,
i want to automize this legend, i do not want to write every case for the parameters.
t = -2:0.1:4;
d = 1;
c = 1;
hold on ;
for A=1:0.2:2
y= A * (t - d).*(t-c) ;
plot (t,y) ;
end
xlim ([-2 4]);
ylim([-1 5]) ;
grid on ;
xlabel('x-Achse');
ylabel('y-Achse');
titletext = 'Funktion f(x) = $A*(x-x1)*(x-x2)$';
title(titletext,Interpreter="latex");
leg = legend('1,0','1,2','1,4','1,6','1,8','2,0', Location='best');
title(leg,'A=');
hold off ;
What ae your Ideas?
And thanks for the help.

回答(1 个)

Davide Masiello
Davide Masiello 2022-11-3
t = -2:0.1:4;
d = 1;
c = 1;
A = 1:0.2:2;
figure
hold on
for i = 1:length(A)
y= A(i) * (t - d).*(t-c) ;
plot (t,y)
end
xlim ([-2 4]);
ylim([-1 5]) ;
grid on
xlabel('x-Achse')
ylabel('y-Achse')
titletext = 'Funktion f(x) = $A*(x-x1)*(x-x2)$';
title(titletext,Interpreter="latex")
legend([repmat('A = ',length(A),1),num2str(A')], Location='best');
hold off
  1 个评论
Davide Masiello
Davide Masiello 2022-11-3
编辑:Davide Masiello 2022-11-3
Also note this can be done without loop
% parameters
t = -2:0.1:4;
d = 1;
c = 1;
A = 1:0.2:2;
% function
y = A'.*(t - d).*(t-c) ;
% plot
plot (t,y)
xlim ([-2 4]);
ylim([-1 5]) ;
grid on
xlabel('x-Achse',Interpreter="latex")
ylabel('y-Achse',Interpreter="latex")
title('Funktion f(x) = $A*(x-x1)*(x-x2)$',Interpreter="latex")
legend([repmat('A = ',length(A),1),num2str(A')], Location='best',Interpreter="latex");

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by