function P3_08BC clear, clc, format short g, format compact x1=[0.0464 0.0861 0.2004 0.2792 0.3842 0.4857 0.5824 0.6904 0.7842 0.8972]'; gamma1=[1.2968 1.2798 1.2358 1.1988 1.1598 1.1196 1.0838 1.0538 1.0311 1.0078]'; gamma2=[0.9985 0.9998 1.0068 1.0159 1.0359 1.0676 1.1096 1.1664 1.2401 1.4038]'; %xyData=[]; X=x1; m=size(X,1); %Determine the number of data points Y=gamma1+gamma2; prob_title = (['Nonlinear Regression for Parameters of Margules Equations']); dep_var_name=['gsum ']; ind_var_name=['x1']; parm=[0.25 0.46]; npar=size(parm,2); % Determine the number of the parameters options=optimset('MaxFunEvals',1000); % Change the default value for MaxFunEvals Beta=fminsearch(@NonlinFun,parm,options,X,Y); % Find optimal parameters using fminsearch [f,Ycalc]=NonlinFun(Beta,X,Y); % Compute Y (calculated) at the optimum disp([' Results,' prob_title ]); Res=[]; for i=1:npar Res=[Res; i Beta(i)]; end disp(' Parameter No. Value '); disp(Res); s2=sum((Y-Ycalc)'*(Y-Ycalc))/(m-npar); %variance disp([' Variance ', num2str(s2)]); ymean=mean(Y); R2=(Ycalc-ymean)'*(Ycalc-ymean)/((Y-ymean)'*(Y-ymean));%linear correlation coefficient disp([' Correlation Coefficient ', num2str(R2)]) errsum=0; for i=1:m gamma1c(i)= exp((1-x1(i)) ^ 2 * (2 * Beta(2) - Beta(1)) + 2 * (1-x1(i)) ^ 3 * (Beta(1) - Beta(2))); gamma2c(i)= exp(x1(i) ^ 2 * (2 * Beta(1) - Beta(2)) + 2 * x1(i)^ 3 * (Beta(2) - Beta(1))); errsum=errsum +(gamma1(i)-gamma1c(i))^2+(gamma2(i)-gamma2c(i))^2; end disp([' Sum of squares of errors ', num2str(errsum)]); plot(Y,Y-Ycalc,'*') % Residual plot title(['Residual Plot, ' prob_title ]) xlabel([dep_var_name '(Measured)']) ylabel('Residual') pause plot(X,Ycalc,'r-',X,Y,'b+' ) %Plot of experimental and calculated data title(['Calculated/Experimental Data ' prob_title]) xlabel([ind_var_name]) ylabel([dep_var_name]) function [f,Ycalc]=NonlinFun(parm,X,Y) A=parm(1); B=parm(2); for i=1:size(X,1); Ycalc(i,1)=exp((1-X(i))^2*(2*B-A)+2*(1-X(i))^3*(A-B))+exp(X(i)^2*(2*A-B)+2*X(i)^3*(B-A)); end resid(:,1)=Y-Ycalc; f=resid'*resid;