mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-11 13:27:38 +01:00
Beautifying task1.m
This commit is contained in:
parent
c7e5df2bcc
commit
c916b65bd9
16
ENUME/projectC/functionDataPoints.m
Normal file
16
ENUME/projectC/functionDataPoints.m
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
function dataPoints = functionDataPoints()
|
||||||
|
dataPoints = (-5:5)';
|
||||||
|
dataPoints(:, 2) = [
|
||||||
|
-6.5743
|
||||||
|
0.9765
|
||||||
|
3.1026
|
||||||
|
1.8572
|
||||||
|
1.3165
|
||||||
|
-0.6144
|
||||||
|
0.1032
|
||||||
|
0.3729
|
||||||
|
2.5327
|
||||||
|
7.3857
|
||||||
|
9.4892
|
||||||
|
];
|
||||||
|
end
|
||||||
84
ENUME/projectC/task1.m
Normal file
84
ENUME/projectC/task1.m
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
function task1(polynomialDegree)
|
||||||
|
for currentPolynomialDegree = 0 : polynomialDegree
|
||||||
|
dataPoints = functionDataPoints();
|
||||||
|
% obtain LSPSolutions of approximating polynomial
|
||||||
|
[LSPSolutions, error, conditionNumber] = approximate(dataPoints, currentPolynomialDegree);
|
||||||
|
displayInfo(currentPolynomialDegree, error, conditionNumber);
|
||||||
|
plotGraph(currentPolynomialDegree, dataPoints, LSPSolutions)
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function displayInfo(currentPolynomialDegree, error, conditionNumber)
|
||||||
|
disp("Current Polynomial Degree:");
|
||||||
|
disp(currentPolynomialDegree);
|
||||||
|
disp("Error: ");
|
||||||
|
disp(error);
|
||||||
|
disp("Condition number of Gram's Matrix: ");
|
||||||
|
disp(conditionNumber);
|
||||||
|
end
|
||||||
|
|
||||||
|
function plotGraph(currentPolynomialDegree, dataPoints, LSPSolutions)
|
||||||
|
figure;
|
||||||
|
grid on;
|
||||||
|
hold on;
|
||||||
|
plotDataPoints(currentPolynomialDegree, dataPoints);
|
||||||
|
plotApproximation(dataPoints, LSPSolutions);
|
||||||
|
hold off;
|
||||||
|
end
|
||||||
|
|
||||||
|
function plotDataPoints(currentPolynomialDegree, dataPoints)
|
||||||
|
title(['Polynomial approximation of the funtion using: ', num2str(currentPolynomialDegree), ' degree']);
|
||||||
|
scatter(dataPoints(:, 1), dataPoints(:, 2), 72, 'x');
|
||||||
|
end
|
||||||
|
|
||||||
|
function plotApproximation(dataPoints, LSPSolutions)
|
||||||
|
x = dataPoints(1):0.05:dataPoints(end, 1);
|
||||||
|
y = valueApproximationAtx(LSPSolutions, x);
|
||||||
|
plot(x, y);
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
% find the approximating polynomial of the given degree
|
||||||
|
function [LSPSolutions, error, conditionNumber] = approximate(dataPoints, polydeg)
|
||||||
|
% initialize A matrix
|
||||||
|
A = zeros(size(dataPoints, 1), polydeg + 1);
|
||||||
|
|
||||||
|
A = calculateACells(dataPoints, A);
|
||||||
|
LSPSolutions = solveLSP(A, dataPoints);
|
||||||
|
|
||||||
|
% Calculate error of solution
|
||||||
|
error = norm(dataPoints(:, 2) - A * LSPSolutions);
|
||||||
|
% Calculate condition number of Gram's matrix
|
||||||
|
GramsMatrix = A' * A;
|
||||||
|
conditionNumber = cond(GramsMatrix);
|
||||||
|
end
|
||||||
|
|
||||||
|
function Matrix = calculateACells(dataPoints, Matrix)
|
||||||
|
for row = 1 : size(Matrix, 1)
|
||||||
|
for column = 1 : size(Matrix, 2)
|
||||||
|
Matrix(row, column) = dataPoints(row, 1) ^ (column - 1);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function solutions = solveLSP(Matrix, dataPoints)
|
||||||
|
[Q, eqsys, invqtq] = QRDecomposition(Matrix);
|
||||||
|
eqsys(:, end + 1) = invqtq * Q' * dataPoints(:, 2);
|
||||||
|
solutions = backSubstitution(eqsys);
|
||||||
|
end
|
||||||
|
|
||||||
|
% evaluate the value of an approximation at the given x
|
||||||
|
function arrayOfValues = valueApproximationAtx(LSPSolutions, arrayOfArguments)
|
||||||
|
arrayOfValues = zeros(1, size(arrayOfArguments, 2));
|
||||||
|
arrayOfValues = calculateArrayofValues(arrayOfArguments, arrayOfValues, LSPSolutions);
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function arrayOfValues = calculateArrayofValues(arrayOfArguments, arrayOfValues, LSPSolutions)
|
||||||
|
for x = 1 : size(arrayOfArguments, 2)
|
||||||
|
for i = 1 : size(LSPSolutions, 1)
|
||||||
|
arrayOfValues(x) = arrayOfValues(x) + LSPSolutions(i) * arrayOfArguments(x) ^ (i - 1);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,68 +1,56 @@
|
|||||||
% define function data points
|
function task1()
|
||||||
taskfunc = (-5:5)';
|
for polydeg = 0:3
|
||||||
taskfunc(:, 2) = [
|
dataPoints = functionDataPoints();
|
||||||
-6.5743
|
% obtain factors of approximating polynomial
|
||||||
0.9765
|
[factors, error, gramcond] = approximate(dataPoints, polydeg);
|
||||||
3.1026
|
|
||||||
1.8572
|
% print error and condition number
|
||||||
1.3165
|
disp(['Approximation degree ', num2str(polydeg), ':']);
|
||||||
-0.6144
|
disp(['Error: ', num2str(error)]);
|
||||||
0.1032
|
disp(['Condition number: ', num2str(gramcond)]);
|
||||||
0.3729
|
|
||||||
2.5327
|
% plot data points
|
||||||
7.3857
|
figure;
|
||||||
9.4892
|
grid on;
|
||||||
];
|
hold on;
|
||||||
|
title(['Polynomial approximation of the function (degree ', ...
|
||||||
% perform the task
|
num2str(polydeg), ')']);
|
||||||
for polydeg = 0:3
|
scatter(dataPoints(:, 1), dataPoints(:, 2));
|
||||||
% obtain factors of approximating polynomial
|
|
||||||
[factors, error, gramcond] = approximate(taskfunc, polydeg);
|
% plot approximation
|
||||||
|
x = dataPoints(1):0.05:dataPoints(end, 1);
|
||||||
% print error and condition number
|
y = evalapprox(factors, x);
|
||||||
disp(['Approximation degree ', num2str(polydeg), ':']);
|
plot(x, y);
|
||||||
disp(['Error: ', num2str(error)]);
|
|
||||||
disp(['Condition number: ', num2str(gramcond)]);
|
% finish and print graph
|
||||||
|
hold off;
|
||||||
% plot data points
|
set(gcf, 'PaperPosition', [0 0 6 4]);
|
||||||
figure;
|
set(gcf, 'PaperSize', [6 4]);
|
||||||
grid on;
|
%print(['report/approx', num2str(polydeg)], '-dpdf');
|
||||||
hold on;
|
end
|
||||||
title(['Polynomial approximation of the function (degree ', ...
|
|
||||||
num2str(polydeg), ')']);
|
|
||||||
scatter(taskfunc(:, 1), taskfunc(:, 2));
|
|
||||||
|
|
||||||
% plot approximation
|
|
||||||
x = taskfunc(1):0.05:taskfunc(end, 1);
|
|
||||||
y = evalapprox(factors, x);
|
|
||||||
plot(x, y);
|
|
||||||
|
|
||||||
% finish and print graph
|
|
||||||
hold off;
|
|
||||||
set(gcf, 'PaperPosition', [0 0 6 4]);
|
|
||||||
set(gcf, 'PaperSize', [6 4]);
|
|
||||||
%print(['report/approx', num2str(polydeg)], '-dpdf');
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
% find the approximating polynomial of the given degree
|
% find the approximating polynomial of the given degree
|
||||||
function [factors, error, gramcond] = approximate(func, polydeg)
|
function [factors, error, gramcond] = approximate(dataPoints, polydeg)
|
||||||
% define the A matrix used for solving the error minimization problem
|
% define the A matrix used for solving the error minimization problem
|
||||||
A = zeros(size(func, 1), polydeg + 1);
|
A = zeros(size(dataPoints, 1), polydeg + 1);
|
||||||
|
|
||||||
% calculate cells of A using natural basis
|
% calculate cells of A using natural basis
|
||||||
for row = 1:size(A, 1)
|
for row = 1:size(A, 1)
|
||||||
for col = 1:size(A, 2)
|
for col = 1:size(A, 2)
|
||||||
A(row, col) = func(row, 1) ^ (col - 1);
|
A(row, col) = dataPoints(row, 1) ^ (col - 1);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
% solve least-square problem using QRdash decomposition
|
% solve least-square problem using QRdash decomposition
|
||||||
[Q, eqsys, invqtq] = QRDecomposition(A);
|
[Q, eqsys, invqtq] = QRDecomposition(A);
|
||||||
eqsys(:, end + 1) = invqtq * Q' * func(:, 2);
|
eqsys(:, end + 1) = invqtq * Q' * dataPoints(:, 2);
|
||||||
factors = backSubstitution(eqsys);
|
factors = backSubstitution(eqsys);
|
||||||
|
|
||||||
% calculate error and condition number of Gram's matrix
|
% calculate error and condition number of Gram's matrix
|
||||||
error = norm(func(:, 2) - A * factors);
|
error = norm(dataPoints(:, 2) - A * factors);
|
||||||
gramcond = cond(A' * A);
|
gramcond = cond(A' * A);
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user