how i can improve this code?

1 Ansicht (letzte 30 Tage)
Fadlu Ibrahim
Fadlu Ibrahim am 8 Jan. 2024
Beantwortet: Ayush am 8 Jan. 2024
function Ia = bp_sx150s(Va,G,TaC)
% function bp_sx150s.m models the BP SX 150S PV module
% calculates module current under given voltage, irradiance and temperature
% Ia = bp_sx150s(Va,G,T)
%
% Out: Ia = Module operating current (A), vector or scalar
% In: Va = Module operating voltage (V), vector or scalar
% G = Irradiance (1G = 1000 W/m^2), scalar
% TaC = Module temperature in deg C, scalar
% Define constants
k = 1.381e-23; % Boltzmann’s constant
q = 1.602e-19; % Electron charge
% Following constants are taken from the datasheet of PV module and
% curve fitting of I-V character (Use data for 1000W/m^2)
n = 2; % Diode ideality factor (n),
% 1 (ideal diode) < n < 2
Eg = 1.12; % Band gap energy; 1.12eV (Si), 1.42 (GaAs),
% 1.5 (CdTe), 1.75 (amorphous Si)
Ns = 72; % # of series connected cells (BP SX150s, 72 cells)
TrK = 298; % Reference temperature (25C) in Kelvin
Voc_TrK = 36.1 /Ns; % Voc (open circuit voltage per cell) @ temp TrK
Isc_TrK = 7.55; % Isc (short circuit current per cell) @ temp TrK
a = 0.65e-3; % Temperature coefficient of Isc (0.065%/C)
% Define variables
TaK = 273 + TaC; % Module temperature in Kelvin
Vc = Va / Ns; % Cell voltage
% Calculate short-circuit current for TaK
Isc = Isc_TrK * (1 + (a * (TaK - TrK)));
% Calculate photon generated current @ given irradiance
Iph = G * Isc;
% Define thermal potential (Vt) at temp TrK
Vt_TrK = n * k * TrK / q;
% Define b = Eg * q/(n*k);
b = Eg * q /(n * k);
% Calculate reverse saturation current for given temperature
Ir_TrK = Isc_TrK / (exp(Voc_TrK / Vt_TrK) -1);
Ir = Ir_TrK * (TaK / TrK)^(3/n) * exp(-b * (1 / TaK -1 / TrK));
% Calculate series resistance per cell (Rs = 5.1mOhm)
dVdI_Voc = -1.0/Ns; % Take dV/dI @ Voc from I-V curve of datasheet
Xv = Ir_TrK / Vt_TrK * exp(Voc_TrK / Vt_TrK);
Rs = - dVdI_Voc - 1/Xv;
% Define thermal potential (Vt) at temp Ta
Vt_Ta = n * k * TaK / q;
% Ia = Iph - Ir * (exp((Vc + Ia * Rs) / Vt_Ta) -1)
% f(Ia) = Iph - Ia - Ir * ( exp((Vc + Ia * Rs) / Vt_Ta) -1) = 0
% Solve for Ia by Newton's method: Ia2 = Ia1 - f(Ia1)/f'(Ia1)
Ia=zeros(size(Vc)); % Initialize Ia with zeros
% Perform 5 iterations
for j=1:5
end
Ia = Ia - (Iph - Ia - Ir .* ( exp((Vc + Ia .* Rs) ./ Vt_Ta) -1))...
./ (-1 - Ir * (Rs ./ Vt_Ta) .* exp((Vc + Ia .* Rs) ./ Vt_Ta));
End
  2 Kommentare
