Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
[8] Question 1
Code output:
(a) Normal Equations
LU Factorisation
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)
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
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
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)
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)'
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)