mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 17:23:03 +02:00
70 lines
3.0 KiB
Plaintext
70 lines
3.0 KiB
Plaintext
function x = adamspc(functions, initialValues, interval, stepSize)
|
|
[x, derrivatives, explicitCoefficients, implicitCoefficients, stepCount] = initialize(functions, initialValues, interval, stepSize);
|
|
[stepSize, stepCount, x] = adamsPcLoop(x, derrivatives, explicitCoefficients, implicitCoefficients, stepCount, functions, stepSize);
|
|
% append arguments to output
|
|
x = [interval(1):stepSize:(stepCount * stepSize); x];
|
|
end
|
|
|
|
function [x, derrivatives, explicitCoefficients, implicitCoefficients, stepCount] = initialize(functions, initialValues, interval, stepSize)
|
|
% obtain first five steps from RK4
|
|
[x, derrivatives] = RK4(functions, initialValues, interval, stepSize, 5);
|
|
x = x(2:end, :);
|
|
|
|
% define coefficient
|
|
explicitCoefficients = [1901, -2774, 1616, -1274, 251] / 720;
|
|
% Constants that can be found on the Internet or in Mister Tatjewski
|
|
% book on page 177, notice how beta(3) = 1616 instead of 2616
|
|
% I have found on the internet different value for this parameter and
|
|
% got better results with it so I decided to stick with it
|
|
implicitCoefficients = [475, 1427, -798, 482, -173, 27] / 1440;
|
|
% Constants that can be found on the Internet or in Mister Tatjewski
|
|
% book on page 178
|
|
% build output based on preceding values
|
|
stepCount = ceil((interval(2) - interval(1)) / stepSize);
|
|
end
|
|
|
|
function [stepSize, stepCount, x] = adamsPcLoop(x, derrivatives, explicitCoefficients, implicitCoefficients, stepCount, functions, stepSize)
|
|
for step = 6: (stepCount + 1)
|
|
% P
|
|
predictionOfX = adamsPredict(x, step, stepSize, explicitCoefficients, derrivatives);
|
|
|
|
|
|
|
|
% correct
|
|
x(:, step) = x(:, step - 1);
|
|
for equationNumber = 1:size(functions, 1)
|
|
for previous = 1:5
|
|
x(equationNumber, step) = x(equationNumber, step) ...
|
|
+ stepSize * implicitCoefficients(previous + 1) * derrivatives(equationNumber, step - previous);
|
|
end
|
|
x(equationNumber, step) = x(equationNumber, step) ...
|
|
+ stepSize * implicitCoefficients(1) * derrivativePrediction(equationNumber);
|
|
end
|
|
|
|
% evaluate
|
|
for equationNumber = 1:size(functions, 1)
|
|
derrivatives(equationNumber, step) = functions{equationNumber}(x(:, step));
|
|
end
|
|
end
|
|
end
|
|
|
|
function predictionOfX = adamsPredict(x, step, stepSize, explicitCoefficients, derrivatives)
|
|
predictionOfX = x(:, step - 1);
|
|
for equationNumber = 1 : 2
|
|
for previous = 1:5
|
|
predictionOfX(equationNumber) = predictionOfX(equationNumber) ...
|
|
+ stepSize * explicitCoefficients(previous) * derrivatives(equationNumber, step - previous);
|
|
end
|
|
end
|
|
end
|
|
|
|
function evaluateAdamsValue = adamsEvaluate
|
|
evaluateAdamsValue = zeros(size(functions, 1), 1);
|
|
for equationNumber = 1:size(functions, 1)
|
|
evaluateAdamsValue(equationNumber) = functions{equationNumber}(predictionOfX);
|
|
end
|
|
end
|
|
|
|
function adamsCorrect
|
|
|
|
end |