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
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")

figure2 - Instead approximate with lower degree polynomial:

yf(t)=c1+c2t++cn1tn2+cntn1, Or as matrix multiplication:

[y1y2y3ym][f(t1)f(t2)f(t3)f(tm)]=[1t1tn111t2tn121t3tn131tmtn1m][c1c2cn]=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
figure 3