WUT_Computer_Science/NotProgramming/ENUME/projectC/RK4.m

48 lines
1.8 KiB
Mathematica
Raw Normal View History

% solve ODE system using RK4 with constant step size
2022-01-08 03:24:09 +01:00
function [x, derivativesTable] = RK4(equations, initialValues, interval, stepSize, maxSteps)
x = initialValues;
2022-01-08 03:24:09 +01:00
derivativesTable = buildDerivatiesTable(x, equations);
% Calculate stepCount
stepCount = ceil((interval(2) - interval(1)) / stepSize);
if nargin == 5
stepCount = min(stepCount, maxSteps - 1);
end % IF we include max steps in our function input
% (nargin is number of arguments in input)
% then choose smaller number between maxSteps and stepCount and choose
% it for stepCount
2022-01-08 03:24:09 +01:00
[x, derivativesTable] = rk4Loop(x, stepCount, stepSize, equations, derivativesTable);
% append arguments to output
x = [interval(1):stepSize:(stepCount * stepSize); x];
end
2022-01-08 03:24:09 +01:00
function derivativesTable = buildDerivatiesTable(x, equations)
derivativesTable = zeros(size(x));
2022-01-08 03:24:09 +01:00
for eqnum = 1:size(equations, 1)
derivativesTable(eqnum, 1) = equations{eqnum}(x(:, 1));
end
end
2022-01-08 03:24:09 +01:00
function [x, derivativesTable] = rk4Loop(x, stepCount, stepSize, equations, derivativesTable)
for step = 1 : stepCount
2022-01-08 03:24:09 +01:00
[x, derivativesTable] = rk4stepLoop(x, step, equations, stepSize, derivativesTable);
end
end
2022-01-08 03:24:09 +01:00
function [x, derivativesTable] = rk4stepLoop(x, step, equations, stepSize, derivativesTable)
stepValue = x(:, step);
2022-01-08 03:24:09 +01:00
[x, derivativesTable] = equationsLoop(x, equations, stepValue, stepSize, step, derivativesTable);
end
function [x, derivativesTable] = equationsLoop(x, equations, stepValue, stepSize, step, derivativesTable)
for equationNumber = 1 : 2
2022-01-08 03:24:09 +01:00
Phi = RK4Phi(equations{equationNumber}, stepValue, stepSize);
x(equationNumber, step + 1) = x(equationNumber, step) + stepSize * Phi;
2022-01-08 03:24:09 +01:00
derivativesTable(equationNumber, step + 1) = equations{equationNumber}(x(:, step + 1));
end
end