the limits. Comparison between values is done with the if statement. This state-
ment asks whether a condition is fulfilled. If the condition is fulfilled, a command or
a series of commands is executed. If the condition is not fulfilled, a second if
statement (elseif) may ask if a second condition is fulfilled and allows condi-
tional execution of a second command or series of commands, until the end
statement is encountered. The if statement of fit4fit.m in lines 5–7 has the
following structure:
if (condition1) commands1; elseif (condition2) commands2; end ;
Condition 1, ep(I) < MIN(I) is true when the exponent of the fitted loga-
rithm of a parameter is smaller than MIN. In this case, the fitted parameter will be
set equal to the logarithm of MIN with the command pin(I) ¼ log(MIN(I)).
Condition 2 is true when the exponent of the fitted parameter is larger than MAX, and
consequently it then will be forced to be equal to log(MAX) . For the actual
calculations, the varied parameters pin have to be translated into meaningful
rate or equilibrium constants. Th is is done within fit4fit.m in lines 9–13 . Calcu-
lating the exponent from the logarithm has the additional advantage that the
constants never reach negative values. Negative concentrations or rate constants
simply do not make sense.
Note that the function leasqr in lines 40 and41 of the main program fit4.m
expects column vectors as arguments. The transpose operator ’ in line 22 of
fit4fit.m is used for this purpose. Also note that the resulting parame ters from
pin in lines 9–12 also are defined in global. This may seem unnecessary, but
sometimes one likes to repeat the fit and keep one parameter fixed. This can easily
be achieved without re-writing the whole program, simply by inserting the percent
symbol % in front of a line. This turns the line into a comment. For example, line 9
written as %KD1 ¼ exp(pin(1)); will not be executed. Because KD1 is
included in the
global statement, this variable can be used inside the function
fit4fit.m. Its value then is not varied and kept at the initial value defined in line 24
of fit4.m. The results of fit4.m are shown in Fig. 8.5.
For MATLAB users, the programs fit4M.m and fit4fitM.m are slightly differ-
ent. The MATLAB rout ine lsqcurvefit allows the selection of MIN (lb) and
MAX(ub) values directly, so that the if and elseif statements in lines 3–8 are
not required. The function lsqcurvefi t is rather flexible, but this flexib ility
requires options to be set. This is done in line 42 of the program fit4M.m with
the function optimset.
In general, MATLAB users may have to spend some time trying to find the best
options for the function lsqcurvefit. This is similar to finding the optimal solver
for differential equations. GNU Octave usually runs quite smoothly with its default
parameters both for leasqr and for lsode.
132 8 Fitting the Data