How to create multiple inputs and generate outputs.

7 views (last 30 days)
Hye, I need help on my program, currently it can only run with a single input - Temperature (T) and Sun Irradiance (G). If you see in my codes, the inputs is only one 'T' and one 'G'. The main objective of this program is to find the PMax and Plot a graph of PMax over time. It can also plot graph of I-V Characteristic and Power Against Voltage.
Let say if I have a 100 inputs to be run, can anyone help me on how to edit the program so that it can import a set of data from a spreadsheet and can find the PMax of each correspondence (T) and (G), and can automatically Plot the graph? Seriously I'm stuck here. Need help asap. Thanks
Here is the current coding that I've made for a single input ;
clc
clear
VOC=64.2; %open circuit voltage (V)
ISC=5.96; %short circuit current (A)
VR=54.7; %reference voltage for maximum power
IR=5.58; %reference current for maximum power
TR=298; %reference temperature (K)
GR=1000; %reference irradiance (W/m^2)
NS=96; %number of cells per module
AM0=1.631; %area of one module (m^2)
mi=0.0035; %temperature coefficient for short circuit current (A/K)
% VOC=44.8; %open circuit voltage (V)
% ISC=8.33; %short circuit current (A)
% VR=35.2; %reference voltage for maximum power
% IR=7.95; %reference current for maximum power
% TR=298; %reference temperature (K)
% GR=1000; %reference irradiance (W/m^2)
% NS=72; %number of cells per module
% AM0=1.94; %area of one module (m^2)
% mi=0.00045; %temperature coefficient for short circuit current (A/K)
%-----CELL PARAMETERS-----
EG=1.124; %energy gap (eV)
A=1.3; %ideality factor
k=1.38*10^-23; %Boltzmann's constant (J/K)
q=1.6*10^-19; %charge of electron (C)
%-----INPUT-----
TA=input('Temperature (oC): ');
T1=TA+273; %convert from Celcius to Kelvin
%T1=input('Temperature (K): ');
G1=input('Solar irradiance (W/m^2): ');
% MS1=input('Number of modules in series (0 for single): ');
% MP1=input('Number of modules in parallel (0 for single): ');
MS1=0;
MP1=0;
%-----CALCULATION OF V BY VARYING I FOR A SINGLE MODULE-----
IL1=(ISC+(mi)*(T1-TR))*(G1/GR); %light generated current
IRS1=ISC/(exp((VOC*q)/(NS*A*k*TR))-1); %reference saturation current
IS1=IRS1*((T1/TR)^3)*exp((q*EG/(A*k))*((1/TR)-(1/T1))); %saturation current
RS1=((NS*A*k*TR/q)*log(((ISC-IR)/IRS1)+1)-VR)/IR; %series resistance
h1=IL1/1000; %setting increment
IA=0:h1:IL1; %range of I with increment h
N1=length(IA); %number of data points
VA(N1)=0; %setting final value of V
for i=1:(N1-1)
VA(i)=(NS*A*k*T1/q)*log(1+(IL1-IA(i))/IS1)-IA(i)*RS1; %computing V
end
%
% -----DETERMINATION OF OUTPUT BASED ON NUMBER OF MODULES-----
if MS1==0 %determining number of modules connected in series
MSA=1; %single module
else
MSA=MS1; %multiple modules connected in series
end
if MP1==0 %determining number of modules connected in parallel
MPA=1; %single module
else
MPA=MP1; %multiple modules connected in parallel
end
V1=MSA*VA; %new voltage limit
I1=MPA*IA; %new current limit
P1(N1)=0; %setting final value of P
for i=1:(N1-1)
P1(i)=I1(i)*V1(i); %computing P
end
%-----MAXIMUM POINT DETERMINATION-----
[PMAX1,i]=max(P1); %maximum P
VMAX1=V1(i); %V at maximum P
IMAX1=I1(i); %I at maximum P
%-----FILL FACTOR DETERMINATION-----
FF1=(VMAX1*IMAX1)/(V1(1)*I1(N1));
%-----EFFICIENCY DETERMINATION-----
AM1=MSA*MPA*AM0; %area based on number of modules
n1=(PMAX1/(AM1*GR))*100;
%-----I-V CHARACTERISTIC PLOT-----
figure(1)
plot(V1,I1), xlabel('Voltage (V)'), ylabel('Current (A)');
%
% %-----POWER AGAINST VOLTAGE PLOT-----
figure(2)
plot(V1,P1), xlabel('Voltage (V)'), ylabel('Power (W)');
  2 Comments
