Using for loop to plot multiple plots on the same graph
조회 수: 116 (최근 30일)
이전 댓글 표시
I am trying to plot 3 plots on the same graph , I believe the best way to do this would be a for loop. I'd like to keep the paramaters that I am changing flexible, so I would like to call them from an array. This is the code that I have so far, where 'Tau1' is a parameter that varies the function 'G_fun'. I am only getting one plot which the legend is labelling to be the last plot with Tau1 = 1e-5. How would I be get a different plot of G_fun for each value of Tau1 on the same graph? Thank you in advance for any help.
N = [1e-3,1e-4,1e-5];
for i = 1:1:3
Tau1 = N(i); % the parameter to be changed
if N(i) == 1e-3
figure
hold on
end % end if
bode(G_fun);
end
hold off
legend('Tau1 1e-3', 'Tau1 1e-4', 'Tau1 1e-5')
grid on
title('System Bode Plots')
채택된 답변
DGM
2021년 4월 2일
편집: DGM
2021년 4월 2일
I'm going to guess the primary issue is with your function not getting access to the changing parameter, but let's just show that it does work. There are a few things that can be improved:
% i just changed these to suit my example TF
N = [2e-1,1e-1,1e-2];
% you can just move this outside the loop to avoid the conditional
figure
hold on
legendstrings=cell(size(N));
for i = 1:length(N) % make stuff independent of array sizes
Tau1 = N(i);
% idk what your TF is or how tau is used in it
% this is just some garbage i made as an example
H = tf([1 0.1 7.5*(1-Tau1)],[1 0.12 9 0 0]);
bode(H)
% build the legends from the parameters themselves
% instead of using literal strings that might be wrong if N changes
legendstrings{i}=sprintf('Tau1 = %2.2e',Tau1);
end
hold off
legend(legendstrings,'location','northwest') % you can just set the location as needed
grid on
title('System Bode Plots')
추가 답변 (1개)
William Rose
2021년 4월 2일
Try putting the figure command and the hold on command outside and before the for loop.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!