%c 加上额外项 % 重新生成时间向量 t = linspace(1996, 2010, length(data)); % 构造矩阵A和向量b A =…
- User
%c 加上额外项
% 重新生成时间向量 t = linspace(1996, 2010, length(data));
% 构造矩阵A和向量b A = [ones(length(data),1) t’ cos(2pit’) sin(2pit’) cos(4pit’)]; b = data;
% 求解最小二乘问题 x = A\b; c1 = x(1); c2 = x(2); c3 = x(3); c4 = x(4); c5 = x(5);
% 计算拟合值和RMSE f = c1 + c2t + c3cos(2pit) + c4sin(2pit) + c5cos(4pit); RMSE = sqrt(mean((f-data).^2));
% 绘图 plot(t, data, ‘o’, t, f, ‘-’) xlabel(‘年份’) ylabel(‘CO2浓度 (ppv)’) legend(‘数据点’, ‘拟合曲线’)
- Assistant
在代码中加入额外项可以进一步提高拟合精度。以下是在上述代码中加入sin(4πt)项的示例:
% 重新生成时间向量 t = linspace(1996, 2010, length(data));
% 构造矩阵A和向量b A = [ones(length(data),1) t’ cos(2pit) sin(2pit) cos(4pit) sin(4pit)]; b = data;
% 求解最小二乘问题 x = A\b; c1 = x(1); c2 = x(2); c3 = x(3); c4 = x(4); c5 = x(5); c6 = x(6);
% 计算拟合值和RMSE f = c1 + c2t + c3cos(2pit) + c4sin(2pit) + c5cos(4pit) + c6sin(4pi*t); RMSE = sqrt(mean((f-data).^2));
% 绘图 plot(t, data, ‘o’, t, f, ‘-’) xlabel(‘年份’) ylabel(‘CO2浓度 (ppv)’) legend(‘数据点’, ‘拟合曲线’)