Fadlu Ibrahim
Fadlu Ibrahim am 8 Jan. 2024
Bearbeitet: madhan ravi am 8 Jan. 2024
function Ia = bp_sx150s(Va,G,TaC)
% function bp_sx150s.m models the BP SX 150S PV module
% calculates module current under given voltage, irradiance and temperature
% Ia = bp_sx150s(Va,G,T)
%
% Out: Ia = Module operating current (A), vector or scalar
% In: Va = Module operating voltage (V), vector or scalar
% G = Irradiance (1G = 1000 W/m^2), scalar
% TaC = Module temperature in deg C, scalar
% Define constants
k = 1.381e-23; % Boltzmann’s constant
q = 1.602e-19; % Electron charge
% Following constants are taken from the datasheet of PV module and
% curve fitting of I-V character (Use data for 1000W/m^2)
n = 2; % Diode ideality factor (n),
% 1 (ideal diode) < n < 2
Eg = 1.12; % Band gap energy; 1.12eV (Si), 1.42 (GaAs),
% 1.5 (CdTe), 1.75 (amorphous Si)
Ns = 72; % # of series connected cells (BP SX150s, 72 cells)
TrK = 298; % Reference temperature (25C) in Kelvin
Voc_TrK = 36.1 /Ns; % Voc (open circuit voltage per cell) @ temp TrK
Isc_TrK = 7.55; % Isc (short circuit current per cell) @ temp TrK
a = 0.65e-3; % Temperature coefficient of Isc (0.065%/C)
% Define variables
TaK = 273 + TaC; % Module temperature in Kelvin
Vc = Va / Ns; % Cell voltage
% Calculate short-circuit current for TaK
Isc = Isc_TrK * (1 + (a * (TaK - TrK)));
% Calculate photon generated current @ given irradiance
Iph = G * Isc;
% Define thermal potential (Vt) at temp TrK
Vt_TrK = n * k * TrK / q;
% Define b = Eg * q/(n*k);
b = Eg * q /(n * k);
% Calculate reverse saturation current for given temperature
Ir_TrK = Isc_TrK / (exp(Voc_TrK / Vt_TrK) -1);
Ir = Ir_TrK * (TaK / TrK)^(3/n) * exp(-b * (1 / TaK -1 / TrK));
% Calculate series resistance per cell (Rs = 5.1mOhm)
dVdI_Voc = -1.0/Ns; % Take dV/dI @ Voc from I-V curve of datasheet
Xv = Ir_TrK / Vt_TrK * exp(Voc_TrK / Vt_TrK);
Rs = - dVdI_Voc - 1/Xv;
% Define thermal potential (Vt) at temp Ta
Vt_Ta = n * k * TaK / q;
% Ia = Iph - Ir * (exp((Vc + Ia * Rs) / Vt_Ta) -1)
% f(Ia) = Iph - Ia - Ir * ( exp((Vc + Ia * Rs) / Vt_Ta) -1) = 0
% Solve for Ia by Newton's method: Ia2 = Ia1 - f(Ia1)/f'(Ia1)
Ia=zeros(size(Vc)); % Initialize Ia with zeros
% Perform 5 iterations
for j=1:5
end
Ia = Ia - (Iph - Ia - Ir .* ( exp((Vc + Ia .* Rs) ./ Vt_Ta) -1))...
./ (-1 - Ir * (Rs ./ Vt_Ta) .* exp((Vc + Ia .* Rs) ./ Vt_Ta));
End
Dyuman Joshi
Dyuman Joshi am 8 Jan. 2024
What is the use of the empty for loop?
Improve as in how? The code looks good on a cursory glance. You should profile your code and see where the bottleneck is.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ayush
Ayush am 8 Jan. 2024
Here are some improvements and corrections to enhance the code's readability, performance, and functionality:
  1. Add a check for the convergence of Newton's method.
  2. Vectorize the for-loop for better performance.
  3. Use more descriptive variable names.
  4. Add comments to explain the code more clearly.
  5. Provide default values for the function inputs to make it more user-friendly.
  6. Ensure the function is properly vectorized to handle both scalar and vector inputs for Va.
function Ia = bp_sx150s(Va, G, TaC)
% bp_sx150s models the BP SX 150S PV module and calculates module current
% under given voltage, irradiance, and temperature.
%
% Outputs:
% Ia - Module operating current (A), vector or scalar
%
% Inputs:
% Va - Module operating voltage (V), vector or scalar
% G - Irradiance (1G = 1000 W/m^2), scalar
% TaC - Module temperature in deg C, scalar
% Constants
k = 1.381e-23; % Boltzmann's constant (J/K)
q = 1.602e-19; % Electron charge (C)
n = 2; % Diode ideality factor
Eg = 1.12; % Band gap energy for Silicon (eV)
Ns = 72; % Number of series connected cells
TrK = 298; % Reference temperature (25C) in Kelvin
Voc_TrK = 36.1 / Ns; % Open circuit voltage per cell at TrK (V)
Isc_TrK = 7.55; % Short circuit current per cell at TrK (A)
a = 0.65e-3; % Temperature coefficient of Isc (A/K)
% Variables
TaK = 273 + TaC; % Module temperature in Kelvin
Vc = Va / Ns; % Cell voltage (V)
% Calculate short-circuit current for TaK
Isc = Isc_TrK * (1 + a * (TaK - TrK));
% Calculate photon generated current at given irradiance
Iph = G * Isc;
% Thermal voltage at reference temperature
Vt_TrK = n * k * TrK / q;
% Reverse saturation current at reference temperature
Ir_TrK = Isc_TrK / (exp(Voc_TrK / Vt_TrK) - 1);
% Coefficient b
b = Eg * q / (n * k);
% Reverse saturation current at operating temperature
Ir = Ir_TrK * (TaK / TrK)^(3/n) * exp(-b * (1 / TaK - 1 / TrK));
% Series resistance per cell (from datasheet)
dVdI_Voc = -1.0 / Ns; % Slope at Voc
Xv = Ir_TrK / Vt_TrK * exp(Voc_TrK / Vt_TrK);
Rs = -dVdI_Voc - 1 / Xv;
% Thermal voltage at operating temperature
Vt_Ta = n * k * TaK / q;
% Initialize Ia with zeros and set a tolerance for convergence
Ia = zeros(size(Va));
tolerance = 1e-6;
maxIterations = 100;
% Newton-Raphson method to solve for Ia
for j = 1:maxIterations
f = Iph - Ia - Ir .* (exp((Vc + Ia .* Rs) ./ Vt_Ta) - 1);
df = -1 - (Ir .* Rs ./ Vt_Ta) .* exp((Vc + Ia .* Rs) ./ Vt_Ta);
delta = f ./ df;
Ia = Ia - delta;
% Check for convergence
if all(abs(delta) < tolerance)
break;
end
end
% If convergence is not reached, a warning can be displayed
if j == maxIterations
warning('Newton-Raphson did not converge within the maximum number of iterations.');
end
end
Thanks,
Ayush

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by