Created
May 19, 2017 19:34
-
-
Save jonathan-nwosu/89f3a5e08dcd64890c6e6b049d180695 to your computer and use it in GitHub Desktop.
Basic matrix operations on Matlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%Question 1.. | |
clear all | |
close all | |
clc | |
A = [20 -10 0 0 0 0 0 0 | |
-10 20 -10 0 0 0 0 0 | |
0 -10 15 -5 0 0 0 0 | |
0 0 -5 10 -5 0 0 0 | |
0 0 0 -5 20 -15 0 0 | |
0 0 0 0 -15 30 -15 0 | |
0 0 0 0 0 -15 30 -15 | |
0 0 0 0 0 0 -15 15]; | |
B = [100 | |
75 | |
100 | |
50 | |
75 | |
50 | |
100 | |
75]; | |
n = length(B); %number of equations and unknowns in this script (q1) | |
%Calculation of the solution | |
% LU Decomposition method | |
%(Initialising U and L....) | |
U = A; | |
L = eye(n); | |
% > LU Decomposition method < | |
%(step 1: Forward Elimination) | |
for k = 1 : n-1 | |
for i = k+1 : n | |
L(i,k) = U(i,k) / U(k,k); | |
U(i,:) = U(i,:) - L(i,k) * U(k,:); | |
end | |
end | |
% >> LU Decomposition method << | |
%(step 2: Forward Substitution) | |
D = zeros(n,1); % initialise solution vector | |
D(1) = B(1); | |
for i = 2 : n | |
D(i) = ( B(i) - L(i,1:i-1) * D(1:i-1) ); | |
end | |
% >>> LU Decomposition method <<< | |
%(step 3: Back Substitution) | |
X = zeros(n,1); % initialise solution vector | |
X(n) = D(n) / U(n,n); | |
for i = n-1 : -1 : 1 | |
X(i) = ( D(i) - U(i,i+1:n) * X(i+1:n) ) / U(i,i); | |
end | |
%Print solution | |
fprintf('The calculated displacements of the %d masses are: \n', n) | |
for i = 1:n | |
fprintf('X%d = %6.2f mm\n', i, X(i)) | |
end | |
%question2 | |
%% Read or Input any square Matrix | |
A = [20 -10 0 0 0 0 0 0 | |
-10 20 -10 0 0 0 0 0 | |
0 -10 15 -5 0 0 0 0 | |
0 0 -5 10 -5 0 0 0 | |
0 0 0 -5 20 -15 0 0 | |
0 0 0 0 -15 30 -15 0 | |
0 0 0 0 0 -15 30 -15 | |
0 0 0 0 0 0 -15 15];% coefficients matrices | |
C = [100 | |
75 | |
100 | |
50 | |
75 | |
50 | |
100 | |
75];% constants vectors | |
n = length(C); | |
X = zeros(n,1); | |
Error_eval = ones(n,1); | |
%% Checking if the matrix A is diagonally dominant | |
for i = 1:n | |
j = 1:n; | |
j(i) = []; | |
B = abs(A(i,j)); | |
Check(i) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining row values combined? | |
if Check(i) < 0 | |
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i) | |
end | |
end | |
%% Starts the Iterative method | |
iteration = 0; | |
while max(Error_eval) > 0.00001 | |
iteration = iteration + 1; | |
Z = X; % save current values to calculate error later | |
for i = 1:n | |
j = 1:n; % define the array of the coefficients' elements | |
j(i) = []; % eliminate the unknow's coefficient from the remaining coefficients | |
Xmass = X; % copy the unknows to a new variable | |
Xmass(i) = []; % eliminate unknowns under question from the set of values | |
X(i) = (C(i) - sum(A(i,j) * Xmass)) / A(i,i); | |
end | |
Xsolution(:,iteration) = X; | |
Error_eval = sqrt((X - Z).^2); | |
end | |
%Print solution | |
fprintf('The calculated displacements of the %d masses are: \n', n) | |
for i = 1:n | |
fprintf('X%d = %6.2f mm\n', i, X(i)) | |
end | |
%question 3 | |
%Coefficent of matrix A | |
A = [20 -10 0 0 0 0 0 0 | |
-10 20 -10 0 0 0 0 0 | |
0 -10 15 -5 0 0 0 0 | |
0 0 -5 10 -5 0 0 0 | |
0 0 0 -5 20 -15 0 0 | |
0 0 0 0 -15 30 -15 0 | |
0 0 0 0 0 -15 30 -15 | |
0 0 0 0 0 0 -15 15]; | |
n = length(A); | |
%Factorisuing A... | |
% >>> Using the LU Decomposition method <<< | |
%(Initialise U and L) | |
U = A; | |
L = eye(n); | |
% >>> Using the LU Decomposition method <<< | |
%(step 1: Forward Elimination) | |
for k = 1 : n-1 | |
for i = k+1 : n | |
L(i,k) = U(i,k) / U(k,k); | |
U(i,:) = U(i,:) - L(i,k) * U(k,:); | |
end | |
end | |
% The solution is calculated with B | |
B =[100 | |
75 | |
100 | |
50 | |
75 | |
50 | |
100 | |
75] ; | |
% >>> Using the LU Decomposition method <<< | |
%(step 2.1: Forward Substitution) | |
D = zeros(n,1); % initialise the solution vector | |
D(1) = B(1); | |
for i = 2 : n | |
D(i) = ( B(i) - L(i,1:i-1) * D(1:i-1) ); | |
end | |
% >>> LU Decomposition method <<< | |
%(step 2.2: Back Substitution) | |
X_B = zeros(n,1); % initialise solution vector | |
X_B(n) = D(n) / U(n,n); | |
for i = n-1 : -1 : 1 | |
X_B(i) = ( D(i) - U(i,i+1:n) * X_B(i+1:n) ) / U(i,i); | |
end | |
%The solution is calcuated with C | |
for W_6 = linspace(70,130,15); | |
C = [100 | |
75 | |
100 | |
50 | |
75 | |
W_6 | |
100 | |
75]; | |
% >>> Using the LU Decomposition method <<< | |
%(step 2.1: Forward Substitution) | |
D = zeros(n,1); % initialise solution vector | |
D(1) = C(1); | |
for i = 2 : n | |
D(i) = ( C(i) - L(i,1:i-1) * D(1:i-1) ); | |
end | |
% >>> Using the LU Decomposition method <<< | |
%(step 2.2: Back Substitution) | |
X_C = zeros(n,1); % initialise solution vector | |
X_C(n) = D(n) / U(n,n); | |
for i = n-1 : -1 : 1 | |
X_C(i) = ( D(i) - U(i,i+1:n) * X_C(i+1:n) ) / U(i,i); | |
end | |
%Printing solutions | |
fprintf('The calculated displacements of the %d masses are: \n', n) | |
for i = 1:n | |
fprintf('X%d = %6.2f mm (with B), and X%d = %6.2f mm (with C)\n', i, X_B(i), i, X_C(i)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment