How to extract the result after each iteration while using for loop?

8 次查看(过去 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

采纳的回答

Stephen23
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
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 CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by