Kindly please help me with the problem as I need to use nlinfit function for fitting unknown parameters but it is showing some error. Although yesterday I was getting some values for parameters to be fitted but now if I am running it is having some issue for the function output to be used in fitted with NaN answer for last iteration only. X data is a concatenated matrix of three columns as independent variable and yk is dependent variable, taua is a matrix of initial guesses of number of parameters to be fitted.
function [yk]=activity_coefficientE(taua,x)
T=523;
alpha12=0.3; alpha13=0.3; alpha21=0.3; alpha23=0.3; alpha31=0.3; alpha32=0.3;
alpha18=0.2; alpha81=0.2; alpha28=0.2; alpha82=0.2; alpha38=0.2; alpha83=0.3;
alpha19=0.2; alpha91=0.2; alpha29=0.2; alpha92=0.2; alpha39=0.2; alpha93=0.2;
alpha110=0.2;alpha101=0.2;alpha210=0.2;alpha102=0.2;alpha310=0.2;alpha103=0.2;
alpha113=0.2;alpha131=0.2;alpha213=0.2;alpha132=0.2;alpha313=0.2;alpha133=0.2;
alpha114=0.2;alpha141=0.2;alpha214=0.2;alpha142=0.2;alpha314=0.2;alpha143=0.2;
alpha115=0.2;alpha151=0.2;alpha215=0.2;alpha152=0.2;alpha315=0.2;alpha153=0.2;
alpha117=0.2;alpha171=0.2;alpha217=0.2;alpha172=0.2;alpha317=0.2;alpha173=0.2;
alpha118=0.2;alpha181=0.2;alpha218=0.2;alpha182=0.2;alpha318=0.2;alpha183=0.2;
alpha810=0.2;alpha915=0.2;alpha1314=0.2;alpha108=0.2;alpha159=0.2;alpha1413=0.2;
alpha1718=0.2;alpha1817=0.2;
tau12=0; tau13=0; tau21=0; tau23=0; tau31=0; tau32=0;
%taua=randi([-5,5],1,112)
tau18=taua(1)+taua(57)/T;
tau81=taua(2)+taua(58)/T;
tau28=taua(3)+taua(59)/T;
tau82=taua(4)+taua(60)/T;
tau38=taua(5)+taua(61)/T;
tau83=taua(6)+taua(62)/T;
tau19=taua(7)+taua(63)/T;
tau91=taua(8)+taua(64)/T;
tau29=taua(9)+taua(65)/T;
tau92=taua(10)+taua(66)/T;
tau39=taua(11)+taua(67)/T;
tau93=taua(12)+taua(68)/T;
tau110=taua(13)+taua(69)/T;
tau101=taua(14)+taua(70)/T;
tau210=taua(15)+taua(71)/T;
tau102=taua(16)+taua(72)/T;
tau310=taua(17)+taua(73)/T;
tau103=taua(18)+taua(74)/T;
tau113=taua(19)+taua(75)/T;
tau131=taua(20)+taua(76)/T;
tau213=taua(21)+taua(77)/T;
tau132=taua(22)+taua(78)/T;
tau313=taua(23)+taua(79)/T;
tau133=taua(24)+taua(80)/T;
tau114=taua(25)+taua(81)/T;
tau141=taua(26)+taua(82)/T;
tau214=taua(27)+taua(83)/T;
tau142=taua(28)+taua(84)/T;
tau314=taua(29)+taua(85)/T;
tau143=taua(30)+taua(86)/T;
tau115=taua(31)+taua(87)/T;
tau151=taua(32)+taua(88)/T;
tau215=taua(33)+taua(89)/T;
tau152=taua(34)+taua(90)/T;
tau315=taua(35)+taua(91)/T;
tau153=taua(36)+taua(92)/T;
tau117=taua(37)+taua(93)/T;
tau171=taua(38)+taua(94)/T;
tau217=taua(39)+taua(95)/T;
tau172=taua(40)+taua(96)/T;
tau317=taua(41)+taua(97)/T;
tau173=taua(42)+taua(98)/T;
tau118=taua(43)+taua(99)/T;
tau181=taua(44)+taua(100)/T;
tau218=taua(45)+taua(101)/T;
tau182=taua(46)+taua(102)/T;
tau318=taua(47)+taua(103)/T;
tau183=taua(48)+taua(104)/T;
tau810=taua(49)+taua(105)/T;
tau108=taua(50)+taua(106)/T;
tau915=taua(51)+taua(107)/T;
tau159=taua(52)+taua(108)/T;
tau1314=taua(53)+taua(109)/T;
tau1413=taua(54)+taua(110)/T;
tau1718=taua(55)+taua(111)/T;
tau1817=taua(56)+taua(112)/T;
G12=exp(-(tau12*alpha12));
G21=exp(-(tau21*alpha21));
G13=exp(-(tau13*alpha13));
G31=exp(-(tau31*alpha31));
G23=exp(-(tau23*alpha23));
G32=exp(-(tau32*alpha32));
G18=exp(-(tau18*alpha18));
G81=exp(-(tau81*alpha81));
G28=exp(-(tau28*alpha28));
G82=exp(-(tau82*alpha82));
G38=exp(-(tau38*alpha83));
G83=exp(-(tau83*alpha83));
G19=exp(-(tau19*alpha19));
G91=exp(-(tau91*alpha91));
G29=exp(-(tau29*alpha29));
G92=exp(-(tau92*alpha92));
G39=exp(-(tau39*alpha39));
G93=exp(-(tau93*alpha93));
G110=exp(-(tau110*alpha110));
G101=exp(-(tau101*alpha101));
G210=exp(-(tau210*alpha210));
G102=exp(-(tau102*alpha102));
G310=exp(-(tau310*alpha310));
G103=exp(-(tau103*alpha103));
G113=exp(-(tau113*alpha113));
G131=exp(-(tau131*alpha131));
G213=exp(-(tau213*alpha213));
G132=exp(-(tau132*alpha132));
G313=exp(-(tau313*alpha313));
G133=exp(-(tau133*alpha133));
G114=exp(-(tau114*alpha114));
G141=exp(-(tau141*alpha141));
G214=exp(-(tau214*alpha214));
G142=exp(-(tau142*alpha142));
G314=exp(-(tau314*alpha314));
G143=exp(-(tau143*alpha143));
G115=exp(-(tau115*alpha115));
G151=exp(-(tau151*alpha151));
G215=exp(-(tau215*alpha215));
G152=exp(-(tau152*alpha152));
G315=exp(-(tau315*alpha315));
G153=exp(-(tau153*alpha153));
G117=exp(-(tau117*alpha117));
G171=exp(-(tau171*alpha171));
G217=exp(-(tau217*alpha217));
G172=exp(-(tau172*alpha172));
G317=exp(-(tau317*alpha317));
G173=exp(-(tau173*alpha173));
G118=exp(-(tau118*alpha118));
G181=exp(-(tau181*alpha181));
G218=exp(-(tau218*alpha218));
G182=exp(-(tau182*alpha182));
G318=exp(-(tau318*alpha318));
G183=exp(-(tau183*alpha183));
G810=exp(-(tau810*alpha810));
G108=exp(-(tau108*alpha108));
G915=exp(-(tau915*alpha915));
G159=exp(-(tau159*alpha159));
G1314=exp(-(tau1314*alpha1314));
G1413=exp(-(tau1413*alpha1413));
G1718=exp(-(tau1718*alpha1718));
G1817=exp(-(tau1817*alpha1817));
%calculating mole fractions of ionic species
x1=x(:,1);
x2=x(:,2);
x3=x(:,3);
%x1=[0.1577 0.1492 0.1462 0.1366 0.1299 0.1180 0.0863 0.0761 0.0550 ];
%x2=[0.8278 0.7945 0.7678 0.7450 0.6979 0.6309 0.4611 0.4114 0.2952 ];
%x3=[0.0145 0.0563 0.0860 0.1184 0.1722 0.2511 0.4526 0.5125 0.6498 ];
A=[0.0674243 0.0773881 0.0843400 0.0865343 0.0899223 0.0882858 0.0715087 0.0643867 0.0483658];
B=[0.0141081 0.0479814 0.0643151 0.0737477 0.0820756 0.0838701 0.0701576 0.0634457 0.0479639];
C=[0.0565665 0.0450072 0.0387724 0.0313828 0.02506094 0.0186280 0.0092734 0.0073438 0.0041595 ];
D=[0.0336447 0.0267694 0.0230611 0.0186659 0.0149058 0.0110795 0.0055157 0.0043679 0.0024739 ];
E=[0.0008148 0.0008756 0.00087131 0.0008794 0.0008711 0.0008441 0.0007384 0.0006997 0.0005980 ];
N=length(A);
x1n=zeros(N,1);x2n=zeros(N,1);x3n=zeros(N,1);
X1=zeros(N,1);X2=zeros(N,1);X3=zeros(N,1);X4=zeros(N,1);X5=zeros(N,1);X6=zeros(N,1);X7=zeros(N,1);
X12=zeros(N,1);X16=zeros(N,1);
for i=1:N
x1n(i)=(x1(i)-A(i)-D(i)-2*E(i)-C(i)+3*B(i))
x2n(i)=(x2(i)-A(i)-C(i)-D(i))
x3n(i)=(x3(i)-B(i))
X1(i)=(x1n(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X2(i)=(x2n(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X3(i)=(x3n(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X4(i)=(A(i)+D(i)+E(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X5(i)=(C(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X6(i)=(A(i)-B(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X7(i)=(B(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X12(i)=(E(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
X16(i)=(C(i)+D(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)))
end
yc4=X4./(X4+X5);
yc5=X5./(X4+X5);
yc6=X6./(X6+X7+X12+X16);
yc7=X7./(X6+X7+X12+X16);
yc12=X12./(X6+X7+X12+X16);
yc16=X16./(X6+X7+X12+X16);
alpha14=yc6.*alpha18+yc7.*alpha19+yc12.*alpha113+yc16.*alpha117;
%alpha41=alpha14;
alpha24=yc6.*alpha28+yc7.*alpha29+yc12.*alpha213+yc16.*alpha217;
%alpha42=alpha24;
alpha34=yc6.*alpha38+yc7.*alpha39+yc12.*alpha313+yc16.*alpha317;
%alpha43=alpha34;
alpha15=yc6.*alpha110+yc7.*alpha115+yc12.*alpha114+yc16.*alpha118;
%alpha51=alpha15;
alpha25=yc6.*alpha210+yc7.*alpha215+yc12.*alpha214+yc16.*alpha218;
%alpha52=alpha25;
alpha35=yc6.*alpha310+yc7.*alpha315+yc12.*alpha314+yc16.*alpha318;
%alpha53=alpha35;
alpha16=yc4.*alpha81+yc5.*alpha101;
%alpha61=alpha16;
alpha26=yc4.*alpha82+yc5.*alpha102;
%alpha62=alpha26;
alpha36=yc4.*alpha83+yc5.*alpha103;
%alpha63=alpha36;
alpha17=yc4.*alpha91+yc5.*alpha151;
%alpha71=alpha17;
alpha27=yc4.*alpha92+yc5.*alpha152;
%alpha72=alpha27;
alpha37=yc4.*alpha93+yc5.*alpha153;
%alpha73=alpha37;
alpha112=yc4.*alpha131+yc5.*alpha141;
%alpha121=alpha112;
alpha212=yc4.*alpha132+yc5.*alpha142;
%alpha122=alpha212;
alpha312=yc4.*alpha133+yc5.*alpha143;
%alpha123=alpha312;
alpha116=yc4.*alpha171+yc5.*alpha181;
%alpha161=alpha116;
alpha216=yc4.*alpha172+yc5.*alpha182;
%alpha162=alpha216;
alpha316=yc4.*alpha173+yc5.*alpha183;
%alpha163=alpha316;
alpha46=yc5.*alpha810;
%alpha64=alpha46;
alpha47=yc5.*alpha915;
%alpha74=alpha47;
alpha412=yc5.*alpha1314;
%alpha124=alpha412;
alpha416=yc5.*alpha1718;
%alpha164=alpha416;
alpha56=yc4.*alpha108;
%alpha65=alpha56;
alpha57=yc4.*alpha159;
%alpha75=alpha57;
alpha512=yc4.*alpha1413;
%alpha125=alpha512;
alpha516=yc4.*alpha1817;
%alpha165=alpha516;
G14=yc6.*G18+yc7.*G19+yc12.*G113+yc16.*G117;
%G41=G14;
G24=yc6.*G28+yc7.*G29+yc12.*G213+yc16.*G217;
%G42=G24;
G34=yc6.*G38+yc7.*G39+yc12.*G313+yc16.*G317;
%G43=G34;
G15=yc6.*G110+yc7.*G115+yc12.*G114+yc16.*G118;
%G51=G15;
G25=yc6.*G210+yc7.*G215+yc12.*G214+yc16.*G218;
%G52=G25;
G35=yc6.*G310+yc7.*G315+yc12.*G314+yc16.*G318;
%G53=G35;
G16=yc4.*G81+yc5.*G101;
%G61=G16;
G26=yc4.*G82+yc5.*G102;
%G62=G26;
G36=yc4.*G83+yc5.*G103;
%G63=G36;
G17=yc4.*G91+yc5.*G151;
%G71=G17;
G27=yc4.*G92+yc5.*G152;
%G72=G27;
G37=yc4.*G93+yc5.*G153;
%G73=G37;
G112=yc4.*G131+yc5.*G141;
%G121=G112;
G212=yc4.*G132+yc5.*G142;
%G122=G212;
G312=yc4.*G133+yc5.*G143;
%G123=G312;
G116=yc4.*G171+yc5.*G181;
%G161=G116;
G216=yc4.*G172+yc5.*G182;
%G162=G216;
G316=yc4.*G173+yc5.*G183;
%G163=G316;
G46=yc5.*G810;
%G64=G46;
G47=yc5.*G915;
%G74=G47;
G412=yc5.*G1314;
%G124=G412;
G416=yc5.*G1718;
%G164=G416;
G56=yc4.*G108;
%G65=G56;
G57=yc4.*G159;
%G75=G57;
G512=yc4.*G1413;
%G125=G512;
G516=yc4.*G1817;
%G165=G516;
tau14=-log(G14)./alpha14;
%tau41=tau14;
tau24=-log(G24)./alpha24;
%tau42=tau24;
tau34=-log(G34)./alpha34;
%tau43=tau34;
tau15=-log(G15)./alpha15;
%tau51=tau15;
tau25=-log(G25)./alpha25;
%tau52=tau25;
tau35=-log(G35)./alpha35;
%tau53=tau35;
tau16=-log(G16)./alpha16;
%tau61=tau16;
tau26=-log(G26)./alpha26;
%tau62=tau26;
tau36=-log(G36)./alpha36;
%tau63=tau36;
tau17=-log(G17)./alpha17;
%tau71=tau17;
tau27=-log(G27)./alpha27;
%tau72=tau27;
tau37=-log(G37)./alpha37;
%tau73=tau37;
tau112=-log(G112)./alpha112;
%tau121=tau112;
tau212=-log(G212)./alpha212;
%tau122=tau212;
tau312=-log(G312)./alpha312;
%tau123=tau312;
tau116=-log(G116)./alpha116;
%tau161=tau116;
tau216=-log(G216)./alpha216;
%tau162=tau216;
tau316=-log(G316)./alpha316;
%tau163=tau316;
tau46=-log(G46)./alpha46;
%tau64=tau46;
tau47=-log(G47)./alpha47;
%tau74=tau47;
tau412=-log(G412)./alpha412;
%tau124=tau412;
tau416=-log(G416)./alpha416;
%tau164=tau416;
tau56=-log(G56)./alpha56;
%tau65=tau56;
tau57=-log(G57)./alpha57;
%tau75=tau57;
tau512=-log(G512)./alpha512;
%tau125=tau512;
tau516=-log(G516)./alpha516;
%tau165=tau516;
ln_y1_1=G12.*X2.*tau12+ G31.*X3.*tau13+ G14.*X4.*tau14+G15.*X5.*tau15+G16.*X6.*tau16+G17.*X7.*tau17+G112.*X12.*tau112+G116.*X16.*tau116;
ln_y1_2=G12.*X2+ G13.*X3+ G14.*X4+G15.*X5+G16.*X6+G17.*X7+G112.*X12+G116.*X16;
ln_y2_1=G21.*X1.*tau12+ G32.*X3.*tau32+ G24.*X4.*tau24+G25.*X5.*tau25+G26.*X6.*tau26+G27.*X7.*tau27+G212.*X12.*tau212+G216.*X16.*tau216;
ln_y2_2=G12.*X1+ G23.*X3+G24.*X4+G25.*X5+G26.*X6+G27.*X7+G212.*X12+G216.*X16;
ln_y3_1=G13.*X1.*tau13+ G23.*X3.*tau23+ G34.*X4.*tau34+G35.*X5.*tau35+G36.*X6.*tau36+G37.*X7.*tau37+G312.*X12.*tau312+G316.*X16.*tau316;
ln_y3_2=G13.*X1+ G23.*X3+ G34.*X4+G35.*X5+G36.*X6+G37.*X7+G312.*X12+G316.*X16;
ln_y4_1=G14.*X1.*tau14+G24.*X2.*tau24+G34.*X3.*tau34+G46.*X6.*tau46+G47.*X7.*tau47+G412.*X12.*tau412+G416.*X16.*tau416;
ln_y4_2=G14.*X1+G24.*X2+G34.*X3+G46.*X6+G47.*X7+G412.*X12+G416.*X16;
ln_y5_1=G15.*X1.*tau15+G25.*X2.*tau25+G35.*X3.*tau35+G56.*X6.*tau56+G57.*X7.*tau57+G512.*X12.*tau512+G516.*X16.*tau516;
ln_y5_2=G15.*X1+G25.*X2+G35.*X3+G56.*X6+G57.*X7+G512.*X12+G516.*X16;
ln_y6_1=G16.*X1.*tau16+G26.*X2.*tau26+G36.*X3.*tau36+G46.*X4.*tau46+G56.*X5.*tau56;
ln_y6_2=G16.*X1+G26.*X2+G36.*X3+G46.*X4+G56.*X5;
ln_y7_1=G17.*X1.*tau17+G27.*X2.*tau27+G37.*X3.*tau37+G47.*X4.*tau47+G57.*X5.*tau57;
ln_y7_2=G17.*X1+G27.*X2+G37.*X3+G47.*X4+G57.*X5;
ln_y12_1=G112.*X1.*tau112+G212.*X2.*tau212+G312.*X3.*tau312+G412.*X4.*tau412+G512.*X5.*tau512;
ln_y12_2=G112.*X1+G212.*X2+G312.*X3+G412.*X4+G512.*X5;
ln_y16_1=G116.*X1.*tau116+G216.*X2.*tau216+G316.*X3.*tau316+G416.*X4.*tau416+G516.*X5.*tau516;
ln_y16_2=G116.*X1+G216.*X2+G316.*X3+G416.*X4+G516.*X5;
ln_y1_3=(((X2.*G12)./ln_y2_2).*(tau12-(ln_y2_1)./(ln_y2_2)))+(((X3.*G13)./ln_y3_2).*(tau13-(ln_y3_1)./(ln_y3_2)));
ln_y1_4=(((X6.*G16)./ln_y6_2).*(tau16- (ln_y6_1./ln_y6_2))) + (((X7.*G17)./ln_y7_2).*(tau17- (ln_y7_1./ln_y7_2)))+(((X12.*G12)./ln_y12_2).*(tau112- (ln_y12_1./ln_y12_2)))+(((X16.*G16)./ln_y16_2).*(tau116- (ln_y16_1./ln_y16_2)));
ln_y1_5=(((X4.*G14)./ln_y4_2).*(tau14- (ln_y4_1./ln_y4_2))) + (((X5.*G15)./ln_y5_2).*(tau15- (ln_y5_1./ln_y5_2)));
yk=exp((ln_y1_1./ln_y1_2) + ln_y1_3 + ln_y1_4+ ln_y1_5) % activity coefficient for H2O
end
........................................
Another function where above function to be called.....
% calling the function act_coeff to estimate the binary interaction parameters
for i=1:112
filename = 'EagelsDATA.xlsx'; %reading VLE data from excel file
Data = xlsread(filename);
x(:,1) = Data([10:15 17:19],16);
x(:,2) = Data([10:15 17:19],1);
x(:,3)= Data([10:15 17:19],2);
taua=(randi([-5,5],1,112));
yk=[0.0606 (values calculated from above function and will be used for fitting)
0.4327
0.6545
0.9417
1.2570
1.6881
1.9108
1.7777
1.3821]
% taua =[ -2 3 4 -3 -4 1 4 -2 4 -4 -1 4 5 -3 3 2 -5 3 -4
% 1 4 1 5 -1 -1 -3 2 -3 4 3 4 2 5 4 -2 4 3 -1
% 1 0 -5 -5 -5 -3 4 2 1 4 0 2 -3 -4 5 0 -3 2 5
% 1 0 5 1 -3 5 4 1 5 2 3 2 0 -5 -4 -2 1 -2 5
%-5 5 -2 -2 4 1 -1 3 -1 1 5 -1 0 -1 4 5 5 1 4
% 1 0 4 -4 4 0 -1 -2 -5 -3 -4 -5
% -5 0 -2 0 -5] (random values for which yk was calculted from the command
taua= randi([-5,5],1,112))
try % try-catch used to continue the loop without stopping on encountering an error
[taua1]= nlinfit(x,yk,#activity_coefficientE,taua)
catch exception
continue
end
end
I am not able to attach excel sheet here so data from excel sheet is as:
x =[0.1577 0.1492 0.1462 0.1366 0.1299 0.1180 0.0863 0.0761 0.0550; column 1
0.8278 0.7945 0.7678 0.7450 0.6979 0.6309 0.4611 0.4114 0.2952 ; column 2
0.0145 0.0563 0.0860 0.1184 0.1722 0.2511 0.4526 0.5125 0.6498 ]; column 3
I found 3 major problems with what you did.
Problem #1 - errors
The reason you get the error is because your function "activity_coefficientE" can sometimes return NaN or inf values. My suggestion is to look for these values and set the value of "yk" to a large value so that the optimizer in "nlinfit" will stay away from coefficients that produce infinite or NaN values. This is the code at the bottom of the function so that you avoid crashes:
if any(~isfinite(yk))
yk = 10 * ones(size(yk));
end
Problem #2 - random initial guesses
The trouble with using random numbers for your initial conditions is that every time you run it you get a different answer, so sometimes it works and sometimes it doesn't. If you set the random number generator seed, you can get the same random numbers each time you run the script. If you change you seed, you can get a different set of random numbers. I shortened your main script to this, where I try 100 different random seeds (and store the results of each attempt) to see what answers result:
for i=1:100
rng(i)
taua = randi([-5,5],1,112);
taua1(i, :) = nlinfit(x,yk,#activity_coefficientE,taua);
end
Each row of "taua1" is a set of 111 coefficients.
Problem #3 - Trying to fit 9 points with 112 coefficients
Every time nlinfit is called, you get this warning:
Warning: Rank deficient
because you have more coefficients (112) that you are asking nlinfit to find than data points you are fitting (9). It's like trying to find the 2nd order equation that best fits 2 points, there are an infinite number of solutions. When curve fitting you should have more data points than coefficients to make sure you're not fitting noise. You need more data points in "yk" and "x" and/or fewer coefficients to fit. I've done a lot of curve fitting and I've never seen an equation with 112 coefficients, so I am thinking that you are not solving the problem correctly. Perhaps the 112 coefficients aren't really independent or there are 112 data points and 9 coefficients that you want to find.
For completeness, here is my edited version of the activity_coefficientE.m function that I created to work on this solution. In general, I never see Matlab code with this many variables with similar names. Much of this code could be greatly simplified by using vector operations. Most of my changes involve formatting, adding semicolons, and the checks for non-finite values at the end.
function yk=activity_coefficientE(taua,x)
T=523;
alpha12=0.3; alpha13=0.3; alpha21=0.3; alpha23=0.3; alpha31=0.3; alpha32=0.3;
alpha18=0.2; alpha81=0.2; alpha28=0.2; alpha82=0.2; alpha38=0.2; alpha83=0.3;
alpha19=0.2; alpha91=0.2; alpha29=0.2; alpha92=0.2; alpha39=0.2; alpha93=0.2;
alpha110=0.2;alpha101=0.2;alpha210=0.2;alpha102=0.2;alpha310=0.2;alpha103=0.2;
alpha113=0.2;alpha131=0.2;alpha213=0.2;alpha132=0.2;alpha313=0.2;alpha133=0.2;
alpha114=0.2;alpha141=0.2;alpha214=0.2;alpha142=0.2;alpha314=0.2;alpha143=0.2;
alpha115=0.2;alpha151=0.2;alpha215=0.2;alpha152=0.2;alpha315=0.2;alpha153=0.2;
alpha117=0.2;alpha171=0.2;alpha217=0.2;alpha172=0.2;alpha317=0.2;alpha173=0.2;
alpha118=0.2;alpha181=0.2;alpha218=0.2;alpha182=0.2;alpha318=0.2;alpha183=0.2;
alpha810=0.2;alpha915=0.2;alpha1314=0.2;alpha108=0.2;alpha159=0.2;alpha1413=0.2;
alpha1718=0.2;alpha1817=0.2;
tau12=0; tau13=0; tau21=0; tau23=0; tau31=0; tau32=0;
tau18=taua(1)+taua(57)/T;
tau81=taua(2)+taua(58)/T;
tau28=taua(3)+taua(59)/T;
tau82=taua(4)+taua(60)/T;
tau38=taua(5)+taua(61)/T;
tau83=taua(6)+taua(62)/T;
tau19=taua(7)+taua(63)/T;
tau91=taua(8)+taua(64)/T;
tau29=taua(9)+taua(65)/T;
tau92=taua(10)+taua(66)/T;
tau39=taua(11)+taua(67)/T;
tau93=taua(12)+taua(68)/T;
tau110=taua(13)+taua(69)/T;
tau101=taua(14)+taua(70)/T;
tau210=taua(15)+taua(71)/T;
tau102=taua(16)+taua(72)/T;
tau310=taua(17)+taua(73)/T;
tau103=taua(18)+taua(74)/T;
tau113=taua(19)+taua(75)/T;
tau131=taua(20)+taua(76)/T;
tau213=taua(21)+taua(77)/T;
tau132=taua(22)+taua(78)/T;
tau313=taua(23)+taua(79)/T;
tau133=taua(24)+taua(80)/T;
tau114=taua(25)+taua(81)/T;
tau141=taua(26)+taua(82)/T;
tau214=taua(27)+taua(83)/T;
tau142=taua(28)+taua(84)/T;
tau314=taua(29)+taua(85)/T;
tau143=taua(30)+taua(86)/T;
tau115=taua(31)+taua(87)/T;
tau151=taua(32)+taua(88)/T;
tau215=taua(33)+taua(89)/T;
tau152=taua(34)+taua(90)/T;
tau315=taua(35)+taua(91)/T;
tau153=taua(36)+taua(92)/T;
tau117=taua(37)+taua(93)/T;
tau171=taua(38)+taua(94)/T;
tau217=taua(39)+taua(95)/T;
tau172=taua(40)+taua(96)/T;
tau317=taua(41)+taua(97)/T;
tau173=taua(42)+taua(98)/T;
tau118=taua(43)+taua(99)/T;
tau181=taua(44)+taua(100)/T;
tau218=taua(45)+taua(101)/T;
tau182=taua(46)+taua(102)/T;
tau318=taua(47)+taua(103)/T;
tau183=taua(48)+taua(104)/T;
tau810=taua(49)+taua(105)/T;
tau108=taua(50)+taua(106)/T;
tau915=taua(51)+taua(107)/T;
tau159=taua(52)+taua(108)/T;
tau1314=taua(53)+taua(109)/T;
tau1413=taua(54)+taua(110)/T;
tau1718=taua(55)+taua(111)/T;
tau1817=taua(56)+taua(112)/T;
G12=exp(-(tau12*alpha12));
G21=exp(-(tau21*alpha21));
G13=exp(-(tau13*alpha13));
G31=exp(-(tau31*alpha31));
G23=exp(-(tau23*alpha23));
G32=exp(-(tau32*alpha32));
G18=exp(-(tau18*alpha18));
G81=exp(-(tau81*alpha81));
G28=exp(-(tau28*alpha28));
G82=exp(-(tau82*alpha82));
G38=exp(-(tau38*alpha83));
G83=exp(-(tau83*alpha83));
G19=exp(-(tau19*alpha19));
G91=exp(-(tau91*alpha91));
G29=exp(-(tau29*alpha29));
G92=exp(-(tau92*alpha92));
G39=exp(-(tau39*alpha39));
G93=exp(-(tau93*alpha93));
G110=exp(-(tau110*alpha110));
G101=exp(-(tau101*alpha101));
G210=exp(-(tau210*alpha210));
G102=exp(-(tau102*alpha102));
G310=exp(-(tau310*alpha310));
G103=exp(-(tau103*alpha103));
G113=exp(-(tau113*alpha113));
G131=exp(-(tau131*alpha131));
G213=exp(-(tau213*alpha213));
G132=exp(-(tau132*alpha132));
G313=exp(-(tau313*alpha313));
G133=exp(-(tau133*alpha133));
G114=exp(-(tau114*alpha114));
G141=exp(-(tau141*alpha141));
G214=exp(-(tau214*alpha214));
G142=exp(-(tau142*alpha142));
G314=exp(-(tau314*alpha314));
G143=exp(-(tau143*alpha143));
G115=exp(-(tau115*alpha115));
G151=exp(-(tau151*alpha151));
G215=exp(-(tau215*alpha215));
G152=exp(-(tau152*alpha152));
G315=exp(-(tau315*alpha315));
G153=exp(-(tau153*alpha153));
G117=exp(-(tau117*alpha117));
G171=exp(-(tau171*alpha171));
G217=exp(-(tau217*alpha217));
G172=exp(-(tau172*alpha172));
G317=exp(-(tau317*alpha317));
G173=exp(-(tau173*alpha173));
G118=exp(-(tau118*alpha118));
G181=exp(-(tau181*alpha181));
G218=exp(-(tau218*alpha218));
G182=exp(-(tau182*alpha182));
G318=exp(-(tau318*alpha318));
G183=exp(-(tau183*alpha183));
G810=exp(-(tau810*alpha810));
G108=exp(-(tau108*alpha108));
G915=exp(-(tau915*alpha915));
G159=exp(-(tau159*alpha159));
G1314=exp(-(tau1314*alpha1314));
G1413=exp(-(tau1413*alpha1413));
G1718=exp(-(tau1718*alpha1718));
G1817=exp(-(tau1817*alpha1817));
%calculating mole fractions of ionic species
x1=x(:,1);
x2=x(:,2);
x3=x(:,3);
A=[0.0674243 0.0773881 0.0843400 0.0865343 0.0899223 0.0882858 0.0715087 0.0643867 0.0483658];
B=[0.0141081 0.0479814 0.0643151 0.0737477 0.0820756 0.0838701 0.0701576 0.0634457 0.0479639];
C=[0.0565665 0.0450072 0.0387724 0.0313828 0.02506094 0.0186280 0.0092734 0.0073438 0.0041595 ];
D=[0.0336447 0.0267694 0.0230611 0.0186659 0.0149058 0.0110795 0.0055157 0.0043679 0.0024739 ];
E=[0.0008148 0.0008756 0.00087131 0.0008794 0.0008711 0.0008441 0.0007384 0.0006997 0.0005980 ];
N=length(A);
x1n=zeros(N,1);x2n=zeros(N,1);x3n=zeros(N,1);
X1=zeros(N,1);X2=zeros(N,1);X3=zeros(N,1);X4=zeros(N,1);X5=zeros(N,1);X6=zeros(N,1);X7=zeros(N,1);
X12=zeros(N,1);X16=zeros(N,1);
for i=1:N
x1n(i)=(x1(i)-A(i)-D(i)-2*E(i)-C(i)+3*B(i));
x2n(i)=(x2(i)-A(i)-C(i)-D(i));
x3n(i)=(x3(i)-B(i));
X1(i)=(x1n(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X2(i)=(x2n(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X3(i)=(x3n(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X4(i)=(A(i)+D(i)+E(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X5(i)=(C(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X6(i)=(A(i)-B(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X7(i)=(B(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X12(i)=(E(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
X16(i)=(C(i)+D(i)/(x1n(i)+x2n(i)+x3n(i)+2*A(i)+4*B(i)+2*C(i)+2*D(i)+2*E(i)));
end
yc4=X4./(X4+X5);
yc5=X5./(X4+X5);
yc6=X6./(X6+X7+X12+X16);
yc7=X7./(X6+X7+X12+X16);
yc12=X12./(X6+X7+X12+X16);
yc16=X16./(X6+X7+X12+X16);
alpha14=yc6.*alpha18+yc7.*alpha19+yc12.*alpha113+yc16.*alpha117;
alpha24=yc6.*alpha28+yc7.*alpha29+yc12.*alpha213+yc16.*alpha217;
alpha34=yc6.*alpha38+yc7.*alpha39+yc12.*alpha313+yc16.*alpha317;
alpha15=yc6.*alpha110+yc7.*alpha115+yc12.*alpha114+yc16.*alpha118;
alpha25=yc6.*alpha210+yc7.*alpha215+yc12.*alpha214+yc16.*alpha218;
alpha35=yc6.*alpha310+yc7.*alpha315+yc12.*alpha314+yc16.*alpha318;
alpha16=yc4.*alpha81+yc5.*alpha101;
alpha26=yc4.*alpha82+yc5.*alpha102;
alpha36=yc4.*alpha83+yc5.*alpha103;
alpha17=yc4.*alpha91+yc5.*alpha151;
alpha27=yc4.*alpha92+yc5.*alpha152;
alpha37=yc4.*alpha93+yc5.*alpha153;
alpha112=yc4.*alpha131+yc5.*alpha141;
alpha212=yc4.*alpha132+yc5.*alpha142;
alpha312=yc4.*alpha133+yc5.*alpha143;
alpha116=yc4.*alpha171+yc5.*alpha181;
alpha216=yc4.*alpha172+yc5.*alpha182;
alpha316=yc4.*alpha173+yc5.*alpha183;
alpha46=yc5.*alpha810;
alpha47=yc5.*alpha915;
alpha412=yc5.*alpha1314;
alpha416=yc5.*alpha1718;
alpha56=yc4.*alpha108;
alpha57=yc4.*alpha159;
alpha512=yc4.*alpha1413;
alpha516=yc4.*alpha1817;
G14=yc6.*G18+yc7.*G19+yc12.*G113+yc16.*G117;
G24=yc6.*G28+yc7.*G29+yc12.*G213+yc16.*G217;
G34=yc6.*G38+yc7.*G39+yc12.*G313+yc16.*G317;
G15=yc6.*G110+yc7.*G115+yc12.*G114+yc16.*G118;
G25=yc6.*G210+yc7.*G215+yc12.*G214+yc16.*G218;
G35=yc6.*G310+yc7.*G315+yc12.*G314+yc16.*G318;
G16=yc4.*G81+yc5.*G101;
G26=yc4.*G82+yc5.*G102;
G36=yc4.*G83+yc5.*G103;
G17=yc4.*G91+yc5.*G151;
G27=yc4.*G92+yc5.*G152;
G37=yc4.*G93+yc5.*G153;
G112=yc4.*G131+yc5.*G141;
G212=yc4.*G132+yc5.*G142;
G312=yc4.*G133+yc5.*G143;
G116=yc4.*G171+yc5.*G181;
G216=yc4.*G172+yc5.*G182;
G316=yc4.*G173+yc5.*G183;
G46=yc5.*G810;
G47=yc5.*G915;
G412=yc5.*G1314;
G416=yc5.*G1718;
G56=yc4.*G108;
G57=yc4.*G159;
G512=yc4.*G1413;
G516=yc4.*G1817;
tau14=-log(G14)./alpha14;
tau24=-log(G24)./alpha24;
tau34=-log(G34)./alpha34;
tau15=-log(G15)./alpha15;
tau25=-log(G25)./alpha25;
tau35=-log(G35)./alpha35;
tau16=-log(G16)./alpha16;
tau26=-log(G26)./alpha26;
tau36=-log(G36)./alpha36;
tau17=-log(G17)./alpha17;
tau27=-log(G27)./alpha27;
tau37=-log(G37)./alpha37;
tau112=-log(G112)./alpha112;
tau212=-log(G212)./alpha212;
tau312=-log(G312)./alpha312;
tau116=-log(G116)./alpha116;
tau216=-log(G216)./alpha216;
tau316=-log(G316)./alpha316;
tau46=-log(G46)./alpha46;
tau47=-log(G47)./alpha47;
tau412=-log(G412)./alpha412;
tau416=-log(G416)./alpha416;
tau56=-log(G56)./alpha56;
tau57=-log(G57)./alpha57;
tau512=-log(G512)./alpha512;
tau516=-log(G516)./alpha516;
ln_y1_1=G12.*X2.*tau12+ G31.*X3.*tau13+ G14.*X4.*tau14+G15.*X5.*tau15+G16.*X6.*tau16+G17.*X7.*tau17+G112.*X12.*tau112+G116.*X16.*tau116;
ln_y1_2=G12.*X2+ G13.*X3+ G14.*X4+G15.*X5+G16.*X6+G17.*X7+G112.*X12+G116.*X16;
ln_y2_1=G21.*X1.*tau12+ G32.*X3.*tau32+ G24.*X4.*tau24+G25.*X5.*tau25+G26.*X6.*tau26+G27.*X7.*tau27+G212.*X12.*tau212+G216.*X16.*tau216;
ln_y2_2=G12.*X1+ G23.*X3+G24.*X4+G25.*X5+G26.*X6+G27.*X7+G212.*X12+G216.*X16;
ln_y3_1=G13.*X1.*tau13+ G23.*X3.*tau23+ G34.*X4.*tau34+G35.*X5.*tau35+G36.*X6.*tau36+G37.*X7.*tau37+G312.*X12.*tau312+G316.*X16.*tau316;
ln_y3_2=G13.*X1+ G23.*X3+ G34.*X4+G35.*X5+G36.*X6+G37.*X7+G312.*X12+G316.*X16;
ln_y4_1=G14.*X1.*tau14+G24.*X2.*tau24+G34.*X3.*tau34+G46.*X6.*tau46+G47.*X7.*tau47+G412.*X12.*tau412+G416.*X16.*tau416;
ln_y4_2=G14.*X1+G24.*X2+G34.*X3+G46.*X6+G47.*X7+G412.*X12+G416.*X16;
ln_y5_1=G15.*X1.*tau15+G25.*X2.*tau25+G35.*X3.*tau35+G56.*X6.*tau56+G57.*X7.*tau57+G512.*X12.*tau512+G516.*X16.*tau516;
ln_y5_2=G15.*X1+G25.*X2+G35.*X3+G56.*X6+G57.*X7+G512.*X12+G516.*X16;
ln_y6_1=G16.*X1.*tau16+G26.*X2.*tau26+G36.*X3.*tau36+G46.*X4.*tau46+G56.*X5.*tau56;
ln_y6_2=G16.*X1+G26.*X2+G36.*X3+G46.*X4+G56.*X5;
ln_y7_1=G17.*X1.*tau17+G27.*X2.*tau27+G37.*X3.*tau37+G47.*X4.*tau47+G57.*X5.*tau57;
ln_y7_2=G17.*X1+G27.*X2+G37.*X3+G47.*X4+G57.*X5;
ln_y12_1=G112.*X1.*tau112+G212.*X2.*tau212+G312.*X3.*tau312+G412.*X4.*tau412+G512.*X5.*tau512;
ln_y12_2=G112.*X1+G212.*X2+G312.*X3+G412.*X4+G512.*X5;
ln_y16_1=G116.*X1.*tau116+G216.*X2.*tau216+G316.*X3.*tau316+G416.*X4.*tau416+G516.*X5.*tau516;
ln_y16_2=G116.*X1+G216.*X2+G316.*X3+G416.*X4+G516.*X5;
ln_y1_3=(((X2.*G12)./ln_y2_2).*(tau12-(ln_y2_1)./(ln_y2_2)))+(((X3.*G13)./ln_y3_2).*(tau13-(ln_y3_1)./(ln_y3_2)));
ln_y1_4=(((X6.*G16)./ln_y6_2).*(tau16- (ln_y6_1./ln_y6_2))) + (((X7.*G17)./ln_y7_2).*(tau17- (ln_y7_1./ln_y7_2)))+(((X12.*G12)./ln_y12_2).*(tau112- (ln_y12_1./ln_y12_2)))+(((X16.*G16)./ln_y16_2).*(tau116- (ln_y16_1./ln_y16_2)));
ln_y1_5=(((X4.*G14)./ln_y4_2).*(tau14- (ln_y4_1./ln_y4_2))) + (((X5.*G15)./ln_y5_2).*(tau15- (ln_y5_1./ln_y5_2)));
yk=exp((ln_y1_1./ln_y1_2) + ln_y1_3 + ln_y1_4+ ln_y1_5)'; % activity coefficient for H2O
if any(~isfinite(yk))
yk = 10 * ones(size(yk));
end
I have the following code that I wish to estimate the parameters of a custom distribution using MATLAB's function mle(). For more details on the distribution.
The main code is:
x = [0 0 0 0 0.000649967501624919 0.00569971501424929 0.0251487425628719 0.0693465326733663 0.155342232888356 0.284835758212089 0.458277086145693 0.658567071646418 0.908404579771011 1.17284135793210 1.43977801109945 1.71951402429879 1.98925053747313 2.27553622318884 2.57147142642868 2.80390980450977 3.03829808509575 3.26583670816459 3.45642717864107 3.65106744662767 3.81950902454877 3.98275086245688 4.11259437028149 4.24683765811709 4.35043247837608 4.43832808359582 4.58427078646068 4.62286885655717 4.68361581920904 4.75686215689216 4.80245987700615 4.84005799710015 4.86280685965702 4.91675416229189 4.92725363731813 4.90890455477226 4.96570171491425 4.92315384230789 4.95355232238388 4.92790360481976 4.93135343232838 4.90310484475776 4.90885455727214 4.86765661716914 4.85490725463727 4.81940902954852 4.81450927453627 4.78621068946553 4.74206289685516 4.71791410429479 4.69961501924904 4.65706714664267 4.63611819409030 4.60176991150443 4.57512124393780 4.53507324633768 4.48252587370631 4.47062646867657 4.43127843607820 4.39963001849908 4.37598120093995 4.29548522573871 4.31033448327584 4.21708914554272 4.21913904304785 4.18669066546673 4.16719164041798 4.09774511274436 4.07989600519974 4.02869856507175 3.98485075746213 3.95785210739463 3.93945302734863 3.90240487975601 3.87025648717564 3.81185940702965 3.78461076946153 3.74091295435228 3.71666416679166 3.67276636168192 3.65846707664617 3.61361931903405 3.58712064396780 3.55452227388631 3.53082345882706 3.49197540122994 3.48582570871456 3.46512674366282 3.41227938603070 3.36278186090695 3.35528223588821 3.31238438078096 3.27213639318034 3.23863806809660 3.24173791310434 3.19339033048348 3.20118994050298 3.16489175541223 3.10739463026849 3.09484525773711 3.08094595270237 3.02129893505325 3.02309884505775 2.99375031248438 2.95765211739413 2.93230338483076 2.89560521973901 2.87805609719514 2.85440727963602 2.82285885705715 2.80175991200440 2.79091045447728 2.73901304934753 2.72701364931753 2.73441327933603 2.71646417679116 2.68236588170592 2.65551722413879 2.63356832158392 2.60361981900905 2.58147092645368 2.57697115144243 2.54287285635718 2.53502324883756 2.47702614869257 2.50387480625969 2.46487675616219 2.45722713864307 2.42707864606770 2.41762911854407 2.39823008849558 2.38708064596770 2.34058297085146 2.35613219339033 2.32123393830309 2.30503474826259 2.27613619319034 2.27248637568122 2.25113744312784 2.24908754562272 2.22703864806760 2.20583970801460 2.17244137793110 2.15709214539273 2.16469176541173 2.12139393030348 2.12809359532023 2.11389430528474 2.09774511274436 2.07629618519074 2.07459627018649 2.05394730263487 2.04724763761812 2.01684915754212 2.01684915754212 2.00409979501025 1.98955052247388 1.96540172991350 1.95890205489726 1.93035348232588 1.92295385230738 1.90605469726514 1.89785510724464 1.87070646467677 1.88000599970002 1.86295685215739 1.84420778961052 1.82510874456277 1.80480975951202 1.80785960701965 1.80870956452177 1.77581120943953 1.76771161441928 1.77131143442828 1.76636168191590 1.75081245937703 1.73156342182891 1.69876506174691 1.70836458177091 1.70376481175941 1.67196640167992 1.68101594920254 1.66586670666467 1.66061696915154 1.64296785160742 1.63291835408230 1.62506874656267 1.62516874156292 1.60556972151392 1.59007049647518 1.59187040647968 1.57947102644868 1.57577121143943 1.54527273636318 1.57237138143093 1.54637268136593 1.54802259887006 1.50492475376231 1.52077396130193 1.50417479126044 1.50162491875406 1.50062496875156 1.48957552122394 1.47997600119994 1.47027648617569 1.44452777361132 1.45407729613519 1.44272786360682 1.43247837608120 1.41657917104145 1.40787960601970 1.39323033848308 1.40282985850707 1.39403029848508 1.38233088345583 1.37888105594720 1.37943102844858 1.36183190840458 1.34808259587021 1.34503274836258 1.33703314834258 1.33308334583271 1.32253387330633 1.32698365081746 1.29963501824909 1.30758462076896 1.29103544822759 1.29473526323684 1.27413629318534 1.26858657067147 1.27888605569722 1.26063696815159 1.27863606819659 1.25168741562922 1.23913804309785 1.24788760561972 1.22308884555772 1.24198790060497 1.22133893305335 1.20678966051697 1.20098995050247 1.20343982800860 1.18779061046948 1.19024048797560 1.17194140292985 1.17369131543423 1.16869156542173 1.15814209289536 1.15429228538573 1.15904204789761 1.12774361281936 1.15344232788361 1.13744312784361 1.12909354532273 1.12479376031198 1.11099445027749 1.11469426528674 1.11064446777661 1.10464476776161 1.10309484525774 1.10689465526724 1.07654617269137 1.07884605769712 1.07359632018399 1.06864656767162 1.07544622768862 1.06689665516724 1.04884755762212 1.06164691765412 1.04979751012449 1.04529773511324 1.02839858007100 1.03634818259087 1.01709914504275 1.02089895505225 1.01024948752562 1.01549922503875 1.01319934003300 1.01404929753512 1.00839958002100 0.995400229988501 0.989850507474626 0.978801059947003 0.977551122443878 0.980450977451127 0.975451227438628 0.969201539923004 0.964151792410380 0.964601769911504 0.958802059897005 0.955702214889256 0.948602569871506 0.960751962401880 0.941352932353382 0.928653567321634 0.949002549872506 0.937053147342633 0.913854307284636 0.916204189790510 0.915454227288636 0.902604869756512 0.909454527273636 0.895505224738763 0.898355082245888 0.894455277236138 0.902454877256137 0.883705814709265 0.888405579721014 0.876356182190891 0.881555922203890 0.878156092195390 0.868456577171141 0.870406479676016 0.863906804659767 0.862456877156142 0.858757062146893 0.851307434628269 0.851107444627769 0.833908304584771 0.843507824608770 0.831708414579271 0.836858157092145 0.829058547072646 0.828508574571272 0.822908854557272 0.820508974551273 0.815559222038898 0.819709014549273 0.809609519524024 0.813409329533523 0.800759962001900 0.806609669516524 0.806959652017399 0.792260386980651 0.787660616969152 0.783810809459527 0.794960251987401 0.771061446927654 0.788910554472276 0.789510524473776 0.763061846907655 0.776761161941903 0.767561621918904 0.773611319434028 0.750262486875656 0.765811709414529 0.765911704414779 0.748012599370032 0.741612919354032 0.757312134393280 0.752612369381531 0.741362931853407 0.742212889355532 0.741912904354782 0.743162841857907 0.732963351832408 0.732813359332033 0.733363331833408 0.721913904304785 0.716664166791661 0.726713664316784 0.709764511774411 0.700064996750163 0.710764461776911 0.717664116794160 0.707314634268287 0.707114644267787 0.705614719264037 0.709164541772911 0.696665166741663 0.680765961701915 0.686715664216789 0.694465276736163 0.683015849207540 0.681715914204290 0.694465276736163 0.688615569221539 0.680665966701665 0.672316384180791 0.672866356682166 0.656517174141293 0.665316734163292 0.671566421678916 0.666266686665667 0.652917354132293 0.663366831658417 0.651917404129794 0.663816809159542 0.661366931653417 0.647017649117544 0.655167241637918 0.647867606619669 0.636918154092295 0.645467726613669 0.633118344082796 0.640217989100545 0.634668266586671 0.618669066546673 0.635068246587671 0.632568371581421 0.623118844057797 0.623868806559672 0.623718814059297 0.621368931553422 0.623768811559422 0.608419579021049 0.616019199040048 0.609869506524674 0.606569671516424 0.614019299035048 0.610269486525674 0.596520173991300 0.595570221488926 0.593270336483176 0.596670166491675 0.598470076496175 0.597770111494425 0.593720313984301 0.592770361481926 0.585420728963552 0.580870956452177 0.584120793960302 0.580270986450677 0.577971101444928 0.579021048947553 0.572821358932053 0.585970701464927 0.572921353932303 0.567071646417679 0.569971501424929 0.571271436428179 0.568421578921054 0.567421628918554 0.569521523923804 0.563721813909305 0.558772061396930 0.562171891405430 0.557872106394680 0.549072546372681 0.558722063896805 0.536973151342433 0.561021948902555 0.544172791360432 0.552122393880306 0.553072346382681 0.546222688865557 0.551472426378681 0.540772961351932 0.541122943852807 0.542772861356932 0.530323483825809 0.526023698815059 0.529273536323184 0.524573771311435 0.525923703814809 0.524923753812309 0.516474176291185 0.527273636318184 0.527723613819309 0.518424078796060 0.517874106294685 0.516074196290186 0.517924103794810 0.523173841307935 0.514474276286186 0.513174341282936 0.498875056247188 0.518024098795060 0.507924603769812 0.505524723763812 0.507174641267937 0.502874856257187 0.502624868756562 0.500624968751562 0.510824458777061 0.490925453727314 0.492675366231688 0.489925503724814 0.478126093695315 0.485775711214439 0.491775411229439 0.489925503724814 0.491325433728314 0.487225638718064 0.485725713714314 0.485675716214189 0.477676116194190 0.483875806209690 0.478026098695065 0.470176491175441 0.471926403679816 0.483625818709065 0.469376531173441 0.474026298685066 0.467826608669567 0.462426878656067];
Censored = ones(1,size(x,2));%
custpdf = #eval_custpdf;
custcdf = #eval_custcdf;
phat = mle(x,'pdf', custpdf,'cdf', custcdf,'start',[1 0.1 0.3 0.1 0.01 -0.3],...
'lowerbound',[0 0 0 0 0 -inf],'upperbound',[inf inf inf inf inf inf],'Censoring',Censored);
% Cheking how close the estimated PDF and CDF match with those from the data x
t = 0.001:0.001:0.5;
figure();
plot(t,x);hold on
plot(t,custpdf(t, phat(1), phat(2), phat(3), phat(4), phat(5), phat(6)))
figure();
plot(t,cumsum(x)./sum(x));hold on
plot(t,custcdf(t, phat(1), phat(2), phat(3), phat(4), phat(5), phat(6)))
The functions are:
function out = eval_custpdf(x,myalpha,mybeta,mytheta,a,b,c)
first_integral = integral(#(x) eval_K(x,a,b,c),0,1).^-1;
theta_t_ratio = (mytheta./x);
incomplete_gamma = igamma(myalpha,theta_t_ratio.^mybeta);
n_gamma = gamma(myalpha);
exponent_term = exp(-theta_t_ratio.^mybeta-(c.*(incomplete_gamma./n_gamma)));
numerator = first_integral.* mybeta.*incomplete_gamma.^(a-1).*...
theta_t_ratio.^(myalpha*mybeta+1).*exponent_term;
denominator = mytheta.* n_gamma.^(a+b-1).* (n_gamma-incomplete_gamma.^mybeta).^(1-b);
out = numerator./denominator;
end
function out = eval_custcdf(x,myalpha,mybeta,mytheta,a,b,c)
first_integral = integral(#(x) eval_K(x,a,b,c),0,1).^-1;
theta_t_ratio = (mytheta./x);
incomplete_gamma = igamma(myalpha,theta_t_ratio.^mybeta);
n_gamma = gamma(myalpha);
second_integral = integral(#(x) eval_K(x,a,b,c),0, incomplete_gamma.^mybeta./n_gamma);
% |<----- PROBLEMATIC LINE ----->|
out = first_integral*second_integral;
end
function out = eval_K(x,a,b,c)
out = x.^(a-1).*(1-x).^(b-1).*exp(-c.*x);
end
The integral that is causing the problem is the second intergral in the function eval_custcdf() as its upper limit is an array (denoted by PROBLEMATIC LINE).
Is there a way to take a single value from the array x such that the upper limit remains a scalar? And then calculate the cdf such that the output of the cdf is an array? Using a forloop, maybe? But I cannot seem to figure how to implement that?
How can I work around this problem?
Any help would be appreciated.
Thanks in advance.
eval_custcdf is a function expected to return 1D array of length n for
a given n data input.
I use a for loop to compute the output for a given input, then
return the whole array as output of eval_custcdf
I passed the input array elements one at a time
This is how eval_custcdf may look like
function out = eval_custcdf(x,myalpha,mybeta,mytheta,a,b,c)
out = zeros(size(x));
for i = 1: length(x)
first_integral = integral(#(w) eval_K(w,a,b,c),0,1).^-1;
theta_t_ratio = (mytheta./x(i));
incomplete_gamma = igamma(myalpha,theta_t_ratio.^mybeta);
n_gamma = gamma(myalpha);
second_integral = integral(#(w) eval_K(w,a,b,c),0, incomplete_gamma.^mybeta./n_gamma);
out(i) = first_integral*second_integral;
end
end
I am using MATLAB_R2016b.
I have data of this format
Temperature = [310:10:800];
Pressure = [0.1 0 1.0 10.0 100.0 1000.0];
Cv = ...
[ 73.6400 25.3290 73.5920 73.1260 69.4500 61.8600
72.8060 25.3810 72.7640 72.3450 68.9780 61.7040
71.9230 25.4380 71.8850 71.5070 68.4230 61.3140
71.0060 25.4990 70.9710 70.6290 67.8040 60.8160
70.0680 25.5640 70.0360 69.7270 67.1400 60.2840
69.1220 25.6340 69.0940 68.8140 66.4460 59.7550
68.1800 25.7070 68.1540 67.9000 65.7350 59.2500
27.6640 25.7840 67.2240 66.9940 65.0150 58.7780
27.3630 25.8640 66.3120 66.1040 64.2950 58.3390
27.1700 25.9480 65.4220 65.2330 63.5820 57.9340
27.0440 26.0340 64.5570 64.3850 62.8790 57.5600
26.9660 26.1230 63.7210 63.5640 62.1900 57.2130
26.9240 26.2150 62.9130 62.7700 61.5170 56.8890
26.9110 26.3090 62.1360 62.0050 60.8620 56.5870
26.9200 26.4050 61.3890 61.2690 60.2250 56.3020
26.9460 26.5030 33.1250 60.5620 59.6080 56.0320
26.9870 26.6030 31.8460 59.8850 59.0090 55.7750
27.0390 26.7050 31.0570 59.2360 58.4290 55.5290
27.1010 26.8080 30.5000 58.6170 57.8680 55.2920
27.1700 26.9120 30.0840 58.0280 57.3240 55.0630
27.2460 27.0170 29.7670 57.4700 56.7980 54.8410
27.3280 27.1240 29.5260 56.9450 56.2900 54.6250
27.4140 27.2320 29.3430 56.4560 55.7970 54.4150
27.5040 27.3410 29.2080 56.0070 55.3210 54.2090
27.5980 27.4500 29.1110 55.6040 54.8600 54.0080
27.6940 27.5610 29.0460 55.2610 54.4150 53.8100
27.7930 27.6720 29.0060 54.9970 53.9840 53.6160
27.8950 27.7840 28.9870 54.8470 53.5670 53.4260
27.9980 27.8970 28.9870 51.7540 53.1650 53.2390
28.1030 28.0110 29.0020 47.2710 52.7760 53.0550
28.2100 28.1250 29.0290 44.3160 52.4010 52.8750
28.3180 28.2400 29.0670 42.1390 52.0390 52.6980
28.4270 28.3550 29.1150 40.4520 51.6910 52.5230
28.5380 28.4710 29.1710 39.1070 51.3570 52.3520
28.6500 28.5880 29.2340 38.0170 51.0350 52.1840
28.7630 28.7060 29.3040 37.1240 50.7260 52.0200
28.8770 28.8240 29.3780 36.3870 50.4300 51.8580
28.9920 28.9420 29.4580 35.7750 50.1460 51.7000
29.1080 29.0610 29.5420 35.2640 49.8730 51.5440
29.2250 29.1810 29.6290 34.8380 49.6100 51.3930
29.3420 29.3010 29.7200 34.4810 49.3570 51.2440
29.4610 29.4220 29.8150 34.1820 49.1120 51.0990
29.5800 29.5440 29.9120 33.9330 48.8720 50.9570
29.6990 29.6660 30.0110 33.7250 48.6360 50.8190
29.8200 29.7880 30.1130 33.5540 48.4000 50.6830
29.9410 29.9110 30.2170 33.4130 48.1630 50.5520
30.0630 30.0340 30.3230 33.3000 47.9210 50.4230
30.1850 30.1580 30.4310 33.2100 47.6720 50.2990
30.3080 30.2820 30.5400 33.1400 47.4140 50.1770
30.4310 30.4070 30.6510 33.0890 47.1430 50.0590];
When I try to query a new [temperature, pressure] pair, for example [0.2, 341] by doing this
interp2(Temperature, Pressure, Cv, 0.2, 341)
I get the following error:
Error using griddedInterpolant
The grid vectors must be strictly monotonically increasing.
Error in interp2>makegriddedinterp (line 229)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 129)
F = makegriddedinterp({X, Y}, V, method,extrap);
What am I doing wrong? And how can I get the desired result?
You need to have the same number of points in Temperature and Pressure as you do in Cv. You can generate these points using meshgrid.
[Temp, Pres] = meshgrid(Temperature, Pressure)
% Temp and Pres are both 6x50 matrices
However, you still have an issue. Temperature and Pressure must be monotonically increasing, as the error message states. This means you can't have a pressure value go down, which it does. You must change the second value in the Pressure array, or for instance you may want to swap columns 1 and 2 of the Pressure and Cv arrays
Pressure = [0.1, 0, 1, 10, 100, 1000]; % original
Pressure = Pressure([2, 1, 3:end]); % swap columns 1 and 2, Pressure = [0,0.1,1,10,...]
Cv = [...]; % All of your data
Cv = Cv(:, [2, 1, 3:end]) % Swap columns 1 and 2
Now you can do your lookup. Note you also had the temperature and pressure values the wrong way around, they must be the same order for inputs 1/2 and inputs 4/5.
[Temp, Pres] = meshgrid(Temperature, Pressure);
out = interp2(Temp, Pres, Cv.', 341, 0.2) % not (..., 0.2, 341) as must be same order
>> out = 30.5468
The only consideration you might want to give is that you're using linear interpolation with interp2, but your pressure data is logarithmic. Check your results are sensible.
I am trying to use sequentialfs on a logistic regression in order to determine the variables to include. I have tried to modify the answer from here Sequential feature selection Matlab in order to make it work, but the handle part is tricky to me!
I am using
[b, dev, stats] = glmfit(X_train,y_train,'binomial','link','logit')
in order to fit a model, and then I use
y_hat = glmval(b,X_test,'logit');
to evaluate the model output. I have tried to make a handle
f = #(X_test, y_test)...
(sum(y_test ~= round(glmval(b,X_test,'logit'))))
Which just says that I have "Too many input arguments", when I use
sequentialfs(f,X,y)
Can anyone help identify the relevant variables? A data set is below, where all variables will be deemed relevant, but the real problem is of a much larger scale which leads to overparametrization of the model.
X=
0,0305780001742986 0,0293310740486058 0,0289653631914407 0,0313646568650811 0,0308948854477814 0,0298740323895053
0,0221062699789144 0,0213746063391538 0,0196872068542263 0,0209915418572080 0,0206064713419377 0,0198587113064423
0,0275588428138312 0,0273957214651399 0,0291622596392042 0,0313729847567230 0,0314439993783026 0,0307137185244424
0,0262451954064372 0,0230629198767100 0,0251192876802874 0,0243459679829053 0,0235752627390268 0,0245208219450122
0,0232074343987232 0,0272778269415268 0,0288725913067116 0,0274565324127577 0,0283032894902223 0,0287391056368953
0,0237589488887855 0,0251929947662669 0,0209989755767701 0,0146662148369109 0,0193830305676060 0,0198900627308170
0,0275142606053146 0,0311683593689258 0,0287109246912083 0,0307544961383919 0,0293964246310913 0,0291758079280418
0,0284337063240141 0,0249820611584738 0,0261664330153102 0,0270312804219022 0,0269178494606530 0,0273467522864029
0,0279150521116060 0,0314021886143824 0,0291020356476994 0,0278247389567505 0,0294200854382611 0,0306460336509255
0,0270622115094110 0,0317795784913586 0,0278283619288299 0,0307757941373800 0,0292513615541838 0,0283900407512898
0,0270275432108930 0,0330384417745352 0,0323886885104962 0,0330255939800101 0,0329789138848656 0,0333091935226094
0,0263417729025468 0,0243442097895390 0,0253328659546050 0,0270828343149025 0,0262845355278762 0,0257915212526289
0,0247503544929709 0,0282150822748136 0,0282408722769508 0,0306907484707723 0,0284025718962319 0,0280291257508206
0,0282116130443164 0,0259317921438547 0,0316179116969559 0,0300579055064814 0,0315134680888256 0,0299693403497154
0,0221040769734790 0,0232354360252008 0,0231588261739581 0,0240414200785524 0,0209509517094598 0,0223174875964419
0,0264965936690525 0,0312918915850473 0,0297480867085914 0,0349060220702562 0,0307640365732823 0,0299291946182921
0,0283027112468824 0,0288419304885060 0,0275801208398665 0,0239401671924088 0,0263296648119700 0,0260497226349653
0,0298063469363023 0,0253535298515575 0,0245113899712628 0,0158158669753461 0,0228044538675689 0,0221885556611738
0,0276409442724517 0,0283430130710139 0,0303893043659674 0,0314511518633802 0,0315673022208602 0,0302375851656905
0,0270849987059202 0,0312381323489334 0,0301662309393833 0,0290482017036615 0,0299207348490636 0,0295746114571195
0,0225683074444599 0,0297455473987182 0,0241145950178924 0,0233857691372279 0,0259911200866772 0,0248345888658664
0,0267870191916224 0,0254332496269710 0,0270915983261551 0,0263567311441536 0,0267470932454238 0,0279742674970829
0,0271933786309775 0,0274722435013798 0,0249484244285920 0,0301299698898670 0,0255527349811283 0,0263901147510067
0,0262114755708772 0,0168593285634855 0,0205147916736994 0,0227484518393022 0,0187327306255277 0,0197581601499656
0,0314796048983783 0,0281771847088388 0,0318159664952504 0,0325586660052902 0,0315277330661507 0,0324593871738733
0,0265694239044569 0,0239306067986609 0,0263341531447523 0,0277011505766640 0,0274385429019891 0,0258861916796922
0,0248731179403750 0,0253801474063385 0,0258606627949811 0,0234446644496543 0,0262626821946271 0,0265908368467206
0,0268335079060221 0,0327877888534457 0,0292050848084788 0,0286811931594028 0,0288058286572012 0,0297311873407772
0,0289655102183149 0,0297912585631799 0,0289955796469846 0,0301374575223736 0,0286017508461882 0,0294957275181626
0,0291599221376467 0,0284752300175276 0,0294302899166185 0,0291600417637315 0,0304544724881292 0,0299842921064845
0,0306574107981617 0,0282562949634004 0,0290757741762068 0,0266759231631069 0,0276079132984815 0,0283490628634946
0,0251419690541645 0,0190208495552799 0,0219740565645893 0,0229700198654972 0,0231913971059666 0,0218104715761714
0,0279646047804379 0,0213712975401601 0,0266486742241442 0,0282351537040943 0,0256010069497723 0,0254151479565295
0,0291819765619095 0,0274841050997730 0,0277287252877124 0,0253910521460239 0,0270103968729900 0,0280719282161061
0,0267034888169756 0,0230641558482702 0,0254891453796317 0,0246876229024146 0,0232209026694445 0,0255548967304529
0,0282525579813869 0,0275995431014968 0,0263616638576222 0,0282844384170093 0,0262147272435204 0,0257109782177451
0,0293098464976821 0,0323503780295293 0,0266630772869637 0,0228075737924046 0,0263296732877124 0,0254313353506367
y=
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
The function you use for sequentialfs should take in four variables. Yours only accepts two:
criterion = fun(XTRAIN,ytrain,XTEST,ytest)
This is because, even though you give only two variables, X and y, sequentialfs splits these into a training and testing subset (straight from the docs):
Starting from an empty feature set, sequentialfs creates candidate
feature subsets by sequentially adding each of the features not yet
selected. For each candidate feature subset, sequentialfs performs
10-fold cross-validation by repeatedly calling fun with different
training subsets of X and y, XTRAIN and ytrain, and test subsets of X
and y, XTEST and ytest
So the function passed for sequentialfs must take both the training and test subsets, e.g.:
function criterion = my_function(X_train,y_train,X_test,y_test)
[b, dev, stats] = glmfit(X_train,y_train,'binomial','link','logit')
y_hat = glmval(b,X_test,'logit');
criterion = sum(y_test ~= round(y_hat));
end
If this function is on your path you can pass it as #my_function, you don't have to make an anonymous function to get a handle.
I have data which are as the following :
MTtmax6000_N1000000_k+0.1_k-T0.001_k-D0.1_kh1.txt
# nMT=1000000 tmax=60000 trelax=10000 k+=0.1 k-T=0.001 k-D=0.1 kh=1
#t (L-L0) L varL NGTP varNGTP Cap varCap
0 0 50090.2 2089.48 0.100257 0.100158 0.104798 0.114295
100 0.897735 50091.1 2109.92 0.099841 0.0998968 0.104373 0.114029
200 1.80163 50092 2130.83 0.099736 0.0995947 0.104204 0.113554
300 2.70513 50092.9 2151.79 0.099775 0.0997319 0.104323 0.113928
400 3.60867 50093.9 2172.17 0.099982 0.0999776 0.104546 0.114294
500 4.50984 50094.8 2192.49 0.100229 0.100263 0.104795 0.114473
600 5.40802 50095.6 2213.72 0.100149 0.100159 0.10463 0.114101
700 6.3161 50096.6 2234.2 0.099856 0.100117 0.10433 0.114139
800 7.21386 50097.5 2254.76 0.099624 0.0997151 0.104171 0.113879
900 8.11601 50098.4 2275.18 0.100183 0.100386 0.104615 0.114237
1000 9.01724 50099.3 2296.13 0.100504 0.100423 0.105058 0.114745
1100 9.92572 50100.2 2317.11 0.100368 0.10056 0.105023 0.115089
1200 10.8262 50101.1 2338.26 0.099476 0.0998665 0.103951 0.113913
1300 11.7243 50102 2359.96 0.099775 0.0997559 0.104246 0.113753
1400 12.6273 50102.9 2381.2 0.100081 0.100099 0.104571 0.11406
1500 13.5297 50103.8 2401.8 0.099702 0.0997495 0.104267 0.114045
1600 14.4281 50104.7 2422.56 0.099792 0.0999496 0.104292 0.113975
1700 15.3369 50105.6 2443.44 0.099912 0.0999296 0.104452 0.114242
I tried to read these data by using dlmread, txtscan or textread when I implement the code I receive this massage:
Error using dlmread (line 139) Mismatch between file and format string. Trouble reading number from file (row 1u, field 1u) ==> # nMT=1000000 tmax=4000 trelax=1000 k+=1 k-T=0.01 k-D=0.1 kh=1\n
I want command to read txt files and ignore two first rows.Any help would be greatly appreciated. I will be grateful to you.
clc;
clear all;
close all;
%%
tic
Values11 = zeros(225,6);
K_minus_t =[0.01];
K_minus_d = [0.1];
%k_plus =[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
m=length(K_minus_t);
r=length(K_minus_d);
kk=0;
ll=1;
for l=1:r
for j=1:m
h=[1];
k_plus =[1];
K_minus_T =K_minus_t(j);
K_minus_D = K_minus_d(l);
sets = {k_plus, K_minus_T, K_minus_D,h};
[x,y,z r] = ndgrid(sets{:});
cartProd = [x(:) y(:) z(:) r(:)];
nFiles = size(cartProd,1);
filename{nFiles,j}=[];
for i=1:nFiles
%% MT_Sym_N1000000_k+1_k-T0.01_k-D0.1_kh1.txt
filename{i,j} = ['MT_Sym_N1000000_' ...
'k+' num2str(cartProd(i,1)) '_' ...
'k-T' num2str(cartProd(i,2),'%6.3g') '_' ...
'k-D' num2str(cartProd(i,3)) '_' ...
'kh' num2str(cartProd(i,4)) '' ...
'.txt'];
file1=dlmread(filename{i,j})
%% line (length)
t= file1(:,1);
dline= file1(:,2);
[coef_line1,s]= polyfit(t, dline, 1);
coef_line(i,:)= coef_line1;
v1{i}=s.R;
v2{i}=s.df;
v3{i}=s.normr;
Dl(i)=sqrt (v3{i}/length(t));
end
end
end
Use importdata:
x = importdata('file.txt',' ',2); %// ' ': col separator; 2: number of header lines
data = x.data; %// x.data is what you want
The first line gives a struct x with data, textdata and colheaders fields. The numeric data is in field data, so x.data is what you want.