Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
for loop hanging in mfile

Subject: for loop hanging in mfile

From: mado

Date: 14 Jan, 2014 18:45:08

Message: 1 of 2

I want to draw characteristic curve for photovoltaic panel at different temperature temp=[25,35,45,55] but it enters for loop and can't exit when i change all temp to 25 the nominal temperature it works any other change hang.

clear all
clc
close all hidden
%% Information from the MSX60 solar array datasheet

% You may change these parameters to fit the I-V model
% to other kinds of solar arrays.

Iscn = 4.7; %Nominal short-circuit voltage [A]
Vocn = 43.2; %Nominal array open-circuit voltage [V]
Imp = 4.35; %Array current @ maximum power point [A]
Vmp = 34.6; %Array voltage @ maximum power point [V]
Pmax_e = Vmp*Imp; %Array maximum output peak power [W]
Kv = -0.223*Vocn %Voltage/temperature coefficient [V/K]
Ki = 0.0042*Iscn %Current/temperature coefficient [A/K]
Ns = 72; %Number of series cells
Npp= 1 ;
Nss= 1;
 

%% Constants

k = 1.3806503e-23; %Boltzmann (J/K)
q = 1.60217646e-19; %Electron charge (C)
a1 = 1; %Diode Ideality Facotrs constant
a=1;
p=a1;


%% Algorithm parameters

%Increment of Rs
Rsinc = 0.0001;

%Initial value of "a"
a = 1.0;

%Increment of "a"
ainc = 0.005;

%Maximum tolerable power error
tol = 0.0001;

%Maximum number of iteractions for each value of "a"
nimax = 10000;

%Voltage points in each iteraction
nv = 100;

%Number of "a" values to try
namax = 80;


%% Nominal values

Gn = 1000; % Nominal irradiance (W/m^2) @ 25oC
Tn = 25 + 273.15; % Nominal operating temperature (K)

Irrad=[1000,800,600,400,200];
Temp=[65,55,45,35,25];
Temperature=1; % Set Temperature=1 for Temperature changes Curves;
Irradiance=0; % Set Irradiance=1 for Irradiance changes Curves;
%% Code for generating I-V and P-V curves

for j=1:6
% %% Adjusting algorithm
    if Temperature==1
    if j==6
        break
    end
    T=Temp(j)+273.15;
        G=Irrad(j);
    end
    if Irradiance==1
        G=Irrad(j);
        T=298.15;
    end

Vtn = k * Tn / q; %Thermal junction voltage (nominal)
Vt = k * T / q; %Thermal junction voltage (current temperature)
Rp =268.013169;Rs = 0.692200; %these Values are calculated using MSX60.m file

% Temperature and irradiation effect on the current
dT = T-Tn;
Ipvn = Iscn; % Nominal light-generated current((a1+a2)/2.2)
Ipv = (Ipvn + Ki*dT) *G/Gn; % Actual light-generated current
Isc_ = ( Iscn + Ki*dT );
Voc_ = ( Vocn + Kv*dT );
%Io = (Ipv - Vocn/Rp)/(exp(Vocn/Vt/a1/Ns)-1); %% NEW %% UPDATED ON JUNE/2010 %%
Io = Isc_/(exp(Voc_/((a1)/p)/Ns/Vt)-1);
clear V
clear I

V = 0:Vocn/nv:Vocn; % Voltage vector
I = zeros(1,size(V,2)); % Current vector

for j = 1 : length(V) %Calculates for all voltage values
    
% Solves g = I - f(I,V) = 0 with Newntonn-Raphson
  
g(j) = Ipv-Io*(exp((V(j)+I(j)*Rs)/Vt/Ns/a1)-1)-(V(j)+I(j)*Rs)/Rp-I(j);
  
while (abs(g(j)) > 0.001)
      
