Files
equation-solver/secant.m
T
Andre Neto d547f2fc28 Make the solvers run and be tested under Octave
The project is MATLAB coursework and had never been run outside MATLAB. Two
things stop it in Octave, both found by running it rather than reading it:

  * newtonr.m called sym/matlabFunction unconditionally. Octave has no
    matlabFunction, so the file died before the first iteration with
    "'sym' undefined near line 12, column 5". The derivative is now taken
    from the Symbolic Math Toolbox when it is there and from a central
    difference otherwise. The method and its results are unchanged: on the
    test functions the numeric derivative agrees with the analytic one to
    2.3e-16 or better.
  * secant.m and newtonr.m built plot labels with "a" + i + "b". A
    double-quoted literal is a char array in Octave, not a string, so the
    addition became numeric and secant.m stopped at line 58 with
    "operator +: nonconformant arguments (op1 is 1x10, op2 is 1x2)". The
    labels are now built by concatenation and the text is identical.

No mathematics was changed. The defects that were found are left in place and
documented instead: tab = (max) is not a preallocation, newtonr returns
without assigning x1 when f'(x0) == 0, secant rejects an interval without a
sign change, and none of the three reports exhaustion of the iteration cap.

Added so the behaviour can be checked instead of asserted:

  * tests/run_tests.m - 13 checks against roots known analytically, solvers
    called at tol=1e-10, assertion tolerance 1e-6, exit 1 on any failure
  * a Makefile with test, demo, run and clean, and tests/demo.m
  * README.md in the house format; README.txt is kept as the original note
  * .gitignore for MATLAB autosave files and the captured plot output

Verified on the raspberry pi with Octave 9.4.0: make test reports 13 checks,
0 failures and exits 0, and a deliberately wrong expected root gives exit 1.
projetodlg.m was not made to run under Octave: it is built from MathWorks
dialogs and needs a display.
2026-10-08 14:54:36 +01:00

75 lines
1.7 KiB
Matlab
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function [x1, tab] = secant(f, x0, x1, tol, max, time)
% catch errors
if (x0 > x1)
errordlg('x1 must be bigger than x0','Error')
error('x1 must be bigger than x0')
end
if f(x0)*f(x1) > 0
errordlg('The interval does not contain a root','Error')
error('The interval does not contain a root')
end
% default arguments
if nargin < 4
tol = 1e-4;
max = 20;
time = 1;
end
tab = (max);
xx = linspace(x0-1, x1+1, 1000);
figure
axis([x0-1 x1+1 -1 inf])
set(gca,'DataAspectRatio',[1 1 1])
plot(xx, f(xx), 'LineWidth', 1.5)
title('Secant Method')
xlabel("𝑥")
ylabel("f(𝑥)")
grid on
hold on
shg
% initial markings
plot(x0, f(x0), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
text(x0, f(x0), " f(𝑥_0)")
plot(x1, f(x1), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
text(x1, f(x1), " f(𝑥_1)")
for i = 1:max
% calculate x2
x2 = (x0*f(x1) - x1*f(x0)) / (f(x1) - f(x0));
% add element to the table
tab(i) = x2;
% animations
line_x0 = plot([x0 x2], [f(x0) 0],'m', 'LineWidth', 1.5, 'LineStyle', '-.');
line_x1 = plot([x1 x2], [f(x1) 0],'m', 'LineWidth', 1.5, 'LineStyle', '-.');
dot_x2 = plot(x2, 0, 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4);
pause(time)
dotted_line = plot([x2 x2],[0 f(x2)],"r", "LineWidth", 1.5, "LineStyle", ":");
plot(x2, f(x2), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
text(x2, f(x2), [" f(𝑥_{" num2str(i + 1) "})"])
pause(time)
% condition
if abs(x2-x1)<tol
break
end
x0 = x1;
x1 = x2;
delete(line_x0)
delete(line_x1)
delete(dotted_line)
delete(dot_x2)
end
end