How to extract the result after each iteration while using for loop?
조회 수: 15 (최근 30일)
이전 댓글 표시
I am working with nested for loop. My code seems to work. However I need to extract the result after each iteration and finally plot the result with respect to the iteration value . Can anyone help me doing that.
clc
clear all
close all
%parametrs
Fs= 2.16*10^-5*pi; % Geometrical Factor for sun
Fa=pi; % Geometrical Factor for earth
q=1.6*10^-16; % Electronic Charge
h= 6.626*10^-34; % Plancks Constant
c= 3*10^8; % Speed of light
K = 8.61*10^-5; % Boltzmanns Constant
Ts=5760; % Temparature of the sun
Ta=300; % Ambient Temparature
A=((2*Fs)/((h^3)*(c^2)));
B=((2*Fa)/((h^3)*(c^2)));
%Power Input
Irradiance=@(E) ((q*A).*(E.^2./(exp((E./(K.*Ts))-1))));
Pinput=integral(Irradiance,0,inf);
vdata=0:0.01:1.6;
n=length(vdata);
Jdark=zeros(1,n);
for i=1.55:0.1:3.6
Eg=i;
Absorbrd_Flux=@(E) ((q*A).*(E.^2./(exp((E./(K.*Ts))-1))));
Jsc=integral(Absorbrd_Flux,Eg,inf);
for j=0:0.1:1.6
V=j;
Jd=@(E) ((q*B).*((E.^2./(exp((E-V)./K*Ta)-1))-(E.^2./(exp(E./K*Ta)-1))));
Jdark=integral(Jd,Eg,inf);
Jv=(Jsc-Jdark);
Pv=V.*Jv;
efficiency=Pv/Pinput;
end
Max_Efficiency=max(efficiency)
end
I need to plot Max_efficiency vs Eg or in this case Max_Efficiency vs i
댓글 수: 0
채택된 답변
Stephen23
2022년 2월 4일
편집: Stephen23
2022년 2월 4일
With MATLAB it is almost always better to loop over indices rather than over data values. That makes it easier to save the data in arrays. For example:
Eg_vec = 1.55:0.1:3.6;
Eg_num = numel(Eg_vec);
output = nan(1,Eg_num); % preallocate the output array.
for k = 1:Eg_num
Eg = Eg_vec(k);
.. the rest of your code in the loop
output(k) = max(efficiency);
end
plot(Eg_vec,output)
Avoid i and j as loop iterators, they are already defined as the imaginary unit.
댓글 수: 4
Stephen23
2022년 2월 4일
"The results are coming out different."
Different compared to what? As you were not plotting anything before, I do not understand what you are comparing.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!