Algebra
Algebra
UNIVERSITY OF TECHNOLOGY
Linear Algebra
INTERPOLATION, EXTRAPOLATION AND
CLIMATE CHANGE
MEMBER LIST
1 Theoritical background
1.1 Polynomial Interpolation and Least Squares Method
Polynomial Interpolation
Polynomial interpolation is the process of estimating values between known data points using polynomials. Given a
set of n + 1 data points (xi , yi ), polynomial interpolation aims to find a polynomial P (x) of degree at most n such
that:
1
P (xi ) = yi for all i = 0, 1, . . . , n
To find the coefficients a and b that minimize E, we solve the normal equations derived from setting the partial
derivatives of E with respect to a and b to zero:
m−1
∂E X
= −2 xi [yi − (axi + b)] = 0
∂a i=0
m−1
∂E X
= −2 [yi − (axi + b)] = 0
∂b i=0
m−1
X m−1
X
xi a + mb = yi
i=0 i=0
2
Quadratic Least Squares
For a quadratic function f (x) = ax2 + bx + c, the error is:
m−1
X
E= [yi − (ax2i + bxi + c)]2
i=0
The coefficients a, b, and c that minimize E are found by solving the normal equations:
m−1
∂E X
= −2 x2i [yi − (ax2i + bxi + c)] = 0
∂a i=0
m−1
∂E X
= −2 xi [yi − (ax2i + bxi + c)] = 0
∂b i=0
m−1
∂E X
= −2 [yi − (ax2i + bxi + c)] = 0
∂c i=0
m−1
X m−1
X m−1
X m−1
X
x3i a + x2i b + xi c = xi yi
i=0 i=0 i=0 i=0
m−1
X m−1
X m−1
X
x2i a + xi b + mc = yi
i=0 i=0 i=0
Summary
• Polynomial interpolation uses polynomials to exactly pass through given data points.
• Least squares fitting finds the best-fit polynomial that minimizes the sum of squared residuals, useful when
data has noise or does not follow a polynomial relationship perfectly.
• Both linear and quadratic least squares methods involve solving systems of linear equations derived from
minimizing the error function.
These techniques are fundamental in numerical analysis, data fitting, and are widely used in various scientific
and engineering applications.
2 TASK
1.We will start by approximating the data for the average temperatures in Kansas.In computations, i will work with
high temperatures only. This is a plot of the temperatures in Matlab:
clear;
WeatherHigh=[37 44 55 66 75 84 89 88 80 69 53 41]’;
figure;
plot(1:12,WeatherHigh,’r-x’);
axis([1,12 30 95]);
Result:
3
2. Select the corresponding data from the variable WeatherHigh above and generate the Vandermonde matrix based
on this data. Solving the resulting system using Matlab’s backslash linear system solver:
x=[1,5,8,12]; V=vander(x);
CoefHigh=V WeatherHigh(x);
Result:
3.Matlab’s polyval function can be used to efficiently evaluate a polynomial at the given set of points. It takes two
vectors as arguments. The first vector represents the coefficients of the polynomial and the second vector represents
the points we want to evaluate the polynomial at.Plotting the graph of the polynomial obtained at the previous
step by using the following code:
xc=1:0.1:12;
ycHigh=polyval(CoefHigh,xc);
plot(xc,ycHigh,’b-’,1:12,WeatherHigh,’r-x’);
axis([1,12 30 95]);
Result:
4.Producing a polynomial approximation based on the temperatures from the following six months: January, March,
May, August, October, and December. Using a polynomial of the fifth degree and plotting the resulting polynomial
next to the exact values as in the step above.
WeatherHigh = [37 44 55 66 75 84 89 88 80 69 53 41]’;
selectedMonths = [1 3 5 8 10 12];
selectedTemperatures = WeatherHigh(selectedMonths);
CoefHigh = polyfit(selectedMonths, selectedTemperatures, 5);
xc = 1:0.1:12;
ycHigh = polyval(CoefHigh, xc);
figure;
plot(xc, ycHigh, ’b-’, selectedMonths, selectedTemperatures, ’ro’, 1:12, WeatherHigh, ’r-x’);
axis([1, 12, 30, 95]);
Result:
4
5.Modifying the code above and run it in Matlab using the temperatures from all twelve months:
WeatherHigh = [37 44 55 66 75 84 89 88 80 69 53 41]’;
CoefHigh = polyfit(1:12, WeatherHigh, 11);
xc = 1:0.1:12;
ycHigh = polyval(CoefHigh, xc);
figure;
plot(xc, ycHigh, ’b-’, 1:12, WeatherHigh, ’r-x’);
axis([1, 12, 30, 95]);
warning(’off’, ’MATLAB:polyfit:RepeatedPointsOrRescale’);
Result:
6. Matlab function interp1 allows one interpolate a one-dimensional function.Plot the vectors ycHigh1, ycHigh2,
and ycHigh3 versus xc on the same plot to observe the smoothness of the resulting data.The function interp1 also
has a possibility to extrapolate the results which we will explore shortly
WeatherHigh = [31, 34, 39, 45, 50, 55, 60, 65, 70, 75, 80, 85];
x = 1:12;
xc = 1:0.1:12;
ycHigh1 = interp1(x, WeatherHigh, xc, ’linear’);
ycHigh2 = interp1(x, WeatherHigh, xc, ’pchip’);
ycHigh3= interp1(x, WeatherHigh, xc, ’spline’);
figure;
plot(xc, ycHigh1, ’g-’, ’LineWidth’, 2); hold on;
plot(xc, ycHigh2, ’b-’, ’LineWidth’, 2);
plot(xc, ycHigh3, ’m-’, ’LineWidth’, 2);
plot(x, WeatherHigh, ’r-x’, ’LineWidth’, 2);
axis([1, 12, 30, 95]);
grid on;
hold off;
Result:
5
7.Extrapolation means finding an approximation to the data outside of the initial measurement interval. For our
data, we will take the average global temperatures from the last 136 years starting from the year 1880.To begin,
load the matrix temperature from the file "temperature.mat” using the load command.
load(’temperature.mat’);
years = 1880:2015;
avg-temp = temperature(:, 1);
extrapolation-years = 2016:2030;
extrapolated-temps = interp1(years, avg-temp, extrapolation-years, ’spline’, ’extrap’);
figure;
plot(years, avg-temp, ’b-’, ’LineWidth’, 2);
hold on;
plot(extrapolation-years, extrapolated-temps, ’r–’, ’LineWidth’, 2);
xlim([1880, 2030]);
grid on;
hold off;
8.Plot the graph of temperatures using the plot function. This will produce a graph of fluctuations of average
temperatures since 1880.
load(’temperature.mat’);
years = temperature(:, 1);
temp = temperature(:, 2);
figure;
plot(years, temp, ’b-’, ’LineWidth’, 2);
grid on;
Result:
9.Assume that we want to know the temperature about 10 years into the future (again, it is advised not to
attempt to extrapolate too far beyond the initial interval).To produce three graphs based on the different interpo-
lation/extrapolation strategies which were discussed above, we use this code:
futureyears = 2016:2025;
futuretemp1 = interp1(years, temp, futureyears, ’linear’, ’extrap’);
futuretemp2 = interp1(years, temp, futureyears, ’pchip’, ’extrap’);
futuretemp3 = interp1(years, temp, futureyears, ’spline’, ’extrap’);
6
figure;
plot(years,temp,’b-o’,futureyears,futuretemp1,’g-o’,...
futureyears,futuretemp2,’r-x’,futureyears,futuretemp3,’k-d’);
Result:
10.So what happened in the previous example? The local fluctuations in temperature significantly affected the global
predictions for the future. However, we are not interested in the local fluctuations but rather in global tendencies.We
will use orthogonal projections to separate the global behavior from the “noise”. As a first approximation, let us
consider how we can assess the overall average temperature.
11.Define the variable b1 to be the length(temp)-dimensional 1 vector. In the space after that, define the variable P1
to hold the projection matrix. You will either want to use Matlab’s norm function.Also, define the variable temp1
to be the value P1*temp.
load(’temperature.mat’);
years = temperature(:, 1);
temp = temperature(:, 2);
b1 = ones(length(temp), 1);
P1 = (b1 * b1’) / (b1’ * b1);
temp1 = P1 * temp;
figure;
plot(years, temp, ’b-o’, ’DisplayName’, ’Original Data’);
hold on; plot(years, temp1, ’r-’, ’DisplayName’, ’Projected Data’);
xlabel(’Year’);
ylabel(’Temperature’);
hold off;
Result:
7
13.Question 1:How does the value of temp1 relate to the average value of temperature?
Answer: The value of temp1 is the result of projecting the original temperature data onto the subspace created
using the all-one vector b1. This means temp1 only includes the global component of the temperature, removing
local fluctuations. Therefore, the average value of temp1 is the average temperature value globally.
14.Like vectors, matrices also have norms. The norm |A| of a matrix A describes how far A is from the zero matrix
in a certain sense.
ans=2.0201e-15
Question 2:What does the result say about the relationship between the projection matrix P and its square P*P ?
Answer: The result of norm(P1 * P1 - P1) being close to 0 (in the case of exact solution) indicates that the pro-
jection matrix P1 and its square P1*P1 are nearly identical. This reflects the nature of the projection matrix, as
it performs a symmetric projection onto the corresponding subspace, and performing it twice does not alter the
result.
15.One way to produce an orthonormal basis in Matlab is to use the Matlab orth function:
m=length(years);
B2=[ones(m,1) years];
Q2=orth(B2);
This creates a matrix B = [1 years], whose first column consists entirely of ones and whose second column is the
vector years, which provides the years of observation. The new array Q2 is defined to be the result of orth(B2).
The function orth will create an orthonormal basis (defined by the columns of Q2) for the subspace S.
Question 3: What kind of matrix is the matrix Q2T Q2?
Answer: The matrix Q2T Q2 is an identity matrix. This occurs because the matrix Q2 is an orthogonal matrix,
meaning its columns are orthogonal to each other. When multiplying an orthogonal matrix by its transpose, the
result is an identity matrix because the columns of the orthogonal matrix form an orthogonal basis for the subspace,
and multiplying by its transpose produces the identity matrix.
16.Create a new cell and in this cell create a variable P2 to hold the value of the matrix of the orthogonal projection
onto the subspace S. Repeat the previous steps to produce the projection vector temp2=P2*temp and plot this
vector in the previous figure window.This will produce the linear approximation to the given data.
load(’temperature.mat’);
years = temperature(:,1);
temp = temperature(:,2);
m = length(years);
B2 = [ones(m, 1) years];
Q2 = orth(B2);
P2 = Q2 * Q2’;
temp2 = P2 * temp;
result = norm(P2 * P2 - P2);
disp(result);
pause
Result: 1.7878e-16
17.Finally, let us approximate the data by using a quadratic function. Create a new cell and reproduce the code
from from the previous cell but with renamed variables (e.g., B3 instead of B2). Make sure to copy the plotting
8
code and switch P2 to P3. Aside from changing 2s to 3s, the only other change you’ll need to make is to add a
column to the B3 matrix.
load(’temperature.mat’);
years = temperature(:,1);
temp = temperature(:,2);
m = length(years);
B3 = [ones(m, 1), years,years.2 ];
Q3 = orth(B3);
P3 = Q3 * Q3’;
temp3 = P3 * temp;
result = norm(P3 * P3 - P3);
disp(result);
Result: 3.5574e-16 18.Plot the initial data and all the approximations above on a new plot . Observe that the
quadratic function (shown by the magenta line) appears to follow the global tendencies of the temperature graph
pretty well. load(’temperature.mat’);
years = temperature(:,1);
temp = temperature(:,2);
n = length(temp);
b1 = ones(n, 1);
c1 = (b1’ * temp)/ (b1’ * b1);
temp1 = c1 * b1;
m = length(years);
B2 = [ones(m, 1), years];
Q2 = orth(B2);
P2 = Q2 * Q2’;
temp2 = P2 * temp;
B3 = [ones(m, 1), years, years.2 ];
Q3 = orth(B3);
P3 = Q3 * Q3’;
temp3 = P3 * temp;
figure;
plot(years, temp, ’bo’, ’DisplayName’, ’Original Data’);
hold on;
plot(years, temp1, ’g-’, ’DisplayName’, ’Constant Approximation’);
plot(years, temp2, ’r-’, ’DisplayName’, ’Linear Approximation’);
plot(years, temp3, ’m-’, ’DisplayName’, ’Quadratic Approximation’);
xlabel(’Year’);
ylabel(’Temperature’);
Result:
19.Now, let us make predictions for the future.This should create our prediction for an average temperature for the
9
next 100 years.At the same time, NASA reports that the currently available models for climate change predict rise
in temperatures anywhere between a 2C and 6C, so our simple model predictions are right in the middle!
load(’temperature.mat’);
years = temperature(:,1);
temp = temperature(:,2);
m = length(years);
B3 = [ones(m, 1), years, years.2 ];
Q3 = orth(B3);
P3 = Q3 * Q3’;
temp3 = P3 * temp;
futureyears = 2016:2116;
futuretemp3 = interp1(years, temp3, futureyears, ’spline’, ’extrap’);
plot(futureyears, futuretemp3, ’g-’);
figure;
3 Conclusion
Through this project, I gained valuable insights into various fundamental concepts of algebra, particularly in the con-
text of data approximation and projection. One crucial aspect I learned is the significance of orthogonal projections
in data analysis. By understanding how to project data onto subspaces defined by orthonormal bases, I acquired a
powerful tool for extracting meaningful information from datasets while preserving important characteristics. This
project also reinforced my understanding of matrix operations and their applications in algebraic computations.
Specifically, I learned about projection matrices and their role in transforming data into lower-dimensional spaces,
which is essential for simplifying complex datasets and identifying underlying trends. Moreover, I deepened my un-
derstanding of polynomial approximations, particularly linear and quadratic models. By exploring different degrees
of polynomial approximations, I gained insights into the trade-offs between model complexity and accuracy. This
knowledge is invaluable for effectively modeling real-world phenomena using mathematical expressions, as it allows
for the creation of models that balance simplicity and predictive power. Furthermore, I honed my skills in using
MATLAB for algebraic computations and data visualization. By implementing various algorithms and plotting
the results, I developed proficiency in translating mathematical concepts into practical code and interpreting the
outcomes visually. Overall, this project provided me with a comprehensive understanding of algebraic techniques
for data approximation and projection, empowering me to tackle a wide range of mathematical problems with
confidence and proficiency.
4 Reference
[1] (for Theory): Gilbert Strang (1980), Linear Algebra and its Applications – Second Edition
(for MATLAB): MATLAB Help Center. Link: https://www.mathworks.com/help/?s-tid=gn-supp
10