Image Analyst
Image Analyst on 23 Jun 2012
Edit your question. Highlight your code. Then click the "Code" icon above the text box. Do this whenever you have code in your question.
Kip
Kip on 24 Jun 2012
Okay done, sorry I didn't know I need to do that since I'm still new in this community.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 24 Jun 2012
1) Get rid of the "clear"
2) Move your input to the top, above the rest
3) Move everything else into a function that accepts G and T as parameters and does everything (including the plotting) and returns the appropriate values. Then after the part that reads the inputs from the user, insert a call to that function. And wrap the inputting and call into its own function.
function driveit
input....
[output1, output2, ...] = do_the_work(T1, G1);
end
function [output1, output2, ...] = do_the_work(T1, G1)
VOC = <etc>
end
4) When that works, move the plotting into the first routine instead of the second, so the second just does calculations and no plotting.
5) Add code to the first function to read TA, G1 values from the spreadsheet and loop through each pair, calling the work routine for that pair and getting the results back and doing the existing plotting. Use drawnow() to make each one visible as it is created, or put each one in a new figure, or use subplots, or whatever is appropriate.
6) If some kind of new plotting over the overall set of values read in from the spreadsheet, change the first routine to save the relevant information as it loops over the TA, G1 pairs, and do that new plotting after all the calculations have been done.
7) Now remove anything that is no longer relevant (e.g., individual plots.)
There is a completely different strategy available in some cases, involving making the program work for multiple TA, G1 pairs at the same time, a process known as "vectorization". Your code for calculating the resistance, IL1 can be vectorized, as can the next few statements up to calculating the increment. However, the part from IA onward uses different number of loop values depending on calculations on the T1, G1 values, and since the same amount of work is not being done, vectorization becomes tricky and even non-productive (slower and less clear and uses more memory.)
If you could redefine your hA and IA so that the same amount of work was done for all of the T1, G1 pairs, even if that meant taking the finest step size implied by the set of pairs and using that for everything, then vectorization would be more practical.
But you should always treat vectorization as an optimization, with your most important goal being to Get It Working First. And if that means following the steps I gave above to create a looped version, do that. The looped version might be fast enough. And if it isn't, you'll have a working reference version to compare against as you develop the vectorized version.
  1 Comment
