필터 지우기
필터 지우기

Sum of plots in for loop

조회 수: 1 (최근 30일)
Arthur Moya
Arthur Moya 2020년 6월 15일
댓글: Arthur Moya 2020년 7월 31일
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
  댓글 수: 2
KSSV
KSSV 2020년 6월 15일
편집: KSSV 2020년 6월 15일
Share your code....Add all curves...is it what you want?
Arthur Moya
Arthur Moya 2020년 6월 15일
Hi @KSSV,
Please see the code below.
%% %Partial Temporal Coherence curve
WL = 0.0195 % Ang^-1
DeltaS = 36 % Ang
g = [0:0.01:3] % Ang^-1
Etr = exp(-0.5*pi^2*DeltaS*WL^2.*g.^4);
plot (g,Etr);
%% %Contrast Transfer Function
D= [150:12.5:200] %Ang
for i=1:length(D)
Cs = -12960 % Ang
Xr = 0.5*pi.*D(i)*WL.*g.^2 + 0.25*pi*Cs*WL^3.*g.^4;
Wv= sin(Xr).*Etr;
hold on
plot (g,Wv)
xlabel('g(Ang^{-1})','Fontsize',20)
ylabel('sin X', 'Fontsize',20)
legend ('E_{t}','20','40','60','80','Location','best','Fontsize',30)
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 1)
end

댓글을 달려면 로그인하십시오.

답변 (2개)

Rob Robinson
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.
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')
  댓글 수: 1
Arthur Moya
Arthur Moya 2020년 6월 15일
Hi Rob,
I suspect the curve that most resembles what I am looking for is the max curve. I have seen below some suggestions that I may be looking for the envelope. What would be the difference between the max curve and the envelope?
Thank you and kind regards,
Arthur

댓글을 달려면 로그인하십시오.


Ameer Hamza
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
Ameer Hamza
Ameer Hamza 2020년 6월 15일
Can you share the data?
Arthur Moya
Arthur Moya 2020년 7월 31일
Dear @ Ameer Hamza,
below is the code I used to generate the data.
Thank you and kind regards,
Arthur
%% %Partial Temporal Coherence curve
WL = 0.0195 % Ang^-1
DeltaS = 36 % Ang
g = [0:0.01:3] % Ang^-1
Etr = exp(-0.5*pi^2*DeltaS*WL^2.*g.^4);
plot (g,Etr);
%% %Contrast Transfer Function
D= [150:12.5:200] %Ang
for i=1:length(D)
Cs = -12960 % Ang
Xr = 0.5*pi.*D(i)*WL.*g.^2 + 0.25*pi*Cs*WL^3.*g.^4;
Wv= sin(Xr).*Etr;
hold on
plot (g,Wv)
xlabel('g(Ang^{-1})','Fontsize',20)
ylabel('sin X', 'Fontsize',20)
legend ('E_{t}','20','40','60','80','Location','best','Fontsize',30)
line(xlim, [0,0], 'Color', 'k', 'LineWidth', 1)
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by