3.1 Fitting Functions to Data
Example:
year = 1955:5:2000;
temp = [ -0.0480, -0.0180, -0.0360, -0.0120, -0.0040,
0.1180, 0.2100, 0.3320, 0.3340, 0.4560 ];
scatter(year,temp,label="data",
xlabel="year",ylabel="anomaly (degrees C)",leg=:bottomright)

figure1
- A polynomial interpolant would overfit the data:
t = @. (year-1950)/10;
n = length(t);
V = [ t[i]^j for i in 1:n, j in 0:n-1 ];
c = V\temp;
p = Polynomial(c);
f = yr -> p((yr-1950)/10);
plot!(f,1955,2000,label="interpolant")
- Instead approximate with lower degree polynomial:
y≈f(t)=c1+c2t+⋯+cn−1tn−2+cntn−1, Or as matrix multiplication:
[y1y2y3⋮ym]≈[f(t1)f(t2)f(t3)⋮f(tm)]=[1t1⋯tn−111t2⋯tn−121t3⋯tn−13⋮⋮⋮1tm⋯tn−1m][c1c2⋮cn]=Vc.
V is m×n, taller then it is wider. We cannot solve this exactly. But Julia can solve it approximately with the same \
operator !:
V = [ t.^0 t ];
@show size(V);
c = V\temp;
p = Polynomial(c);
f = yr -> p((yr-1955)/10);
scatter(year,temp,label="data",
xlabel="year",ylabel="anomaly (degrees C)",leg=:bottomright);
plot!(f,1955,2000,label="linear fit")

figure 3