added source file because of gitea webview not supporting .mlx

added with save as
This commit is contained in:
lda committed 2025-06-14 03:58:12 +00:00
1 parent bb5767833b
commit 6e7b1062c1
5 files changed
+1549

No files matched your search

+438
View File
@@ -0,0 +1,438 @@
%% Labwork 3: Solving Linear Systems
%% V. Gauss Elimination
%% Exercise 1: Solve the system of linear equations:
%%
%
% 3x1 + 2x2 = 18
% -x1 + 2x2 = 2
%
% a) Graphical Method
% (As the graph is provided in the lab, this part focuses on creating the plot in MATLAB)
% Define the equations as functions to plot
eq1 = @(x1) (18 - 3*x1) / 2;
eq2 = @(x1) (2 + x1) / 2;
% Create the x1 vector for plotting
x1_vals = 0:0.1:6; % Range of x1 values
% Calculate the corresponding x2 values
x2_eq1 = eq1(x1_vals);
x2_eq2 = eq2(x1_vals);
% Plot the equations
figure; % Create a new figure
plot(x1_vals, x2_eq1, 'b-', 'LineWidth', 2); % Plot the first equation (blue line)
hold on; % Hold the current plot to add the second equation
plot(x1_vals, x2_eq2, 'r-', 'LineWidth', 2); % Plot the second equation (red line)
% Add labels and title
xlabel('x1');
ylabel('x2');
title('Graphical Solution of Linear Equations');
legend('3x1 + 2x2 = 18', '-x1 + 2x2 = 2');
axis([0 6 0 9]); % Set the axis limits to match the lab's graph
grid on; % Add grid lines
hold off; % Release the plot hold
% Mark the solution point (x1=4, x2=3) as given in the lab
plot(4, 3, 'ko', 'MarkerSize', 8, 'MarkerFaceColor', 'k'); % Black circle for the solution
text(4.2, 3.2, 'Solution: (4, 3)'); % Label the solution
% b) Naive Gauss Elimination Method
% (Code provided in the lab)
function x = GaussNaive(A, b)
% GaussNaive: naive Gauss elimination
% x = GaussNaive(A, b): Gauss elimination without pivoting.
% input:
% A = coefficient matrix
% b = right hand side vector
% output:
% x = solution vector
[m, n] = size(A);
if m ~= n, error('Matrix A must be square'); end
nb = n + 1;
Aug = [A b]; % Augmented matrix
% forward elimination
for k = 1:n-1
for i = k+1:n
factor = Aug(i, k) / Aug(k, k);
Aug(i, k:nb) = Aug(i, k:nb) - factor * Aug(k, k:nb);
end
end
% back substitution
x = zeros(n, 1);
x(n) = Aug(n, nb) / Aug(n, n);
for i = n-1:-1:1
x(i) = (Aug(i, nb) - Aug(i, i+1:n) * x(i+1:n)) / Aug(i, i);
end
end
% Define the coefficient matrix A and the right-hand side vector b
A = [3 2; -1 2];
b = [18; 2];
% Solve using the GaussNaive function
x_naive = GaussNaive(A, b)
%% Exercise 2: Solve the system of linear equations:
%%
%
% 2x + y - z = 8
% -3x - y + 2z = -11
% -2x + y + 2z = -3
%
% a) Using inv or mldivide in Matlab
% Define the coefficient matrix A and the right-hand side vector b
A = [2 1 -1; -3 -1 2; -2 1 2];
b = [8; -11; -3];
% Solve using inv
x_inv = inv(A) * b
% Solve using mldivide (backslash operator \) - RECOMMENDED
x_mldivide = A \ b
% b) Use the function GaussPivot.m
% (Code provided in the lab)
function x = GaussPivot(A, b)
% GaussPivot: Gauss elimination pivoting
% x = GaussPivot(A, b): Gauss elimination with pivoting.
% input:
% A = coefficient matrix
% b = right hand side vector
% output:
% x = solution vector
[m, n] = size(A);
if m ~= n, error('Matrix A must be square'); end
nb = n + 1;
Aug = [A b];
% forward elimination
for k = 1:n-1
% partial pivoting
[big, i] = max(abs(Aug(k:n, k)));
ipr = i + k - 1;
if ipr ~= k
Aug([k, ipr], :) = Aug([ipr, k], :);
end
for i = k+1:n
factor = Aug(i, k) / Aug(k, k);
Aug(i, k:nb) = Aug(i, k:nb) - factor * Aug(k, k:nb);
end
end
% back substitution
x = zeros(n, 1);
x(n) = Aug(n, nb) / Aug(n, n);
for i = n-1:-1:1
x(i) = (Aug(i, nb) - Aug(i, i+1:n) * x(i+1:n)) / Aug(i, i);
end
end
% Solve using GaussPivot
x_pivot = GaussPivot(A, b)
% c) Modify the code above to Gauss Elimination without Pivoting
function x = GaussNaive_modified(A, b)
% GaussNaive_modified: Gauss elimination without pivoting (modified from GaussPivot).
% input:
% A = coefficient matrix
% b = right hand side vector
% output:
% x = solution vector
[m, n] = size(A);
if m ~= n, error('Matrix A must be square'); end
nb = n + 1;
Aug = [A b];
% forward elimination (WITHOUT PIVOTING)
for k = 1:n-1
for i = k+1:n
factor = Aug(i, k) / Aug(k, k);
Aug(i, k:nb) = Aug(i, k:nb) - factor * Aug(k, k:nb);
end
end
% back substitution
x = zeros(n, 1);
x(n) = Aug(n, nb) / Aug(n, n);
for i = n-1:-1:1
x(i) = (Aug(i, nb) - Aug(i, i+1:n) * x(i+1:n)) / Aug(i, i);
end
end
% Solve using GaussNaive_modified
x_naive_mod = GaussNaive_modified(A, b)
% d) Explain the code
% (Explanation embedded in the comments within the code above)
%% VI. LU Decomposition
%% Exercise 3: LU decomposition
%%
%
% 2x + y - z = 8
% -3x - y + 2z = -11
% -2x + y + 2z = -3
%
% a) Using function lu in Matlab
% Define A and b (same as before)
A = [2 1 -1; -3 -1 2; -2 1 2];
b = [8; -11; -3];
% Perform LU decomposition
[L, U, P] = lu(A);
% Solve Ly = Pb
y = L \ (P * b);
% Solve Ux = y
x_lu_matlab = U \ y
% b) Use the function LU_pivot.m code
% (Code provided in the lab)
function [L, U, P] = LU_pivot(A)
% LU_pivot: LU decomposition with pivoting
[n, ~] = size(A);
L = eye(n);
P = eye(n);
U = A;
for j = 1:n
[pivot, m] = max(abs(U(j:n, j)));
m = m + j - 1;
if m ~= j
U([m, j], :) = U([j, m], :); % interchange rows m and j in U
P([m, j], :) = P([j, m], :); % interchange rows m and j in P
if j >= 2
L([m, j], 1:j-1) = L([j, m], 1:j-1); % interchange rows m and j in columns 1:j-1 of L
end
end
for i = j+1:n
L(i, j) = U(i, j) / U(j, j);
U(i, :) = U(i, :) - L(i, j) * U(j, :);
end
end
end
% Perform LU decomposition using LU_pivot
[L_pivot, U_pivot, P_pivot] = LU_pivot(A);
% Solve Ly = Pb
y_pivot = L_pivot \ (P_pivot * b);
% Solve Ux = y
x_lu_pivot = U_pivot \ y_pivot
% c) Change the code of LU with pivoting in LU_pivot.m to LU non-pivoting code
function [L, U] = LU_nopivot(A)
% LU_nopivot: LU decomposition without pivoting
[n, ~] = size(A);
L = eye(n);
U = A;
for j = 1:n
for i = j+1:n
L(i, j) = U(i, j) / U(j, j);
U(i, :) = U(i, :) - L(i, j) * U(j, :);
end
end
end
% Perform LU decomposition without pivoting
[L_nopivot, U_nopivot] = LU_nopivot(A);
% Solve Ly = b
y_nopivot = L_nopivot \ b; % No pivoting, so use original b
% Solve Ux = y
x_lu_nopivot = U_nopivot \ y_nopivot
% d) Comparison and explain the difference between LU_pivot.m and mylup.m code:
% (Explanation provided as comments below)
function [L, U, P] = mylup(A)
% MYLUP LU DECOMPOSITION WITH PIVOTING
[n, m] = size(A);
C = zeros(n, n+1);
% initialize
C(1:n, 1:n) = A;
C(1:n, n+1) = (1:n)';
% main algorithm
for j = 1:n-1
if (sum(C(1:n, j)) == 0)
error('mylup:input', 'The solution is not determined.');
end
[~, i] = max(abs(C(1:n, j)));
if (i ~= j)
tt = C(i, 1:n+1);
C(i, 1:n+1) = C(j, 1:n+1);
C(j, 1:n+1) = tt;
end
for i = j+1:n
t = C(i, j) / C(j, j);
C(i, j) = t;
C(i, j+1:n) = C(i, j+1:n) - t * C(j, j+1:n);
end
end
% refine the results
PP = C(1:n, n+1);
Z = C(1:n, 1:n);
L = tril(C, -1);
U = triu(C);
P = zeros(n, n);
for i = 1:n
L(i, i) = 1;
P(i, PP(i)) = 1;
end
end
% Differences between LU_pivot.m and mylup.m:
% - LU_pivot.m is more straightforward, directly manipulating U, L, and P.
% - mylup.m uses an augmented matrix C to keep track of row permutations,
% which can be less intuitive but might be optimized for certain implementations.
% - Both achieve the same goal: LU decomposition with partial pivoting.
% - mylup.m includes a check for a column of zeros, raising an error if found.
%% VII. Common
%% Exercise 1: Create a vector x that:
%%
%
% - is a column vector
% - has all integer value elements from 1 to 20 respectively from the top.
%
% Create the column vector x
x = (1:20)'; % Or x = [1:20]';
% a) Create a vector y that has the same length and size as x's but all entries is 1.
% Create the vector y
y = ones(size(x));
% b) Calculate vector z that is the sum of x and y
% Calculate the vector z
z = x + y;
% c) Calculate the dot product between x and y
% Calculate the dot product
dot_product_xy = dot(x, y); % Or sum(x .* y);
% d) Increment the first, the 5th, the 16th and the last element of z by 3
% Increment the specified elements
z([1, 5, 16, end]) = z([1, 5, 16, end]) + 3;
% e) After step d, multiply those elements with 6.
% Multiply the incremented elements by 6
z([1, 5, 16, end]) = z([1, 5, 16, end]) * 6;
%% Exercise 2: Create a matrix A that:
%%
%
% - has size of 3*3
% - all the entries is 1
%
% Create the matrix A
A = ones(3, 3);
% a) Create matrix b that is transpose of A
% Create the matrix B
B = A'; % Or B = transpose(A); Since A is all ones, A' == A, but this is the general way
% b) Compute sum of A and B
% Compute the sum of A and B
sum_AB = A + B;
% c) Compute A*B and B*A
% Compute A * B and B * A
AB = A * B;
BA = B * A;
% d) Increment all the elements of A by 2.
% Increment all elements of A
A = A + 2;
%% Exercise 3: Give a MATLAB expression that multiplies two vectors to obtain:
% (a) the matrix
% 1 2 3 4 5
% 1 2 3 4 5
% 1 2 3 4 5
% Create the vectors to produce the matrix
vec1a = ones(3, 1);
vec2a = 1:5;
% Matrix (a)
matrix_a = vec1a * vec2a;
% (b) the matrix
% 0 0 0
% 1 1 1
% 2 2 2
% 3 3 3
% 4 4 4
% Create the vectors to produce the matrix
vec1b = (0:4)';
vec2b = ones(1, 3);
% Matrix (b)
matrix_b = vec1b * vec2b;
%% Exercise 4: Create filled 2-D patches
% a) Create matrices x and y.
% Then plot the filled region in red as the figure below using fill in Matlab.
% Define the x and y coordinates for the patches
x_red1 = [0 4 0];
y_red1 = [4 2 2];
x_red2 = [2 4 2];
y_red2 = [0 0 1];
% Create the plot
figure;
fill(x_red1, y_red1, 'c');
hold on;
fill(x_red2, y_red2, 'y');
hold off;
% Add labels and title
xlabel('x');
ylabel('y');
title('Filled 2D Patches');