Page 60 - Basics of MATLAB and Beyond
P. 60
Where p i is the population for year x i , and i =1, 2,...N. We can write
this series of equations as a matrix equation:
2
p 1 1 x 1 x 1
p 2 x 2
1 x 2 c 0
2 c 1 .
. .
. = .
. . c 2
1 x 2
p N x N
N
Or, defining matrices,
P = X · C .
In matlab the X matrix is calculated as follows:
>> X = [ones(size(year)) year year.^2]
X =
1 1788 3196944
1 1790 3204100
.
.
.
1 1993 3972049
1 1994 3976036
1 1995 3980025
The backslash operator solves the equation for the coefficient matrix C:
>> C = X\P
C =
1.0e+09 *
2.0067
-0.0022
0.0000
The third coefficient is not really zero; it is simply too small (compared
9
to 2.0 × 10 ) to show in the default output format. We can change this
by typing:
>> format long e
>> C
C =
2.006702229622023e+09
-2.201930087288049e+06
6.039665477603122e+02
The backslash operator does its best to solve a system of linear equations
using Gaussian elimination or least-squares algorithms, depending on
whether the system is exact, or over- or under-determined. We can
c 2000 by CRC Press LLC