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

12 views (last 30 days)
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.

Answers (1)

Davide Masiello
Davide Masiello on 3 Nov 2022
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 Comment
Davide Masiello
Davide Masiello on 3 Nov 2022
Edited: Davide Masiello on 3 Nov 2022
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");

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!