g(j) = Ipv-Io*(exp((V(j)+I(j)*Rs)/Vt/Ns/a1)-1)-(V(j)+I(j)*Rs)/Rp-I(j);
glin(j) = -Io*Rs/Vt/Ns/a1*exp((V(j)+I(j)*Rs)/Vt/Ns/a1)-Rs/Rp-1;
I_(j) = I(j) - g(j)/glin(j);
I(j) = I_(j);

end

end % fr j = 1 : size(V,2)
%% Display the I-V and P-V curves.

% I-V curve
 figure(3)
 grid on
 hold on
 xlabel('V (V)');
 ylabel('I (A)');
 xlim([0 47]);
 ylim([0 6]);
 plot(V,I,'LineWidth',2,'Color','b'); %
   text(38,5, 'T=25 °C ', 'Color', 'r');
     text(20, 4.8, '1000 W/ m2 ', 'Color', 'k');
   text(20, 3.8, '800 W/ m2 ', 'Color', 'k');
   text(20, 2.9, '600 W/ m2 ', 'Color', 'k');
    text(20, 1.9, '400 W/ m2 ', 'Color', 'k');
   text(20, 1.2, '200 W/ m2 ', 'Color', 'k');

 % P-V curve
 figure(4)
 grid on
 hold on
 xlabel('V (V)');
 ylabel('P (W)');
 xlim([0 47]);
 ylim([0 160]);
   text(36, 150, '1000 W/m2 ', 'Color', 'k');
   text(36, 125, '800 W/m2 ', 'Color', 'k');
   text(36, 95, '600 W/m2 ', 'Color', 'k');
    text(36, 65, '400 W/m2 ', 'Color', 'k');
   text(36, 35, '200 W/m2 ', 'Color', 'k');


 plot(V,V.*I,'LineWidth',2,'Color','b'); %
end

Subject: for loop hanging in mfile

From: Kevin Ellis

Date: 16 Jan, 2014 23:35:08

Message: 2 of 2

