Plot all for loop results
조회 수: 1 (최근 30일)
이전 댓글 표시
I'm doing this exercise about ploting sine waves. The program uses two sine waves. The first one has the same frequency always (f1). The second one changes value (f2= [9 9.5 13 15]). The exercise is almost done, I just need to plot all figures for each of the four for loop iterations. When I run the program I can see the plots but they are quickly replaced by the next iterations. At the end I end up with three figures and I need all twelve. How can I keep all twelve?
%% Second Wave HW.
clc
t=linspace(1,20,10000); %10000 Datapoints
f1=8;
f2=9;
A=1;
for f2=[9 9.5 13 15]
w= A*sin(f1*t); %Blue Wave
w2=A*sin(f2*t); %Red Wave
% First Graph:
figure(1)
plot(t(1:1000),w(1:1000)) %Only Graphing 2T
hold on
plot(t(1:1000),w2(1:1000))
hold off
title(['Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
% Second Graph:
figure(2)
y=w+w2;
plot(t,y)
title(['Added Waves. Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
%Third Graph:
figure(3)
subplot(2,1,1)
plot(t,y)
title(['Composite. Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
subplot(2,1,2)
plot(t(1:1000),w(1:1000)) %Only Graphing 2T
hold on
plot(t(1:1000),w2(1:1000))
hold off
title(['Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
end
댓글 수: 0
채택된 답변
Star Strider
2019년 8월 1일
Don’t number the figures. They will increment themselves.
Try this:
t=linspace(1,20,10000); %10000 Datapoints
f1=8;
f2=9;
A=1;
w= A*sin(f1*t); %Blue Wave
for f2=[9 9.5 13 15]
w2=A*sin(f2*t); %Red Wave
% First Graph:
figure
plot(t(1:1000),w(1:1000)) %Only Graphing 2T
hold on
plot(t(1:1000),w2(1:1000))
hold off
title(['Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
% Second Graph:
figure
y=w+w2;
plot(t,y)
title(['Added Waves. Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
%Third Graph:
figure
subplot(2,1,1)
plot(t,y)
title(['Composite. Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
subplot(2,1,2)
plot(t(1:1000),w(1:1000)) %Only Graphing 2T
hold on
plot(t(1:1000),w2(1:1000))
hold off
title(['Frequencies used= ',num2str(f1),' and ',num2str(f2)])
ylabel(['Amplitude= ',num2str(A)])
end
The only other change I made was to move ‘w’ outside the loop. There is no need to recalculate it each time if it doesn’t change.
댓글 수: 0
추가 답변 (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!