Sei sulla pagina 1di 8

16/20

Lusanda Hlela Assignment 3 HLLLUS001


06/09/2018

[8] Question 1

Code output:
(a) Normal Equations
LU Factorisation

Matrix Number Emach X1 X2 X3 r(X1) r(X2) r(X3)


1 1.4901e-07 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000 ok

2 1.4901e-08 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000


3 1.4901e-09 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000
4 2.2204e-15 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000
5 2.2204e-16 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000
6 2.2204e-17 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000

Observations:
The application of normal equations in this exercise required transforming the given 4 by 3
rectangular matrix.
This was performed by multiplying the Matrix A and the n – vector (b) by its transpose (AT); yielding a
3 by 3 square matrix. Six Emach values were iterated input elements in matrix A, thus producing 6
matrices of A.
Residual Calculations were performed to quantify the error of Xi, and there was no error observed,
as seen from the figure above(r(Xi)).

Code Input:
clear; clc; lines(0)

printf('\n Assignment 3')


printf('\n \n Linear Least Squares')
printf('\n \n Question 1')

e = [10*sqrt(%eps) sqrt(%eps) 0.1*sqrt(%eps) 10*%eps %eps 0.1*%eps];


printf('\n \n (a) Normal Equations\n LU Factorisation');

solutions = ["Matrix Number", "Emach", "X1", "X2", "X3", "r(X1)", "r(X2)", "r(X3)"]
printf("\n\n")

