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

12 ビュー (過去 30 日間)
Md. Golam Zakaria
Md. Golam Zakaria 2022 年 2 月 4 日
コメント済み: bhawna 2025 年 1 月 29 日
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.
  5 件のコメント
Md. Golam Zakaria
Md. Golam Zakaria 2022 年 2 月 4 日
never mind. I checked. everything is fine
bhawna
bhawna 2025 年 1 月 29 日
@Stephen23 thank you so much, your solution was very good and helped me.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by