From 86d22f87a9e7ec8e671a61d35d82dc7bc7393436 Mon Sep 17 00:00:00 2001 From: PolishPigeon Date: Fri, 7 Jan 2022 19:53:21 +0100 Subject: [PATCH] feat: Beautifying backsubstitution code --- ENUME/projectC/backSubstitution.m | 34 +++++----- ENUME/projectC/task2ode.asv | 104 ------------------------------ 2 files changed, 19 insertions(+), 119 deletions(-) delete mode 100644 ENUME/projectC/task2ode.asv diff --git a/ENUME/projectC/backSubstitution.m b/ENUME/projectC/backSubstitution.m index 0c0deb21..8705f642 100644 --- a/ENUME/projectC/backSubstitution.m +++ b/ENUME/projectC/backSubstitution.m @@ -1,16 +1,20 @@ -% solves system with triangular matrix -function result = backSubstitution(eqsys) - for col = size(eqsys, 1):-1:1 - % normalize diagonal coefficients to 1 - eqsys(col, :) = eqsys(col, :) / eqsys(col, col); - - % eliminate factor from other rows - for row = (col - 1):-1:1 - reductor = eqsys(row, col) / eqsys(col, col); - eqsys(row, :) = eqsys(row, :) - eqsys(col, :) * reductor; - end - end - - % rightmost column is now the result - result = eqsys(:, size(eqsys, 2)); +function solution = backSubstitution(Matrix) + Columns = size(Matrix, 1); + Matrix = backSubstitutionOuterLoop(Matrix, Columns); + % rightmost column of the Matrix is now our result + solution = Matrix(:, size(Matrix, 2)); +end + +function Matrix = backSubstitutionOuterLoop(Matrix, Columns) + for k = Columns : -1 : 1 + % Diagonal coefficients of matrix need to be equal to 1 + Matrix(k, :) = Matrix(k, :) / Matrix(k, k); + Matrix = eliminateFactors(Matrix, k); + end +end + +function Matrix = eliminateFactors(Matrix, k) + for row = (k - 1) : -1 : 1 + Matrix(row, :) = Matrix(row, :) - Matrix(k, :) * (Matrix(row, k) / Matrix(k, k)); + end end diff --git a/ENUME/projectC/task2ode.asv b/ENUME/projectC/task2ode.asv deleted file mode 100644 index 6994c99d..00000000 --- a/ENUME/projectC/task2ode.asv +++ /dev/null @@ -1,104 +0,0 @@ -% functions of the ODE system and initial values -sysfuncts = { - @(x) x(2) + x(1) * (0.5 - x(1)^2 - x(2)^2); - @(x) -x(1) + x(2) * (0.5 - x(1)^2 - x(2)^2) -}; -initvalues = [0; 12]; -interval = [0; 15]; - -% define available algorithms -algorithms = { - 'RK4', @rk4, [0.01, 0.013408]; - 'Adams PC', @adamspc, [0.002, 0.01305] -}; - -% solve ODE using different algorithms and step sizes -for alg = 1:size(algorithms, 1) - [algname, algfunc, stepsizes] = algorithms{alg, :}; - - % solve using the given algorithm for each step size - stepresults = cell(size(stepsizes, 2), 3); - stepnames = {'optimal step', 'larger step'}; - for stepno = 1:size(stepsizes, 2) - result = algfunc(sysfuncts, initvalues, interval, stepsizes(stepno)); - stepresults(stepno, :) = { ... - stepsizes(stepno), ... - stepnames{stepno}, ... - result}; - end - - % plot each component against time - for eqnum = 1:size(sysfuncts, 1) - % begin plot - figure; grid on; hold on; - title([algname, ', x_', num2str(eqnum), ' against time']); - set(gcf, 'PaperPosition', [0 0 6 4]); - set(gcf, 'PaperSize', [6 4]); - - % plot component for each step size - for stepresult = stepresults' - plot(stepresult{3}(1, :), stepresult{3}(eqnum + 1, :)); - end - - % finish plot - hold off; - legend(stepresults{:, 2}); -% %print(['report/', func2str(algfunc), 'x', num2str(eqnum)], '-dpdf'); - end - - % plot first two components against each other - figure; grid on; hold on; - title([algname, ' trajectory plot (x_2 against x_1)']); - set(gcf, 'PaperPosition', [0 0 6 4]); - set(gcf, 'PaperSize', [6 4]); - - % plot for each step size - for stepresult = stepresults' - plot(stepresult{3}(2, :), stepresult{3}(3, :)); - end - - % finish plot - hold off; - legend(stepresults{:, 2}); -% %print(['report/', func2str(algfunc), 'traj'], '-dpdf'); -end - -% solve ODE using RK4 with automatic step size -[result, sizes, errors] = rk4auto(sysfuncts, initvalues, interval, ... - 1e-5, 10e-10, 10e-10); - -% plot trajectory -figure; -plot(result(2, :), result(3, :)); -grid on; -title('RK4 auto-step trajectory plot (x_2 against x_1)'); -set(gcf, 'PaperPosition', [0 0 6 4]); -set(gcf, 'PaperSize', [6 4]); -%%print(['report/', 'rk4autotraj'], '-dpdf'); - -% plot statistics -stats = { - "RK4 auto-step step size", "rk4sizes", sizes; - "RK4 auto-step approximation error", "rk4errors", errors -}; -for stat = stats' - figure; - plot(result(1, 2:(end - 1)), stat{3}); - grid on; - title(stat{1}); - set(gcf, 'PaperPosition', [0 0 6 4]); - set(gcf, 'PaperSize', [6 4]); -% %print(strcat("report/", stat{2}), '-dpdf'); -end - -% compare results with ODE45 -odefun = @(t, x) [ sysfuncts{1}(x); sysfuncts{2}(x) ]; -odeoptions = odeset('RelTol', 10e-10, 'AbsTol', 10e-10); -[t, x] = ode45(odefun, interval, initvalues, odeoptions); -figure; -plot(x(:, 1), x(:, 2)); -grid on; -title('ODE45 trajectory plot (x_2 against x_1)'); -set(gcf, 'PaperPosition', [0 0 6 4]); -set(gcf, 'PaperSize', [6 4]); -%print('report/ode45', '-dpdf');