function resid=miss(x) %This function returns the residual of the data from the model. We pass in %the data using the global variables: global tpass cpass ceqpass %We have the model concentrations: c=x(1)+(x(2)-x(1))*exp(x(3)*tpass); %And we have the residual: resid=sum((c-cpass).^2)+(ceqpass-x(1))^2; %Note that we have added in the residual due to the ceq measurement %as well! By the way, if some measurements were more accurate than %others, the residual returned by this function should have each term %weighted appropriately (by the inverse of the variance in each %measurement). This would improve the parameter estimation.