printf(" %-10s",solutions')
printf("\n")

for n = 1:6
//Original sytem
A = [1 1 1; e(n) 0 0; 0 e(n) 0; 0 0 e(n)]; //Rectangular Matrix for an over determined system.
b = [1 0 0 0]';
//Developed system (a)
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

b = A'*b;
A = A'*A ; //Square Matrix to improve sensetivity and properly apply LU Decomposition.
[L, U, P] = lu(A);
c = P*b;
y = L\c;
x = U\y;
r = abs(b - A*x);
m = n;
printf('\n%12.f %18.4e %10.4f %14.4f %14.4f %16.5f %14.5f %14.5f', m,
e(n),x(1),x(2),x(3),r(1),r(2),r(3))
end

Code output:
(b) Augmented system of equations
Matrix Number Emach X1 X2 X3 r(X1) r(X2) r(X3) r(X4)
1 1.4901e-07 0.33 0.33 0.33 0.0 0.0 0.0 0.0 ok
2 1.4901e-08 0.33 0.33 0.33 0.0 0.0 0.0 0.0
3 1.4901e-09 0.33 0.33 0.33 0.0 0.0 0.0 0.0
4 2.2204e-15 0.50 0.50 0.00 0.0 0.0 0.0 0.0
5 2.2204e-16 1.00 0.00 0.00 0.0 0.0 0.0 0.0
6 2.2204e-17 1.00 0.00 0.00 0.0 0.0 0.0 0.0
Observations:
Unlike in (a); the transformation of matrix A was such that the transformed A had more system
equations, providing a solution inclusive of residuals.
Matrix A was transformed into a 7 by 7 square matrix; and three X solutions were obtained, followed
by 4 system residuals.

Code Input:
printf('\n\n (b) Augmented system of equations');

solutions = ["Matrix Number", "Emach", "X1", "X2", "X3", "r(X1)", "r(X2)", "r(X3)", "r(X4)"];
printf("\n\n")
printf(" %-10s",solutions')
printf("\n")

for n = 1:6
// Original sytem
A = [1 1 1; e(n) 0 0; 0 e(n) 0; 0 0 e(n)]; //Rectangular Matrix for an over determined system.
b = [1 0 0 0]';
// Constructing sub-matrices & developing the Augmented system ==>>(7 by 7 matrix).
// AM ==>> AugmentedMatrix
AM(1:4,1:4) = diag(ones());
AM(1:4,5:7) = A;
AM(5:7,1:4) = A';
AM(5:7,5:7) = zeros();
A = AM;
b = [1 0 0 0 0 0 0]';
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

[L, U, P] = lu(A);
c = P*b;
y = L\c;
x = U\y; // 7 by 1 m vector, containing 4 residuals and 3 x solutions.
// Residual Extraction
R = abs(x(1:4,1));
// Solutions Extraction
x = x(5:7,1);
m = n;
printf('\n%12.f %18.4e %10.4f %14.4f %14.4f %16.5f %14.5f %14.5f %14.5f', m,
e(n),x(1),x(2),x(3),R(1),R(2),R(3),R(4));
end

c) QR factorisation (Back slash operator)


Matrix Number Emach X1 X2 X3 r(X1) r(X2) r(X3)
1 1.4901e-07 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
2 1.4901e-08 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000 ok

3 1.4901e-09 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000


4 2.2204e-15 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
5 2.2204e-16 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000
6 2.2204e-17 1.0000 0.0000 0.0000 0.00000 0.00000 0.00000

Code Input:
printf('\n\n (c) QR factorisation (Back slash operator)');

solutions = ["Matrix Number", "Emach", "X1", "X2", "X3", "r(X1)", "r(X2)", "r(X3)"];
printf("\n\n")
printf(" %-10s",solutions')
printf("\n")

for n = 1:6
// Original sytem
A = [1 1 1; e(n) 0 0; 0 e(n) 0; 0 0 e(n)]; //Rectangular Matrix for an over determined system.
b = [1 0 0 0]';
x = A\b;
m = n;
printf('\n%12.f %18.4e %10.4f %14.4f %14.4f %16.5f %14.5f %14.5f', m,
e(n),x(1),x(2),x(3),R(1),R(2),R(3));
end
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

Code output:
(d) Linear least square solver
Matrix Number Emach X1 X2 X3 r(X1) r(X2) r(X3)
1 1.4901e-07 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
2 1.4901e-08 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
ok
3 1.4901e-09 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
4 2.2204e-15 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
5 2.2204e-16 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000
6 2.2204e-17 0.3333 0.3333 0.3333 0.00000 0.00000 0.00000

Code Input:
printf('\n\n (d) Linear least square solver');

solutions = ["Matrix Number", "Emach", "X1", "X2", "X3", "r(X1)", "r(X2)", "r(X3)"];
printf("\n\n")
printf(" %-10s",solutions')
printf("\n")

for n = 1:6
// Original sytem
A = [1 1 1; e(n) 0 0; 0 e(n) 0; 0 0 e(n)]; //Rectangular Matrix for an over determined system.
b = [1 0 0 0]';
x = lsq(A, b);
m = n;
printf('\n%12.f %18.4e %10.4f %14.4f %14.4f %16.5f %14.5f %14.5f', m,
e(n),x(1),x(2),x(3),R(1),R(2),R(3));
end
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

[8] Question 2

Code output:
x-values????
Model matrix G column properties

Matrix number Minimum residual var(X1) var(X2) var(X3) var(X4)

55 0.0000012180 0.00575 0.00555 0.00547 0.00036

Matrix G column variation with minimum residual

Basis set Column(G1) Column(G1) Column(G2) Column(G3)

G(j,i) 2 1 2 3

Observations:
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

ok
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

Code input:
printf('\n')
printf('\n\n Question 2\n')

//m-data points
x = [0:0.05:1]'
y = [3.000 3.510 3.730 3.636 3.220 2.586 1.918 1.420 1.239 1.417 1.874 2.437 2.899 3.089 2.936 2.489
1.902 1.378 1.102 1.171 1.563]'

scf(1);clf
plot(x, y)
xlabel('Independent variables X(i)','fontsize',3)
ylabel('Corresponding Y(i) values','fontsize',3)
title('Data points to be modelled','fontsize',3)

//Basis sets assorted in groups


G1 = [x^0 sqrt(x) x x^2] //2 basis sets to be chosen
G2 = [exp(-x) exp(-3*x) exp(-5*x)] //1 basis set to be chosen
G3 = [sin(x) sin(6*x) sin(12*x) sin(18*x)] //1 basis set to be chosen

//Constructing the basis sets matrices


//4 basis sets required, thus 4 columns of 21 rows per matrix G
//Assuming basis set order is important, permutations principle will be applied
//Following the principle, G1 can form 16 variations in matrix G, hence any two basis sets are required from
G1, thus 4^2 =16.
//One basis set from G2, thus 16*(3^1)=48 variations.
//One basis set from G3, thus 48*(4^1)=192 total matrix G variations.

n=0
g1 = 0
for i = 1:4
G(:,1) = G1(:,i)
g1 = g1 + 1
g2 = 0
for j = 1:4
G(:,2) = G1(:,j)
g2 = g2 +1
g3 = 0
for k = 1:3
G(:,3) = G2(:,k)
g3 = g3 + 1
g4 = 0
for h = 1:4
n=n+1
g4 = g4 + 1
G(:,4)= G3(:,h)
a = G\y
R = abs(G*a - y)
r(n) = R'*R
v(n,:)=(diag(norm(R)*sqrt((G'*G)^-1)))'
N1(n) = g1
N2(n) = g2
N3(n) = g3
N4(n) = g4
end
end
end
end
Lusanda Hlela Assignment 3 HLLLUS001
06/09/2018

n = linspace(1, n, n)'

AG = [n r v(:,1) v(:,2) v(:,3) v(:,4)] //All G matrix properties


N = [n N1 N2 N3 N4] //Column variations

//Extracting the matrix G column variation with minimun residual


[m, n] = min(real((AG(:,2))))

printf('\n\n Model matrix G column properties');


P = ["Matrix number", "Minimum residual", "var(X1)","var(X2)","var(X3)","var(X4)"]
printf("\n\n")
printf(" %-10s",P')
printf("\n")

printf('\n%12.f %24.10f %13.5f %14.5f %14.5f %14.5f \n',AG(n,1:6))

//Finding the matrix combination for the model function


//We know that this matrix is at combination number n = 55 of 192.

printf('\n\n Matrix G column variation with minimum residual');


M = ["Basis set", "Column(G1)", "Column(G1)", "Column(G2)", "Column(G3)"];
printf("\n\n")
printf(" %-10s",M')
printf("\n")

printf('\n%13s %12.f %14.f %14.f %14.f', "G(j,i)" ,N(55,2:5));

A1 = G2(1:21,2) //G(1,2)
A2 = G1(1:21,2) //G(1,1)
A3 = G1(1:21,1) //G(2,2)
A4 = G3(1:21,3) //G(3,3)

A = [A1 A2 A3 A4]

a = A\y
Y = A*a

scf(2);clf;
plot(x,Y,'blk')
xlabel('Independent variables X(i)','fontsize',3)
ylabel('Model Y(i) values','fontsize',3)
title('Modelled data points','fontsize',3)

scf(3);clf
plot(x, y,':diamondmagenta')
plot(x,Y,'->red')
legend('Data','Model')
xlabel('Independent variables X(i)','fontsize',3)
ylabel('Model/Data points Y(i)','fontsize',3)
title('Least squares graphical analysis','fontsize',5)

Potrebbero piacerti anche