% Part (a)
A = importdata('scrippsm.txt');
year = (0:0.0027:0.5)' + 1996; % each data point is approximately 2.7 days apart
t = year - 1996; % set reference year to 1996
y = A - mean(A); % remove mean from data
X = [ones(size(t)) t cos(2pit) sin(2pit)];
C = X\y;
f = X*C + mean(A);
RMSE = sqrt(mean((f-y).^2));
disp(['Optimal coefficients: ' num2str(C')]);
disp(['RMSE: ' num2str(RMSE)]);
% plot results from 1989 to current year
t_plot = (0:0.01:0.5)' + 1989; % plot every 0.01 years
X_plot = [ones(size(t_plot)) t_plot cos(2pit_plot) sin(2pit_plot)];
f_plot = X_plot*C + mean(A);
plot(year, A, 'o', t_plot, f_plot);
xlabel('Year');
ylabel('CO2 concentration (ppv)');
% Part (b)
months = [5 9 17 21]'; % indices of May and September in each year
X_pred = [ones(size(months)) (1996+months/12-2004) cos(2pi(1996+months/12-2004)) sin(2pi(1996+months/12-2004))];
f_pred = X_pred*C + mean(A);
actual = [380.63; 374.06; 382.45; 376.73];
errors = f_pred - actual;
disp(['Prediction errors: ' num2str(errors')]);
% Part (c)
X = [X cos(4pit)];
C = X\y;
f = X*C + mean(A);
RMSE = sqrt(mean((f-y).^2));
disp(['Optimal coefficients: ' num2str(C')]);
disp(['RMSE: ' num2str(RMSE)]);
% repeat Part (a) and (b) with new model
f_plot = X_plotC + mean(A);
plot(year, A, 'o', t_plot, f_plot);
xlabel('Year');
ylabel('CO2 concentration (ppv)');
f_pred = X_predC + mean(A);
errors = f_pred - actual;
disp(['Prediction errors: ' num2str(errors')]);
% Part (d)
X = [X t.^2];
C = X\y;
f = X*C + mean(A);
RMSE = sqrt(mean((f-y).^2));
disp(['Optimal coefficients: ' num2str(C')]);
disp(['RMSE: ' num2str(RMSE)]);
disp(['Improvement: ' num2str(RMSE - sqrt(mean((X(:,1:4)*C(1:4)-y).^2)))]);
% Part (e)
X = [X_plot cos(2pit_plot) sin(2pit_plot)];
C = X\y;
f_plot = XC + mean(A);
plot(year, A, 'o', t_plot, f_plot);
xlabel('Year');
ylabel('CO2 concentration (ppv)');
X_pred = [ones(size(months)) (1996+months/12-2004) cos(2pi*(1996+months/12-2004)) sin(2pi(1996+months/12-2004)) t_pred.^2];
f_pred = X_pred*C + mean(A);
errors = f_pred - actual;
disp(['Prediction errors: ' num2str(errors')]);
% Summary table:
% | Part | Model | Coefficients (C) | RMSE | Prediction Errors |
% |------|-----------------------|---------------------------|----------|-----------------------|
% | (a) | f(t)=c1+c2t+c3cos(2pit)+c4sin(2pit) | optimal values | RMSE value | errors at 4 time points |
% | (c) | f(t)=c1+c2t+c3cos(2pit)+c4sin(2pit)+c5cos(4pit) | optimal values | RMSE value | errors at 4 time points |
% | (d) | f(t)=c1+c2t+c3cos(2pit)+c4sin(2pit)+c5cos(4pit)+c6t^2 | optimal values | RMSE value | - |
% | (e) | f(t)=c1+c2t+c3cos(2pit)+c4sin(2pit)+c5cos(4pit)+c6t^2+cos(2pit)+sin(2pit) | optimal values | RMSE value | errors at 4 time points |
%
% The model in Part (d) brings the most improvement, as evidenced by the significantly lower RMSE value relative to the model in Part (c). The addition of the squared-t term improves the model's ability to capture the non-linear trend in the data. The final model in Part (e) combines the improvements from both Parts (c) and (d) and produces the best results overall, with the lowest RMSE value and the smallest prediction errors.