mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 20:03:04 +02:00
110 lines
4.5 KiB
Plaintext
110 lines
4.5 KiB
Plaintext
interval = [-5, 10];
|
|
rootBrackets = rootBracketing(@polynomial, interval(1), interval(2));
|
|
|
|
printGraph(@polynomial, 'MM1', @mm1, interval, rootBrackets, 'Approximate zeros of function for method of ');
|
|
|
|
printComplexGraph(@polynomial, 'MM1', @mm1, [-1 + i, 0], 'Aproximate complex roots of polynomial');
|
|
|
|
function y = polynomial(x)
|
|
y = -2 * x^4 + 12 * x^3 + 4* x^2 + 1 * x + 3;
|
|
end
|
|
|
|
function [approximation, iterations] = mm1(polynomial, a, b, tolerance)
|
|
|
|
[approximation, approximationValue, iterations] = initialize(a, b, polynomial);
|
|
[approximation, iterations] = mm1Loop(approximation, tolerance, approximationValue, iterations, polynomial);
|
|
end
|
|
|
|
function [approximation, approximationValue, iterations] = initialize(a, b, polynomial)
|
|
approximation = [a, b, (a + b) / 2];
|
|
approximationValue = arrayfun(polynomial, approximation);
|
|
iterations = [approximation(3); polynomial(approximation(3))];
|
|
end
|
|
|
|
function [approximation, iterations] = mm1Loop(approximation, tolerance, approximationValue, iterations, polynomial)
|
|
while abs(polynomial(approximation(3))) > tolerance
|
|
[approximation, approximationValue, iterations] = insideLoop(approximation, approximationValue, polynomial, iterations);
|
|
end
|
|
end
|
|
|
|
function [approximation, approximationValue, iterations] = insideLoop(approximation, approximationValue, polynomial, iterations)
|
|
eqsys = createEquationSystem(approximation, approximationValue);
|
|
[zplus, zminus] = rootsOfQuadraticFormula(eqsys, approximationValue);
|
|
[approximation, approximationValue, iterations] = updateApproximations(zplus, zminus, approximation, iterations, polynomial);
|
|
end
|
|
|
|
function eqsys = createEquationSystem(approximation, approximationValue)
|
|
[z0, z1, diff0, diff1] = initializeEquationSystem(approximation, approximationValue);
|
|
eqsys = solveEquationSystem(z0, diff0, z1, diff1);
|
|
end
|
|
|
|
function [zplus, zminus] = rootsOfQuadraticFormula(eqsys, approximationValue)
|
|
[a, b, c] = createApproximatedQuadraticFormula(eqsys, approximationValue);
|
|
[zplus, zminus] = findRootsOfQuadraticFormula(a, b, c);
|
|
end
|
|
|
|
function [approximation, approximationValue, iterations] = updateApproximations(zplus, zminus, approximation, iterations, polynomial)
|
|
newapprx = chooseNewRoot(zplus, zminus, approximation);
|
|
iterations = addZeroToIterationVector(newapprx, iterations, polynomial);
|
|
worstapprxindex = getWorstApproximationIndex(approximation, newapprx);
|
|
[approximation, approximationValue] = deleteWorstApproximation(worstapprxindex, approximation, polynomial, newapprx);
|
|
end
|
|
|
|
function [z0, z1, diff0, diff1] = initializeEquationSystem(approximation, approximationValue)
|
|
z0 = approximation(1) - approximation(3);
|
|
z1 = approximation(2) - approximation(3);
|
|
diff0 = approximationValue(1) - approximationValue(3);
|
|
diff1 = approximationValue(2) - approximationValue(3);
|
|
end
|
|
|
|
function eqsys = solveEquationSystem(z0, diff0, z1, diff1)
|
|
eqsys = [z0 ^ 2, z0, diff0; z1 ^ 2, z1, diff1];
|
|
reductor = eqsys(2, 1) / eqsys(1, 1);
|
|
eqsys(2, :) = eqsys(2, :) - reductor * eqsys(1, :);
|
|
eqsys(2, 1) = 0;
|
|
eqsys(2, :) = eqsys(2, :) ./ eqsys(2, 2);
|
|
eqsys(1, :) = eqsys(1, :) - eqsys(1, 2) * eqsys(2, :);
|
|
eqsys(1, :) = eqsys(1, :) ./ eqsys(1, 1);
|
|
end
|
|
|
|
function [a, b, c] = createApproximatedQuadraticFormula(eqsys, approximationValue)
|
|
a = eqsys(1, 3);
|
|
b = eqsys(2, 3);
|
|
c = approximationValue(3);
|
|
end
|
|
|
|
function [zplus, zminus] = findRootsOfQuadraticFormula(a, b, c)
|
|
zplus = -2 * c / (b + sqrt(b ^ 2 - 4 * a * c));
|
|
zminus = -2 * c / (b - sqrt(b ^ 2 - 4 * a * c));
|
|
end
|
|
|
|
function newapprx = chooseNewRoot(zplus, zminus, approximation)
|
|
if abs(zplus) < abs(zminus)
|
|
newapprx = approximation(3) + zplus;
|
|
else
|
|
newapprx = approximation(3) + zminus;
|
|
end
|
|
end
|
|
|
|
function iterations = addZeroToIterationVector(newapprx, iterations, polynomial)
|
|
zero = newapprx;
|
|
iterations(:, size(iterations, 2) + 1) = [zero, polynomial(zero)];
|
|
end
|
|
|
|
function worstapprxindex = getWorstApproximationIndex(approximation, newapprx)
|
|
worstapprxindex = -1;
|
|
worstapprxdiff = 0;
|
|
for i = 1:size(approximation, 2)
|
|
diff = abs(approximation(i) - newapprx);
|
|
if diff > worstapprxdiff
|
|
worstapprxindex = i;
|
|
worstapprxdiff = diff;
|
|
end
|
|
end
|
|
end
|
|
|
|
function [approximation, approximationValue] = deleteWorstApproximation(worstapprxindex, approximation, polynomial, newapprx)
|
|
approximation(worstapprxindex) = [];
|
|
approximation(3) = newapprx;
|
|
approximationValue = arrayfun(polynomial, approximation);
|
|
end |