"mado " <mahmmod.shaban@yahoo.com> wrote in message <lb40jj$1tl$1@newscl01ah.mathworks.com>...
> I want to draw characteristic curve for photovoltaic panel at different temperature temp=[25,35,45,55] but it enters for loop and can't exit when i change all temp to 25 the nominal temperature it works any other change hang.
>
> clear all
> clc
> close all hidden
> %% Information from the MSX60 solar array datasheet
>
> % You may change these parameters to fit the I-V model
> % to other kinds of solar arrays.
>
> Iscn = 4.7; %Nominal short-circuit voltage [A]
> Vocn = 43.2; %Nominal array open-circuit voltage [V]
> Imp = 4.35; %Array current @ maximum power point [A]
> Vmp = 34.6; %Array voltage @ maximum power point [V]
> Pmax_e = Vmp*Imp; %Array maximum output peak power [W]
> Kv = -0.223*Vocn %Voltage/temperature coefficient [V/K]
> Ki = 0.0042*Iscn %Current/temperature coefficient [A/K]
> Ns = 72; %Number of series cells
> Npp= 1 ;
> Nss= 1;
>
>
> %% Constants
>
> k = 1.3806503e-23; %Boltzmann (J/K)
> q = 1.60217646e-19; %Electron charge (C)
> a1 = 1; %Diode Ideality Facotrs constant
> a=1;
> p=a1;
>
>
> %% Algorithm parameters
>
> %Increment of Rs
> Rsinc = 0.0001;
>
> %Initial value of "a"
> a = 1.0;
>
> %Increment of "a"
> ainc = 0.005;
>
> %Maximum tolerable power error
> tol = 0.0001;
>
> %Maximum number of iteractions for each value of "a"
> nimax = 10000;
>
> %Voltage points in each iteraction
> nv = 100;
>
> %Number of "a" values to try
> namax = 80;
>
>
> %% Nominal values
>
> Gn = 1000; % Nominal irradiance (W/m^2) @ 25oC
> Tn = 25 + 273.15; % Nominal operating temperature (K)
>
> Irrad=[1000,800,600,400,200];
> Temp=[65,55,45,35,25];
> Temperature=1; % Set Temperature=1 for Temperature changes Curves;
> Irradiance=0; % Set Irradiance=1 for Irradiance changes Curves;
> %% Code for generating I-V and P-V curves
>
> for j=1:6
> % %% Adjusting algorithm
> if Temperature==1
> if j==6
> break
> end
> T=Temp(j)+273.15;
> G=Irrad(j);
> end
> if Irradiance==1
> G=Irrad(j);
> T=298.15;
> end
>
> Vtn = k * Tn / q; %Thermal junction voltage (nominal)
> Vt = k * T / q; %Thermal junction voltage (current temperature)
> Rp =268.013169;Rs = 0.692200; %these Values are calculated using MSX60.m file
>
> % Temperature and irradiation effect on the current
> dT = T-Tn;
> Ipvn = Iscn; % Nominal light-generated current((a1+a2)/2.2)
> Ipv = (Ipvn + Ki*dT) *G/Gn; % Actual light-generated current
> Isc_ = ( Iscn + Ki*dT );
> Voc_ = ( Vocn + Kv*dT );
> %Io = (Ipv - Vocn/Rp)/(exp(Vocn/Vt/a1/Ns)-1); %% NEW %% UPDATED ON JUNE/2010 %%
> Io = Isc_/(exp(Voc_/((a1)/p)/Ns/Vt)-1);
> clear V
> clear I
>
> V = 0:Vocn/nv:Vocn; % Voltage vector
> I = zeros(1,size(V,2)); % Current vector
>
> for j = 1 : length(V) %Calculates for all voltage values
>
> % Solves g = I - f(I,V) = 0 with Newntonn-Raphson
>
> g(j) = Ipv-Io*(exp((V(j)+I(j)*Rs)/Vt/Ns/a1)-1)-(V(j)+I(j)*Rs)/Rp-I(j);
>
> while (abs(g(j)) > 0.001)
>
> g(j) = Ipv-Io*(exp((V(j)+I(j)*Rs)/Vt/Ns/a1)-1)-(V(j)+I(j)*Rs)/Rp-I(j);
> glin(j) = -Io*Rs/Vt/Ns/a1*exp((V(j)+I(j)*Rs)/Vt/Ns/a1)-Rs/Rp-1;
> I_(j) = I(j) - g(j)/glin(j);
> I(j) = I_(j);
>
> end
>
> end % fr j = 1 : size(V,2)
> %% Display the I-V and P-V curves.
>
> % I-V curve
> figure(3)
> grid on
> hold on
> xlabel('V (V)');
> ylabel('I (A)');
> xlim([0 47]);
> ylim([0 6]);
> plot(V,I,'LineWidth',2,'Color','b'); %
> text(38,5, 'T=25 °C ', 'Color', 'r');
> text(20, 4.8, '1000 W/ m2 ', 'Color', 'k');
> text(20, 3.8, '800 W/ m2 ', 'Color', 'k');
> text(20, 2.9, '600 W/ m2 ', 'Color', 'k');
> text(20, 1.9, '400 W/ m2 ', 'Color', 'k');
> text(20, 1.2, '200 W/ m2 ', 'Color', 'k');
>
> % P-V curve
> figure(4)
> grid on
> hold on
> xlabel('V (V)');
> ylabel('P (W)');
> xlim([0 47]);
> ylim([0 160]);
> text(36, 150, '1000 W/m2 ', 'Color', 'k');
> text(36, 125, '800 W/m2 ', 'Color', 'k');
> text(36, 95, '600 W/m2 ', 'Color', 'k');
> text(36, 65, '400 W/m2 ', 'Color', 'k');
> text(36, 35, '200 W/m2 ', 'Color', 'k');
>
>
> plot(V,V.*I,'LineWidth',2,'Color','b'); %
> end

Mado,

I believe I figured out your error. There were a couple of errors in your code. I had to rearrange where you put the plot function so the graph comes out accurately. But to answer your question, you code was getting stuck in the 'while' loop. If you stepped through it iteration by iteration you would see you were passing it an always 'true' condition. Be very careful of 'while' loops. I would always try to use if/else statements or pretty much anything else before using a 'while' loop.

