elements).
If we define these data points (
t
i
, L
i
)
, i
= 1
..
19, then the error between the measured length,
L
i
, at time
t
i
and the model evaluated at
t
i
is
e
i
=
L
i

L
*
1

e

bt
i
,
i
= 1
..
19
.
The
Sum of Square Errors
function satisfies
J
(
L
*
, b
) =
19
X
i
=1
(
L
i

L
(
t
;
L
*
, b
))
2
=
19
X
i
=1
e
2
i
,
and has some scalar value for each pair of the parameter values, (
L
*
, b
), which we select as a vector
in MatLab
p
= [(
L
*
, b
]. With
p
we define a MatLab function for this sum of square errors function
between the data and the model
L
(
t
;
L
*
, b
) =
L
*
1

e

bt
=
p
(1)
1

e

p
(2)
t
.
1
function
J = sumsq
vonBert (p , tdata , ldata )
2
% Function
computing
sum
of
square
e r r o r s
f o r
von
Bertalanffy
model
3
model = p (1)
*
(1

exp
(

p (2)
*
tdata ) ) ;
4
e r r o r
= model

ldata ;
5
J =
e r r o r
*
error
’ ;
6
end
This function takes advantage of the
vector
capabilities of MatLab. The data are stored as
vectors. Any of the internal functions, such as
exp
, with a vector argument produces a vector, and
scalars are added and subtracted componentwise,
i.e.
,
a

exp
([
x
1
, x
2
, ..., x
n
]) = [
a

e
x
1
, a

e
x
2
, ..., a

e
x
n
]
.
In this function,
error
= [
e
1
, e
2
, ..., e
n
] is a row vector, so
error
0
, the transpose, is a column vector.
The product
error
*
error
0
is
[
e
1
, e
2
, ..., e
n
]
e
1
e
2
.
.
.
e
n
=
e
2
1
+
e
2
2
+
...
+
e
2
n
,
which is the sum of square errors.
As noted above the age and length data are stored in
tdfish
and
ldfish
, respectively. The optimal
solution is the
nonlinear least squares fit
to these data, which is the minimum possible value of
the
sum of square errors
function over all possible
L
*
and
b
. MatLab has a powerful function,
which is capable of numerically finding the minimum of a function,
fminsearch
. You can learn about
numerical algorithms for finding minima in Math 541 and details about this particular function
with the MatLab
help fminsearch
command. For our problem, we need a reasonable initial guess,
p
0
= [
L
*
0
, b
0
] = [100
,
0
.
1].
(A poor initial guess may prevent the algorithm from converging or
require multiple iterations.) In addition, since our sum of square errors function requires input of
the data, these must be supplied to the
fminsearch
in the
OPTIONS
part of this function (separated
by []). Below we show how to execute the MatLab function