Sum of plots in for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi there,
I generated the attached plots in a for loop.
What I want eventually is to sum these plots to generate the black curve.
How can I go on about that?
Thank you for your help in advance,
Arthur ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/316350/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/316350/image.png)
댓글 수: 2
답변 (2개)
Rob Robinson
2020년 6월 15일
I don't think the black curve is the sum of all of these curves as the amplitude would be significantly higher than any of the one lines when they are so close to being in phase? I've given an example below for just two curves of how you could calculate the sum of the curves or alternatively the average or maximum. Hopefully one of those is what you are looking for.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/316383/image.png)
c = 0:0.25:0.25;
x = linspace(1,10,100);
figure()
hold on
ySum = zeros(length(c),length(x));
for cVal = 1:length(c)
legendName = sprintf('y=sin(x+%.2f)',c(cVal));
y = sin(x+c(cVal)) ;
ySum(cVal,:) = y;
plot(x,y,'DisplayName',legendName);
end
plot(x,mean(ySum),'DisplayName' ,'Average')
plot(x,sum(ySum),'DisplayName' ,'Sum')
plot(x,max(ySum),'DisplayName' ,'Max')
legend('location','best')
Ameer Hamza
2020년 6월 15일
This seems like envelope(): https://www.mathworks.com/help/releases/R2020a/signal/ref/envelope.html of these curves. It is from the signal processing toolbox.
댓글 수: 4
참고 항목
카테고리
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!