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

조회 수: 5 (최근 30일)
Marc
Marc 2022년 11월 3일
편집: Davide Masiello 2022년 11월 3일
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