Paste THIS code from in place of your for your loop:

%% Code for generating I-V and P-V curves

for j=1:6
%% Adjusting algorithm
    if Temperature==1
        if j==6
            break
        end
        T=Temp(j)+273.15;
        G=Irrad(j);
    end
    if Irradiance==1
        G=Irrad(j);
        T=298.15;
    end

    Vtn = k * Tn / q; %Thermal junction voltage (nominal)
    Vt = k * T / q; %Thermal junction voltage (current temperature)
    Rp =268.013169;Rs = 0.692200; %these Values are calculated using MSX60.m file

    % Temperature and irradiation effect on the current
    dT = T-Tn;
    Ipvn = Iscn; % Nominal light-generated current((a1+a2)/2.2)
    Ipv = (Ipvn + Ki*dT) *G/Gn; % Actual light-generated current
    Isc_ = ( Iscn + Ki*dT );
    Voc_ = ( Vocn + Kv*dT );
    %Io = (Ipv - Vocn/Rp)/(exp(Vocn/Vt/a1/Ns)-1); %% NEW %% UPDATED ON JUNE/2010 %%
    Io = Isc_/(exp(Voc_/((a1)/p)/Ns/Vt)-1);
    clear V
    clear I

    V = 0:Vocn/nv:Vocn; % Voltage vector
    I = zeros(1,size(V,2)); % Current vector

    for i = 1 : length(V) %Calculates for all voltage values
    
        % Solves g = I - f(I,V) = 0 with Newntonn-Raphson
  
        g(i) = Ipv-Io*(exp((V(i)+I(i)*Rs)/Vt/Ns/a1)-1)-(V(i)+I(i)*Rs)/Rp-I(i);
        
        if (abs(g(i)) > 0.001)
        %while (abs(g(i)) > 0.001)

            g(i) = Ipv-Io*(exp((V(i)+I(i)*Rs)/Vt/Ns/a1)-1)-(V(i)+I(i)*Rs)/Rp-I(i);
            glin(i) = -Io*Rs/Vt/Ns/a1*exp((V(i)+I(i)*Rs)/Vt/Ns/a1)-Rs/Rp-1;
            I_(i) = I(i) - g(i)/glin(i);
            I(i) = I_(i);

        end

    end % fr j = 1 : size(V,2)
%% Display the I-V and P-V curves.

    % I-V curve
     figure(3)
     grid on
     hold on
     xlabel('V (V)');
     ylabel('I (A)');
     xlim([0 47]);
     ylim([0 6]);
     plot(V,I,'LineWidth',2,'Color','b'); %
       text(38,5, 'T=25 °C ', 'Color', 'r');
         text(20, 4.8, '1000 W/ m2 ', 'Color', 'k');
       text(20, 3.8, '800 W/ m2 ', 'Color', 'k');
       text(20, 2.9, '600 W/ m2 ', 'Color', 'k');
        text(20, 1.9, '400 W/ m2 ', 'Color', 'k');
       text(20, 1.2, '200 W/ m2 ', 'Color', 'k');

     % P-V curve
     figure(4)
     grid on
     hold on
     xlabel('V (V)');
     ylabel('P (W)');
     xlim([0 47]);
     ylim([0 160]);
     plot(V,V.*I,'LineWidth',2,'Color','b'); %
       text(36, 150, '1000 W/m2 ', 'Color', 'k');
       text(36, 125, '800 W/m2 ', 'Color', 'k');
       text(36, 95, '600 W/m2 ', 'Color', 'k');
        text(36, 65, '400 W/m2 ', 'Color', 'k');
       text(36, 35, '200 W/m2 ', 'Color', 'k');
end

If you use this, you should get all six lines plotted on your power/voltage and current/voltage graphs in thick blue lines. Hope this is what you need.

Kevin

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us