I developed some code for my masters project that will simulate 90 years daily data using 1000 different data sets. The code is working fine and gives the correct output that I wanted but the processing time are very high. It took about 8 hours to finish the simulation.
tic
%% importing the csv file with selected column
files=dir('*_scen_*.csv');
for i=1:length(files);
LHR=importcsv(files(i).name);
%% Definable variables
% Define These Value
TAW=-216; %total available water
RAW=-129; %readily available water
KC=1.0; %crop coefficient
IRL=15; %intense rain level
RC=(80/100); %percentage of recharge
RO=(1-RC); %percentage of runoff
% The very first row of Soil Moisture Deficit
for j=1
SMD(j,i)=(LHR.RAIN(j)-LHR.PET(j));
if SMD(j,i)>0;
SMD(j,i)=0;
elseif SMD(j,i)<RAW;
SMD(j,i)=(LHR.RAIN(j)-(LHR.PET(j)*((TAW-SMD(j-1))/(TAW-RAW))));
end
end
%for the following SMD Calculation
for k=2:(length(LHR.RAIN));
SMD(k,i)=SMD(k-1,i)+(LHR.RAIN(k)-LHR.PET(k));
% The SMD conditions
if SMD(k,i)>0;
SMD(k,i)=0;
elseif SMD(k,i)<RAW;
SMD(k,i)=SMD(k-1,i)+(LHR.RAIN(k)-(LHR.PET(k)*((TAW-SMD(k-1,i))/(TAW-RAW))));
end
end
%Convert negative SMD to Positive
SMD=abs(SMD);
%%Evapotranspiration Calculation
for l=1:(length(SMD));
if SMD(l,i)<abs(RAW);
AET(l,i)=LHR.PET(l);
elseif SMD(l,i)>abs(RAW);
AET(l,i)=KC*LHR.PET(l)*((abs(TAW)-(SMD(l,i)))/(abs(TAW)-abs(RAW)));
end
end
for m=2:(length(SMD));
if SMD(m,i)<abs(RAW);
AET(m,i)=LHR.PET(m);
elseif SMD(m,i)>abs(RAW);
AET(m,i)=KC*LHR.PET(m)*((abs(TAW)-(SMD(m-1,i)))/(abs(TAW)-abs(RAW)));
end
end
%% HER calculation
for n=1:length(SMD);
if SMD(n,i)<(LHR.RAIN(n)-AET(n,i));
HER(n,i)=(LHR.RAIN(n)-AET(n,i)-SMD(n,i));
elseif SMD(n,i)>(LHR.RAIN(n)-AET(n,i));
HER(n,i)=0;
end
end
%% Calculation of recharge anf runoff
for o=1:(length(HER));
if (HER(o,i)+(abs(TAW)-SMD(o,i)))<abs(TAW);
RUNOFF(o,i)=0;
elseif (HER(o,i)+(abs(TAW)-SMD(o,i)))>abs(TAW);
if HER(o,i)>IRL;
RUNOFF(o,i)=RO*HER(o,i);
elseif HER(o,i)<IRL;
RUNOFF(o,i)=0;
end
end
if (HER(o,i)+(abs(TAW)-SMD(o,i)))<abs(TAW);
RECHARGE(o,i)=0;
elseif (HER(o,i)+(abs(TAW)-SMD(o,i)))>abs(TAW);
if HER(o,i)>IRL;
RECHARGE(o,i)=RC*HER(o,i);
elseif HER(o,i)<IRL;
RECHARGE(o,i)=HER(o,i);
end
end
end
%% rainfall
for p=1:length(LHR.RAIN);
RAINFALL(p,i)=LHR.RAIN(p);
PET(p,i)=LHR.PET(p);
end
end
clear i
clear j
clear k
clear l
clear m
clear n
clear o
clear p
toc
Is there any improvement scope for this code that might reduce the processing time? Sorry if the code looks unprofessional; I am in the beginner stage for MATLAB programming.