% Function file for example est8.2 % This evaluates the residuals in the nonlinear equations % for nonlinear least squares % function f=fun(x,xm) ; f=zeros(13,1); % EXtract variables from vector x t1=x(1); % Temperature of tank 1 t2=x(2); % Temperature of tank 2 f0=x(3); % Flow into tank 1 f1=x(4); % Flow into tank 2 f2=x(5); % Flow out of tank 2 c0=x(6); % Concentration of reactant in feed v1=x(7); % Volume of tank 1 v2=x(8); % Volume of tank 2 c1=x(9); % Concentration of reactant in tank 1 c2=x(10); % Concentration of reactant in tank 2 % % Enter measuements from plant t1m=xm(1); % Measured value of temperature in tank 1 t2m=xm(2); % Measured value of temperature in tank 2 f0m=xm(3); % Measured value of flow into tank 1 f1m=xm(4); % Measured value of flow into tank 2 f2m=xm(5) ; % Measured value of flow out of tank 2 c0m=xm(6); % Measured value of feed concentration v1m=xm(7); % Measured value of volume in tank 1 v2m=xm(8); % Measured value of volume in tank 2 c1m=xm(9); % Measured value of concentration in tank 1 % % k1=2400*(exp(-4000/t1)); % Rate of reaction in tank 1 k2=2400*(exp(-4000/t2)); % Rate of reaction in tank 2 % % Next calculate the residuals for least squares % f(1)=(f0-f1); % Residual in total balance tank 1 f(2)=(f1-f2); % Residual in toatl mass balance tank 2 f(3)=f0*c0-f1*c1-k1*c1^2*v1; % Residual in comp balance tank 1 f(4)=f1*c1-f2*c2-k2*c2^2*v2; % Residual in comp balance tank 2 % % These are residuals in measurement equations % f(5)=(t1-t1m); f(6)=(t2-t2m); f(7)=f0-f0m; f(8)=f1-f1m; f(9)=f2-f2m; f(10)=c0-c0m; f(11)=c1-c1m; f(12)=v1-v1m; f(13)=v2-v2m;