Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ROBLEM 1

Write-up:
In this problem, we are given 2 different sets of data. We used the data in NHMC as the values of x,
while we used the C6H6 as the values of y. The two variables may not be a perfect fit since R^2 is not
exactly 1, but it is still correlated to each other seeing how close it is to

1.
Final Answer: R^2: 0.98420. The two variables may not be a perfect fit since R^2 is not
exactly 1, but it is still strongly correlated to each other because it is close to 1.

Best-Fit Line

Script / Code:

x = [11 18 84 260 200 145 188 152 103 193]'; % Given datas of NMHC

y = [1.6 1.9 7.5 21.4 15.4 12.5 15.1 13.7 10.4 15.2]'; % Given datas of 𝐶6𝐻6

% Plot the data points

scatter(x,y,'b','filled','MarkerEdgeColor','k');

axis([10 195 1 16]); grid on; box on;

% Perform least-squares regression


phi = @(x) [x(1) x(1)*x(2)]; % Define basis functions

PHI = cell2mat(arrayfun(@(j) phi([x(j) y(j)]), (1:length(x))','Uni',0)); % Create PHI matrix

W = (PHI'*PHI)\(PHI'*y); % Solve for W

% Display results

a = W(1); b = -W(2);

fprintf('Parameters:\n a = %.3f\n b = %.3f\n',a,b);

xp = linspace(x(1),x(end),100)'; % Finely spaced "x" values

yp = a*xp./(1+b*xp); % Predicted "y" at those "x"

hold on; plot(xp,yp,'k','LineWidth',1.2); hold off;

% Calculate R2

yp = a*x./(1+b*x);

R2 = 1 - sum((y-yp).^2)/sum((y-mean(y)).^2);

fprintf('R^2: %.5f\n',R2);

PROBLEM 2

a)
Write-up:
We used the Ridge Regularization approach to solve this problem in order to discover the solution.to the
issue at hand. The years 1959 through 2019 are entered first as x, and the value of the amount of CO2
that exists annually. It is assumed that a third-degree polynomial and a 0.01 as the regularization value
Using the provided code, it is anticipated that, in accordance with the plot, Mauna Loa, Hawaii's annual
CO2 concentration is expected to be 437.784 in 2030.

Final Answer: R^2: 0.99939. In 2030, the expected annual CO2 concentration (in ppm) is around
437.784
Best-Fit Curve

Script / Code:

x = [1959:1:2019]'; % Years from 1959 to 2019

y = [315.98 316.91 317.64 318.45 318.99 319.62 320.04 321.37 322.18 323.05 324.62 325.68 326.32

327.46 329.68 330.18 331.12 332.04 333.83 335.40 336.84 338.75 340.11 341.45 343.05 344.66

346.12 347.43 349.18 351.57 353.12 354.39 355.61 356.45 357.10 358.83 360.82 362.61 363.73

366.70 368.38 369.55 371.14 373.28 375.80 377.52 379.80 381.90 383.79 385.59 387.43 389.90

391.65 393.86 396.52 398.64 400.83 404.22 406.55 408.52 411.43]'; % annual mean atmospheric

CO2 concentration data set from 1959 to 2019

N = length(x); close all; clc;

scatter(x,y,'b','filled','MarkerEdgeColor','k'); % Plot data points

axis([1958 2030 315 412]); grid on; box on; % Customize the plot

reg = [.01]; % Choices for regularization parameter

style = {'k'}; % Plotting styles (color)

for j = 1:length(reg)

% Perform least-squares regression

phi = @(x) x.^(0:3); % Define basis functions

PHI = cell2mat(arrayfun(phi,x,'Uni',0)); % Create PHI matrix

W = (PHI'*PHI + reg(j)*eye(size(PHI,2)))\(PHI'*y); % Solve for W

% Display results

fprintf('\ny = %.3f\n',W(1));

for k = 2:length(W)

fprintf(' + %.3f x^%d\n',W(k),k-1);

end

xp = linspace(1958,2030,200)'; % Finely spaced "x" values

yp = polyval(flip(W),xp); % Predicted "y" at those "x"

hold on; plot(xp,yp,style{j},'LineWidth',1.5);

% Calculate R2

yp = polyval(flip(W),x);
R2 = 1 - sum((y-yp).^2)/sum((y-mean(y)).^2);

fprintf('R^2: %.5f\n',R2);

end

b)
Write-up:
For this problem, we are given the data on annual CO2 concentration from 1959 to 2019. It is

also given that we need to use a 5th-degree polynomial and a suitable regularization for this

model. With all the given data, we are expected to forecast the CO2 concentration in the year

2022 in Mauna Lao, Hawaii. We used ridge regularization and the provided code to solve this

problem, and the regularization that we used is 0.01. Based from the plot, it is expected that

the annual CO2 concentration on 2030 is around 439.216.

Final Answer: R^2: 0.99941. Using a 0.01 regularization, the expected annual CO2 concentration
(in ppm) is around 439.216. The A model has lower MSE than the B model.

Best-Fit Curve

Script Code:

x = [1959:1:2019]'; % Years from 1959 to 2019

y = [315.98 316.91 317.64 318.45 318.99 319.62 320.04 321.37 322.18 323.05 324.62 325.68 326.32

327.46 329.68 330.18 331.12 332.04 333.83 335.40 336.84 338.75 340.11 341.45 343.05 344.66

346.12 347.43 349.18 351.57 353.12 354.39 355.61 356.45 357.10 358.83 360.82 362.61 363.73

366.70 368.38 369.55 371.14 373.28 375.80 377.52 379.80 381.90 383.79 385.59 387.43 389.90

391.65 393.86 396.52 398.64 400.83 404.22 406.55 408.52 411.43]'; % annual mean atmospheric

CO2 concentration data set from 1959 to 2019

N = length(x); close all; clc;

scatter(x,y,'b','filled','MarkerEdgeColor','k'); % Plot data points

axis([1958 2030 315 412]); grid on; box on; % Customize the plot

reg = [0.01]; % Choices for regularization parameter

style = {'k'}; % Plotting styles (color)


for j = 1:length(reg)

% Perform least-squares regression

phi = @(x) x.^(0:5); % Define basis functions

PHI = cell2mat(arrayfun(phi,x,'Uni',0)); % Create PHI matrix

W = (PHI'*PHI + reg(j)*eye(size(PHI,2)))\(PHI'*y); % Solve for W

% Display results

fprintf('\ny = %.3f\n',W(1));

for k = 2:length(W)

fprintf(' + %.3f x^%d\n',W(k),k-1);

end

xp = linspace(1958,2030,200)'; % Finely spaced "x" values

yp = polyval(flip(W),xp); % Predicted "y" at those "x"

hold on; plot(xp,yp,style{j},'LineWidth',1.5);

% Calculate R2

yp = polyval(flip(W),x);

R2 = 1 - sum((y-yp).^2)/sum((y-mean(y)).^2);

fprintf('R^2: %.5f\n',R2);

end

You might also like