Kip
Kip on 26 Jun 2012
Thank you for your explanation and your time,
I'm not very clear with the call function that you stated, I've tried it, but it always came out with errors, must be I got it wrong, sorry,, but I've also tried your idea on how work with multiple T and G at the same time. I've used the command :importfile(.xls) and import the data from separate excel spreadsheet for each variable (I don't know how to set multiple variables by using data from the same spreadsheet, how to call from specific sheet and column, could you please teach me this?)
And I separated most of the formula into a much simpler command, so that it can calculated column by column. Everything works okay, all came out exactly as how many rows I put in, if the data has 100 rows in the spreadsheet(Tn = 100 or Gn = 100), then it will calculate all 100 rows at the same time. But now I'm stuck at the line 83, To find the range of I with an increment of I (IA), it turned out with nothing, it supposed to create 1001 columns, as I need a range of 1001 for each IL with an even increment, (If got 100 T and 100 G, then IL also will got 100 unit), could you please help me on this? I've tried many things for 2 days and came out with nothing,, and also could you please check other commands if maybe got something missing or if it need further improvement.
Here's the code;
clc
%-----INPUT----
Time=importdata('D:\Data\Time.xls');
T=importdata('D:\Data\Temp.xls');
G=importdata('D:\Data\Solar.xls');
%-----CONSTANT-----
VOC=64.2; %open circuit voltage (V)
ISC=5.96; %short circuit current (A)
VR=54.7; %reference voltage for maximum power
IR=5.58; %reference current for maximum power
TR=298; %reference temperature (K)
GR=1000; %reference irradiance (W/m^2)
NS=96; %number of cells per module
AM0=1.631; %area of one module (m^2)
mi=0.0035; %temperature coefficient for short circuit current (A/K)
% VOC=44.8; %open circuit voltage (V)
% ISC=8.33; %short circuit current (A)
% VR=35.2; %reference voltage for maximum power
% IR=7.95; %reference current for maximum power
% TR=298; %reference temperature (K)
% GR=1000; %reference irradiance (W/m^2)
% NS=72; %number of cells per module
% AM0=1.94; %area of one module (m^2)
% mi=0.00045; %temperature coefficient for short circuit current (A/K)
%-----CELL PARAMETERS-----
EG=1.124; %energy gap (eV)
A=1.3; %ideality factor
k=1.38*10^-23; %Boltzmann's constant (J/K)
q=1.6*10^-19; %charge of electron (C)
%-----CONVERT FROM CELCIUS TO KELVIN-----
K=T+273;
%-----CALCULATION OF V BY VARYING I FOR A SINGLE MODULE-----
%-----CALCULATION OF Light Generated Current-----
KX=K-TR;
GX=G/GR;
ILX=ISC+mi;
ILX1=ILX*KX;
IL=ILX1.*GX; %light generated current
%-----CALCULATION OF Saturation Current-----
IRS1=ISC/(exp((VOC*q)/(NS*A*k*TR))-1); %reference saturation current
KRS=K/TR;
KRS3=KRS.^3;
IS1=IRS1*KRS3;
IS2=(q*EG)/(A*k);
IS3=(1/TR)-(1./K);
IS4=IS2*IS3;
IS5=exp(IS4);
IS=IS1.*IS5; %saturation current
RS1=((NS*A*k*TR/q)*log(((ISC-IR)/IRS1)+1)-VR)/IR; %series resistance
h=IL/1000; %setting increment
IA=0:h1:IL; %range of I with increment h
N1=length(IA); %number of data points
VA(N1)=0; %setting final value of V
for i=1:(N1-1)
VA(i)=(NS*A*k*K/q)*log(1+(IL-IA(i))/IS1)-IA(i)*RS1; %computing V
end
%
% -----DETERMINATION OF OUTPUT BASED ON NUMBER OF MODULES-----
if MS1==0 %determining number of modules connected in series
MSA=1; %single module
else
MSA=MS1; %multiple modules connected in series
end
if MP1==0 %determining number of modules connected in parallel
MPA=1; %single module
else
MPA=MP1; %multiple modules connected in parallel
end
V1=MSA*VA; %new voltage limit
I1=MPA*IA; %new current limit
P1(N1)=0; %setting final value of P
for i=1:(N1-1)
P1(i)=I1(i)*V1(i); %computing P
end
%-----MAXIMUM POINT DETERMINATION-----
[PMAX1,i]=max(P1); %maximum P
VMAX1=V1(i); %V at maximum P
IMAX1=I1(i); %I at maximum P
%-----FILL FACTOR DETERMINATION-----
FF1=(VMAX1*IMAX1)/(V1(1)*I1(N1));
%-----EFFICIENCY DETERMINATION-----
AM1=MSA*MPA*AM0; %area based on number of modules
n1=(PMAX1/(AM1*GR))*100;
%-----I-V CHARACTERISTIC PLOT-----
figure(1)
plot(V1,I1), xlabel('Voltage (V)'), ylabel('Current (A)');
%
% %-----POWER AGAINST VOLTAGE PLOT-----
figure(2)
plot(V1,P1), xlabel('Voltage (V)'), ylabel('Power (W)');
% %-----PMAX AGAINST TIME PLOT-----
figure(2)
plot(PMAX1,Time), xlabel('PMax'), ylabel('Time');
Thank you so much..

Sign in to comment.


Kip
Kip on 26 Jun 2012
Sorry, again, here's the codes,
clc
%-----INPUT----
Time=importdata('D:\Data\Time.xls');
T=importdata('D:\Data\Temp.xls');
G=importdata('D:\Data\Solar.xls');
%-----CONSTANT-----
VOC=64.2; %open circuit voltage (V)
ISC=5.96; %short circuit current (A)
VR=54.7; %reference voltage for maximum power
IR=5.58; %reference current for maximum power
TR=298; %reference temperature (K)
GR=1000; %reference irradiance (W/m^2)
NS=96; %number of cells per module
AM0=1.631; %area of one module (m^2)
mi=0.0035; %temperature coefficient for short circuit current (A/K)
% VOC=44.8; %open circuit voltage (V)
% ISC=8.33; %short circuit current (A)
% VR=35.2; %reference voltage for maximum power
% IR=7.95; %reference current for maximum power
% TR=298; %reference temperature (K)
% GR=1000; %reference irradiance (W/m^2)
% NS=72; %number of cells per module
% AM0=1.94; %area of one module (m^2)
% mi=0.00045; %temperature coefficient for short circuit current (A/K)
%-----CELL PARAMETERS-----
EG=1.124; %energy gap (eV)
A=1.3; %ideality factor
k=1.38*10^-23; %Boltzmann's constant (J/K)
q=1.6*10^-19; %charge of electron (C)
%-----CONVERT FROM CELCIUS TO KELVIN-----
K=T+273;
%-----CALCULATION OF V BY VARYING I FOR A SINGLE MODULE-----
%-----CALCULATION OF Light Generated Current-----
KX=K-TR;
GX=G/GR;
ILX=ISC+mi;
ILX1=ILX*KX;
IL=ILX1.*GX; %light generated current
%-----CALCULATION OF Saturation Current-----
IRS1=ISC/(exp((VOC*q)/(NS*A*k*TR))-1); %reference saturation current
KRS=K/TR;
KRS3=KRS.^3;
IS1=IRS1*KRS3;
IS2=(q*EG)/(A*k);
IS3=(1/TR)-(1./K);
IS4=IS2*IS3;
IS5=exp(IS4);
IS=IS1.*IS5; %saturation current
RS1=((NS*A*k*TR/q)*log(((ISC-IR)/IRS1)+1)-VR)/IR; %series resistance
h=IL/1000; %setting increment
IA=0:h1:IL; %range of I with increment h
N1=length(IA); %number of data points
VA(N1)=0; %setting final value of V
for i=1:(N1-1)
VA(i)=(NS*A*k*K/q)*log(1+(IL-IA(i))/IS1)-IA(i)*RS1; %computing V
end
%
% -----DETERMINATION OF OUTPUT BASED ON NUMBER OF MODULES-----
if MS1==0 %determining number of modules connected in series
MSA=1; %single module
else
MSA=MS1; %multiple modules connected in series
end
if MP1==0 %determining number of modules connected in parallel
MPA=1; %single module
else
MPA=MP1; %multiple modules connected in parallel
end
V1=MSA*VA; %new voltage limit
I1=MPA*IA; %new current limit
P1(N1)=0; %setting final value of P
for i=1:(N1-1)
P1(i)=I1(i)*V1(i); %computing P
end
%-----MAXIMUM POINT DETERMINATION-----
[PMAX1,i]=max(P1); %maximum P
VMAX1=V1(i); %V at maximum P
IMAX1=I1(i); %I at maximum P
%-----FILL FACTOR DETERMINATION-----
FF1=(VMAX1*IMAX1)/(V1(1)*I1(N1));
%-----EFFICIENCY DETERMINATION-----
AM1=MSA*MPA*AM0; %area based on number of modules
n1=(PMAX1/(AM1*GR))*100;
%-----I-V CHARACTERISTIC PLOT-----
figure(1)
plot(V1,I1), xlabel('Voltage (V)'), ylabel('Current (A)');
%
% %-----POWER AGAINST VOLTAGE PLOT-----
figure(2)
plot(V1,P1), xlabel('Voltage (V)'), ylabel('Power (W)');
% %-----PMAX AGAINST TIME PLOT-----
figure(2)
plot(PMAX1,Time), xlabel('PMax'), ylabel('Time');
Thanks for your help.

Products

Community Treasure Hunt

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

Start Hunting!