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.
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
# MATLAB autosave and workspace files
|
||||||
|
*.asv
|
||||||
|
*.m~
|
||||||
|
*.mat
|
||||||
|
|
||||||
|
# plot output captured by `make test` and `make demo`
|
||||||
|
tests/.last-run.log
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# equation-solver - run the solvers and the test suite.
|
||||||
|
#
|
||||||
|
# make test run tests/run_tests.m under Octave (exit 0 = pass, 1 = fail)
|
||||||
|
# make demo solve x^3-1 = 0 with the three methods, prints the numbers
|
||||||
|
# make run start the dialog interface projetodlg (needs a display)
|
||||||
|
# make clean delete the captured plot output
|
||||||
|
#
|
||||||
|
# The solvers plot the function while they iterate, so Octave needs a graphics
|
||||||
|
# toolkit. On a machine with no display, install gnuplot-nox and
|
||||||
|
# fonts-freefont-otf; the scripts then create the figure invisible and the
|
||||||
|
# Makefile captures what gnuplot renders in tests/.last-run.log.
|
||||||
|
|
||||||
|
SHELL := /bin/bash
|
||||||
|
OCTAVE ?= octave-cli
|
||||||
|
LOG := tests/.last-run.log
|
||||||
|
|
||||||
|
.PHONY: help test demo run clean
|
||||||
|
|
||||||
|
help:
|
||||||
|
@echo "make test run the Octave test suite (tests/run_tests.m)"
|
||||||
|
@echo "make demo solve x^3-1 = 0 with the three methods, no window needed"
|
||||||
|
@echo "make run start the dialog interface projetodlg (needs a display)"
|
||||||
|
@echo "make clean delete tests/.last-run.log"
|
||||||
|
|
||||||
|
test:
|
||||||
|
@$(OCTAVE) --no-gui tests/run_tests.m 1>$(LOG); \
|
||||||
|
rc=$$?; \
|
||||||
|
if [ $$rc -ne 0 ]; then echo "(plot output kept in $(LOG))" >&2; fi; \
|
||||||
|
exit $$rc
|
||||||
|
|
||||||
|
demo:
|
||||||
|
@$(OCTAVE) --no-gui tests/demo.m 1>$(LOG)
|
||||||
|
|
||||||
|
run:
|
||||||
|
@$(OCTAVE) --no-gui projetodlg.m
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(LOG)
|
||||||
@@ -1,7 +1,180 @@
|
|||||||
# Equation-Solver
|
# equation-solver
|
||||||
|
|
||||||
Notes from the original authors are kept in [README.txt](README.txt).
|
> MATLAB/Octave coursework mini-project that solves one equation in one variable by bisection, Newton-Raphson or secant, animating the iterates, behind a dialog interface instead of a command-line prompt.
|
||||||
|
|
||||||
|
## What it is
|
||||||
|
|
||||||
|
Three independent solvers for a single equation in one variable. Each one is a
|
||||||
|
plain function file that takes the equation as a function handle and returns the
|
||||||
|
last iterate plus the table of every iterate:
|
||||||
|
|
||||||
|
| file | signature |
|
||||||
|
|---|---|
|
||||||
|
| `bisection.m` | `[c, tab] = bisection(f, x0, x1, tol, max, time)` |
|
||||||
|
| `secant.m` | `[x1, tab] = secant(f, x0, x1, tol, max, time)` |
|
||||||
|
| `newtonr.m` | `[x1, tab] = newtonr(f, x0, tol, max, time)` |
|
||||||
|
|
||||||
|
Defaults are `tol = 1e-4`, `max = 20` iterations and `time = 1` second between
|
||||||
|
frames. The default block is guarded by `nargin < 4` in `bisection`/`secant` and
|
||||||
|
`nargin < 3` in `newtonr`, so calling a solver with some but not all of the
|
||||||
|
optional arguments leaves the remaining ones undefined: pass all of them or
|
||||||
|
none. Each solver opens a figure, plots `f`, marks the iterates on the x-axis
|
||||||
|
and pauses `time` seconds between frames, so the method is animated.
|
||||||
|
|
||||||
|
`bisection.m` requires `x0 < x1` and `f(x0)*f(x1) <= 0` and stops when
|
||||||
|
`f(c) == 0` or when `(x1-x0)/2 < tol`. `secant.m` applies the same two input
|
||||||
|
checks even though the secant method does not need a sign change, and stops when
|
||||||
|
`abs(x2-x1) < tol`, returning the previous iterate rather than the newest one.
|
||||||
|
`newtonr.m` applies no input validation: in MATLAB the derivative comes from the
|
||||||
|
Symbolic Math Toolbox (`sym`, `diff`, `matlabFunction`) and where
|
||||||
|
`matlabFunction` is not available, as in Octave, it falls back to a central
|
||||||
|
difference with `h = 1e-6`. The fallback agrees with the analytic derivative to
|
||||||
|
2.3e-16 or better on the test functions. None of the three reports
|
||||||
|
non-convergence: after `max` iterations each returns its last iterate anyway.
|
||||||
|
|
||||||
|
`projetodlg.m` is the entry point. `listdlg` picks the method, `inputdlg`
|
||||||
|
collects the equation (pre-filled with `exp(x-3/2)-1`) and the parameters of the
|
||||||
|
chosen method, the solver runs inside a `try`/`catch` that reopens the parameter
|
||||||
|
dialog and prints the error message, and a `uitable` with the iterate history and
|
||||||
|
a `msgbox` with the result close the flow. It is built from MathWorks dialog
|
||||||
|
functions and does not run under Octave without a graphical toolkit. The GUI
|
||||||
|
title is `Miniproject MCEE 21/21`. `README.txt` records the intent (make as
|
||||||
|
little use of the command window as possible, with the long-term idea of a real
|
||||||
|
`.exe`) and names the authors: Ines Ferreira and Andre Neto.
|
||||||
|
|
||||||
|
### Known limitations
|
||||||
|
|
||||||
|
- `tab = (max)` at the top of the three solvers is not a preallocation: it
|
||||||
|
assigns the scalar `max` to `tab`. `tab` still ends up with one row per
|
||||||
|
iteration because it grows by indexing, so the returned values are correct and
|
||||||
|
the line is only misleading.
|
||||||
|
- `newtonr.m` with `f'(x0) == 0` announces `x1 is local min/max` through
|
||||||
|
`errordlg` and returns before assigning `x1`. A caller that asks for the
|
||||||
|
output then fails with `element number 1 undefined in return list`.
|
||||||
|
- `secant.m` rejects an interval without a sign change, which the secant method
|
||||||
|
itself does not require.
|
||||||
|
- Neither `bisection.m` nor `secant.m` can tell the caller that it stopped
|
||||||
|
because it ran out of iterations instead of reaching `tol`.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
- MATLAB, or Octave. Tested with **Octave 9.4.0** on Debian 13 (aarch64); no
|
||||||
|
Octave package is needed, `newtonr.m` runs without `symbolic`.
|
||||||
|
- In MATLAB, `newtonr.m` uses the Symbolic Math Toolbox when it is installed.
|
||||||
|
- Without a display, Octave needs a graphics toolkit for the plots the solvers
|
||||||
|
draw: `gnuplot-nox` and `fonts-freefont-otf`. Without the fonts Octave stops
|
||||||
|
at `ft_text_renderer: invalid bounding box, cannot render`.
|
||||||
|
- `projetodlg.m` needs MATLAB, or an Octave built with the `qt` toolkit, and a
|
||||||
|
display.
|
||||||
|
- No third-party MATLAB code is included.
|
||||||
|
|
||||||
|
## Install / Build
|
||||||
|
|
||||||
|
Nothing to build; the `.m` files run from the repository directory.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get install octave # Octave itself
|
||||||
|
sudo apt-get install gnuplot-nox fonts-freefont-otf # only without a display
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Command line, no window needed:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ make demo
|
||||||
|
equation x^3-1 = 0, tol 1e-10, max 200 iterations
|
||||||
|
bisection -> 1.0000000000 (tab rows: 1)
|
||||||
|
secant -> 1.0000000000 (tab rows: 13)
|
||||||
|
newtonr -> 1.0000000000 (tab rows: 7)
|
||||||
|
```
|
||||||
|
|
||||||
|
Or drive a solver directly. The figure is made invisible and stdout, which
|
||||||
|
carries the plot, is discarded; the result is written to stderr:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ octave-cli --no-gui --eval "warning('off','all'); set(0,'defaultfigurevisible','off');
|
||||||
|
graphics_toolkit('gnuplot'); [c,tab]=bisection(@(x) cos(x)-x,0,1,1e-10,200,0);
|
||||||
|
fprintf(stderr,'root=%.10f tab rows=%d\n',c,numel(tab))" >/dev/null
|
||||||
|
root=0.7390851332 tab rows=34
|
||||||
|
```
|
||||||
|
|
||||||
|
The dialog interface is started from MATLAB (or from Octave with a display):
|
||||||
|
|
||||||
|
```matlab
|
||||||
|
projetodlg
|
||||||
|
```
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
|
||||||
|
`make test` runs `tests/run_tests.m`, which calls every method at `tol = 1e-10`
|
||||||
|
and compares the result with a root known analytically, using an assertion
|
||||||
|
tolerance of `1e-6` — four orders of magnitude tighter than the solver default.
|
||||||
|
It also checks that four malformed inputs raise their documented error. It exits
|
||||||
|
with 0 when everything passes and 1 when any check fails.
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ make test
|
||||||
|
equation-solver test suite - Octave 9.4.0
|
||||||
|
solvers called with tol=1e-10, max=200, time=0; assertion tolerance 1e-06
|
||||||
|
|
||||||
|
method file case |x-root| result
|
||||||
|
bisection bisection.m x^3-1 = 0 on [0,2] 0.000e+00 pass
|
||||||
|
bisection bisection.m cos(x)-x = 0 on [0,1] 3.052e-11 pass
|
||||||
|
bisection bisection.m x^2-2 = 0 on [0,2] 4.706e-11 pass
|
||||||
|
secant secant.m x^3-1 = 0 on [0,2] 2.133e-11 pass
|
||||||
|
secant secant.m cos(x)-x = 0 on [0,1] 1.594e-13 pass
|
||||||
|
secant secant.m x^2-2 = 0 on [0,2] 2.220e-16 pass
|
||||||
|
newtonr newtonr.m x^3-1 = 0 from x0=2 0.000e+00 pass
|
||||||
|
newtonr newtonr.m cos(x)-x = 0 from x0=0.5 0.000e+00 pass
|
||||||
|
newtonr newtonr.m x^2-2 = 0 from x0=1 0.000e+00 pass
|
||||||
|
|
||||||
|
rejected inputs (each one must raise its documented error)
|
||||||
|
method file case result
|
||||||
|
bisection bisection.m x^2+1 = 0 on [-2,2]: no root pass
|
||||||
|
secant secant.m x^2+1 = 0 on [-2,2]: no root pass
|
||||||
|
bisection bisection.m x0=2, x1=0: x0 > x1 pass
|
||||||
|
secant secant.m x0=2, x1=0: x0 > x1 pass
|
||||||
|
|
||||||
|
known issues, not fixed here (see README, Known limitations)
|
||||||
|
* tab = (max) at the top of the three solvers is not a preallocation: it
|
||||||
|
assigns the scalar max to tab. tab still holds one row per iteration because
|
||||||
|
it grows by indexing, so the values are correct and the line is only
|
||||||
|
misleading. Here: bisection over [0,1] with max=200 kept 34 rows.
|
||||||
|
* newtonr with f'(x0) == 0 announces "x1 is local min/max" through errordlg
|
||||||
|
and returns before assigning x1. A caller that asks for the output gets:
|
||||||
|
element number 1 undefined in return list
|
||||||
|
|
||||||
|
OK: 13 checks, 0 failures
|
||||||
|
```
|
||||||
|
|
||||||
|
The plots the solvers draw go to stdout and are captured in
|
||||||
|
`tests/.last-run.log`; only the report above is printed. `make run` starts
|
||||||
|
`projetodlg.m` and needs a display — without one it stops with
|
||||||
|
`error: listdlg is not available in this version of Octave`.
|
||||||
|
|
||||||
|
## Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
bisection.m 71 bisection method, animated plot, iterate table
|
||||||
|
newtonr.m 63 Newton-Raphson: symbolic derivative, numeric fallback
|
||||||
|
secant.m 74 secant method, animated plot, iterate table
|
||||||
|
projetodlg.m 84 dialog interface and entry point
|
||||||
|
README.txt 15 original side notes: intent, language choice, authors
|
||||||
|
LICENSE 21 MIT, added 2026-10-08, copyright year 2022
|
||||||
|
Makefile 38 targets: help, test, demo, run, clean
|
||||||
|
tests/run_tests.m 166 the suite `make test` runs
|
||||||
|
tests/demo.m 32 the numbers `make demo` prints
|
||||||
|
.gitignore 7 MATLAB autosave/workspace files, captured plot output
|
||||||
|
```
|
||||||
|
|
||||||
|
The four `.m` files kept from the original upload use CRLF line endings; the
|
||||||
|
files added here use LF.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT — see [LICENSE](LICENSE).
|
MIT — see [`LICENSE`](LICENSE). The file was added on 2026-10-08 and carries the
|
||||||
|
year of the first commit, 2022. This is coursework written by two people:
|
||||||
|
`README.txt` names Ines Ferreira and Andre Neto as authors and maintainers,
|
||||||
|
while `LICENSE` names Andre Neto, so the MIT terms are taken to be agreed
|
||||||
|
between both authors.
|
||||||
|
|||||||
@@ -9,8 +9,16 @@ end
|
|||||||
|
|
||||||
tab = (max);
|
tab = (max);
|
||||||
|
|
||||||
|
% Derivative of f. MATLAB gets it from the Symbolic Math Toolbox;
|
||||||
|
% Octave does not provide matlabFunction, so fall back to a central
|
||||||
|
% difference (O(h^2), ~1e-12 for h=1e-6, far below tol=1e-4).
|
||||||
|
if exist('sym', 'file') && exist('matlabFunction', 'file')
|
||||||
y = sym(f);
|
y = sym(f);
|
||||||
fp = matlabFunction(diff(y));
|
fp = matlabFunction(diff(y));
|
||||||
|
else
|
||||||
|
h = 1e-6;
|
||||||
|
fp = @(x) (f(x+h) - f(x-h)) / (2*h);
|
||||||
|
end
|
||||||
|
|
||||||
x = linspace(x0-10, x0+10, 1000);
|
x = linspace(x0-10, x0+10, 1000);
|
||||||
|
|
||||||
@@ -31,7 +39,7 @@ for i = 1:max
|
|||||||
tab(i) = x0;
|
tab(i) = x0;
|
||||||
|
|
||||||
h1 = plot(x0, f(x0), 'go', 'MarkerFaceColor', 'g', 'MarkerSize', 6, 'LineWidth',2);
|
h1 = plot(x0, f(x0), 'go', 'MarkerFaceColor', 'g', 'MarkerSize', 6, 'LineWidth',2);
|
||||||
text(x0, f(x0)+0.5, " 𝑥_{"+ i + "}")
|
text(x0, f(x0)+0.5, [" 𝑥_{" num2str(i) "}"])
|
||||||
pause(time)
|
pause(time)
|
||||||
delete(h1)
|
delete(h1)
|
||||||
plot(x0, f(x0), 'bo', 'MarkerFaceColor', 'b', 'MarkerSize', 6, 'LineWidth',2)
|
plot(x0, f(x0), 'bo', 'MarkerFaceColor', 'b', 'MarkerSize', 6, 'LineWidth',2)
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ for i = 1:max
|
|||||||
|
|
||||||
dotted_line = plot([x2 x2],[0 f(x2)],"r", "LineWidth", 1.5, "LineStyle", ":");
|
dotted_line = plot([x2 x2],[0 f(x2)],"r", "LineWidth", 1.5, "LineStyle", ":");
|
||||||
plot(x2, f(x2), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
|
plot(x2, f(x2), 'ro', 'MarkerFaceColor', 'r', 'MarkerSize', 4)
|
||||||
text(x2, f(x2), " f(𝑥_{" + (i + 1) + "})")
|
text(x2, f(x2), [" f(𝑥_{" num2str(i + 1) "})"])
|
||||||
pause(time)
|
pause(time)
|
||||||
|
|
||||||
% condition
|
% condition
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
% Command-line demo used by `make demo`: the three methods on x^3-1 = 0.
|
||||||
|
% The numbers go to stderr; the plot the solvers draw goes to stdout and the
|
||||||
|
% Makefile captures it in tests/.last-run.log.
|
||||||
|
|
||||||
|
if (isempty(which('bisection')))
|
||||||
|
here = fileparts(mfilename('fullpath'));
|
||||||
|
if (isempty(here))
|
||||||
|
here = fullfile(pwd, 'tests');
|
||||||
|
end
|
||||||
|
addpath(fullfile(here, '..'));
|
||||||
|
end
|
||||||
|
|
||||||
|
warning('off', 'all');
|
||||||
|
set(0, 'defaultfigurevisible', 'off');
|
||||||
|
graphics_toolkit('gnuplot');
|
||||||
|
|
||||||
|
f = @(x) x.^3 - 1;
|
||||||
|
|
||||||
|
fprintf(stderr, 'equation x^3-1 = 0, tol 1e-10, max 200 iterations\n');
|
||||||
|
for step = 1:3
|
||||||
|
if (step == 1)
|
||||||
|
[x, t] = bisection(f, 0, 2, 1e-10, 200, 0);
|
||||||
|
name = 'bisection';
|
||||||
|
elseif (step == 2)
|
||||||
|
[x, t] = secant(f, 0, 2, 1e-10, 200, 0);
|
||||||
|
name = 'secant ';
|
||||||
|
else
|
||||||
|
[x, t] = newtonr(f, 2, 1e-10, 200, 0);
|
||||||
|
name = 'newtonr ';
|
||||||
|
end
|
||||||
|
fprintf(stderr, '%s -> %.10f (tab rows: %d)\n', name, x, numel(t));
|
||||||
|
end
|
||||||
@@ -0,0 +1,166 @@
|
|||||||
|
% equation-solver - test suite.
|
||||||
|
%
|
||||||
|
% make test
|
||||||
|
% octave-cli --no-gui tests/run_tests.m
|
||||||
|
%
|
||||||
|
% Every method is called with a tolerance of 1e-10 and 200 iterations and the
|
||||||
|
% result is compared with the root known analytically. The assertion tolerance
|
||||||
|
% used here is 1e-6, four orders of magnitude tighter than the 1e-4 default of
|
||||||
|
% the solvers.
|
||||||
|
%
|
||||||
|
% The solvers draw the function while they iterate. There is no window here, so
|
||||||
|
% the figure is created invisible with the gnuplot toolkit; what gnuplot renders
|
||||||
|
% goes to stdout and the Makefile redirects it to tests/.last-run.log. The
|
||||||
|
% report below is written to stderr so that it stays readable.
|
||||||
|
%
|
||||||
|
% Exit status is 0 when every check passes and 1 otherwise.
|
||||||
|
|
||||||
|
if (isempty(which('bisection')))
|
||||||
|
here = fileparts(mfilename('fullpath'));
|
||||||
|
if (isempty(here))
|
||||||
|
here = fullfile(pwd, 'tests');
|
||||||
|
end
|
||||||
|
addpath(fullfile(here, '..'));
|
||||||
|
end
|
||||||
|
|
||||||
|
warning('off', 'all');
|
||||||
|
set(0, 'defaultfigurevisible', 'off');
|
||||||
|
graphics_toolkit('gnuplot');
|
||||||
|
|
||||||
|
TOL = 1e-6; % assertion tolerance
|
||||||
|
MTOL = 1e-10; % tolerance handed to the solvers
|
||||||
|
MAXI = 200; % iteration cap handed to the solvers
|
||||||
|
T = 0; % seconds between animation frames
|
||||||
|
|
||||||
|
methods = {'bisection', 'bisection', 'bisection', ...
|
||||||
|
'secant', 'secant', 'secant', ...
|
||||||
|
'newtonr', 'newtonr', 'newtonr'};
|
||||||
|
files = {'bisection.m', 'bisection.m', 'bisection.m', ...
|
||||||
|
'secant.m', 'secant.m', 'secant.m', ...
|
||||||
|
'newtonr.m', 'newtonr.m', 'newtonr.m'};
|
||||||
|
labels = {'x^3-1 = 0 on [0,2]', ...
|
||||||
|
'cos(x)-x = 0 on [0,1]', ...
|
||||||
|
'x^2-2 = 0 on [0,2]', ...
|
||||||
|
'x^3-1 = 0 on [0,2]', ...
|
||||||
|
'cos(x)-x = 0 on [0,1]', ...
|
||||||
|
'x^2-2 = 0 on [0,2]', ...
|
||||||
|
'x^3-1 = 0 from x0=2', ...
|
||||||
|
'cos(x)-x = 0 from x0=0.5', ...
|
||||||
|
'x^2-2 = 0 from x0=1'};
|
||||||
|
roots = [1, 0.7390851332151607, sqrt(2), ...
|
||||||
|
1, 0.7390851332151607, sqrt(2), ...
|
||||||
|
1, 0.7390851332151607, sqrt(2)];
|
||||||
|
calls = {@() bisection(@(x) x.^3-1, 0, 2, MTOL, MAXI, T), ...
|
||||||
|
@() bisection(@(x) cos(x)-x, 0, 1, MTOL, MAXI, T), ...
|
||||||
|
@() bisection(@(x) x.^2-2, 0, 2, MTOL, MAXI, T), ...
|
||||||
|
@() secant(@(x) x.^3-1, 0, 2, MTOL, MAXI, T), ...
|
||||||
|
@() secant(@(x) cos(x)-x, 0, 1, MTOL, MAXI, T), ...
|
||||||
|
@() secant(@(x) x.^2-2, 0, 2, MTOL, MAXI, T), ...
|
||||||
|
@() newtonr(@(x) x.^3-1, 2, MTOL, MAXI, T), ...
|
||||||
|
@() newtonr(@(x) cos(x)-x, 0.5, MTOL, MAXI, T), ...
|
||||||
|
@() newtonr(@(x) x.^2-2, 1, MTOL, MAXI, T)};
|
||||||
|
|
||||||
|
fprintf(stderr, 'equation-solver test suite - Octave %s\n', version());
|
||||||
|
fprintf(stderr, 'solvers called with tol=%g, max=%d, time=%g; assertion tolerance %g\n\n', ...
|
||||||
|
MTOL, MAXI, T, TOL);
|
||||||
|
fprintf(stderr, '%-10s %-13s %-27s %-12s %s\n', 'method', 'file', 'case', '|x-root|', 'result');
|
||||||
|
|
||||||
|
checks = 0;
|
||||||
|
failures = 0;
|
||||||
|
|
||||||
|
for k = 1:numel(calls)
|
||||||
|
checks = checks + 1;
|
||||||
|
status = 'pass';
|
||||||
|
detail = '';
|
||||||
|
errs = '-';
|
||||||
|
try
|
||||||
|
x = calls{k}();
|
||||||
|
if (isempty(x) || !isscalar(x) || !isfinite(x))
|
||||||
|
status = 'FAIL';
|
||||||
|
detail = 'result is not a finite scalar';
|
||||||
|
else
|
||||||
|
err = abs(x - roots(k));
|
||||||
|
errs = sprintf('%.3e', err);
|
||||||
|
if (err > TOL)
|
||||||
|
status = 'FAIL';
|
||||||
|
detail = sprintf('got %.15g, error above the assertion tolerance', x);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
catch e
|
||||||
|
status = 'FAIL';
|
||||||
|
detail = sprintf('unexpected error: %s', e.message);
|
||||||
|
end
|
||||||
|
if (strcmp(status, 'FAIL'))
|
||||||
|
failures = failures + 1;
|
||||||
|
errs = '-';
|
||||||
|
end
|
||||||
|
fprintf(stderr, '%-10s %-13s %-27s %-12s %s\n', methods{k}, files{k}, labels{k}, errs, status);
|
||||||
|
if (!isempty(detail))
|
||||||
|
fprintf(stderr, '%-10s %-13s %s\n', '', '', detail);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fprintf(stderr, '\nrejected inputs (each one must raise its documented error)\n');
|
||||||
|
fprintf(stderr, '%-10s %-13s %-27s %s\n', 'method', 'file', 'case', 'result');
|
||||||
|
|
||||||
|
rejlabels = {'x^2+1 = 0 on [-2,2]: no root', ...
|
||||||
|
'x^2+1 = 0 on [-2,2]: no root', ...
|
||||||
|
'x0=2, x1=0: x0 > x1', ...
|
||||||
|
'x0=2, x1=0: x0 > x1'};
|
||||||
|
rejfiles = {'bisection.m', 'secant.m', 'bisection.m', 'secant.m'};
|
||||||
|
rejcalls = {@() bisection(@(x) x.^2+1, -2, 2, MTOL, MAXI, T), ...
|
||||||
|
@() secant(@(x) x.^2+1, -2, 2, MTOL, MAXI, T), ...
|
||||||
|
@() bisection(@(x) x.^3-1, 2, 0, MTOL, MAXI, T), ...
|
||||||
|
@() secant(@(x) x.^3-1, 2, 0, MTOL, MAXI, T)};
|
||||||
|
rejwanted = {'The interval does not contain a root', ...
|
||||||
|
'The interval does not contain a root', ...
|
||||||
|
'x1 must be bigger than x0', ...
|
||||||
|
'x1 must be bigger than x0'};
|
||||||
|
|
||||||
|
for k = 1:numel(rejcalls)
|
||||||
|
checks = checks + 1;
|
||||||
|
status = 'pass';
|
||||||
|
detail = '';
|
||||||
|
try
|
||||||
|
x = rejcalls{k}();
|
||||||
|
status = 'FAIL';
|
||||||
|
detail = 'no error raised';
|
||||||
|
catch e
|
||||||
|
if (!strcmp(e.message, rejwanted{k}))
|
||||||
|
status = 'FAIL';
|
||||||
|
detail = sprintf('wrong error: %s', e.message);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if (strcmp(status, 'FAIL'))
|
||||||
|
failures = failures + 1;
|
||||||
|
end
|
||||||
|
fprintf(stderr, '%-10s %-13s %-27s %s\n', ...
|
||||||
|
strrep(rejfiles{k}, '.m', ''), rejfiles{k}, rejlabels{k}, status);
|
||||||
|
if (!isempty(detail))
|
||||||
|
fprintf(stderr, '%-10s %-13s %s\n', '', '', detail);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
fprintf(stderr, '\nknown issues, not fixed here (see README, Known limitations)\n');
|
||||||
|
[bc, bt] = bisection(@(x) cos(x)-x, 0, 1, MTOL, MAXI, T);
|
||||||
|
nrows = numel(bt);
|
||||||
|
fprintf(stderr, ' * tab = (max) at the top of the three solvers is not a preallocation: it\n');
|
||||||
|
fprintf(stderr, ' assigns the scalar max to tab. tab still holds one row per iteration because\n');
|
||||||
|
fprintf(stderr, ' it grows by indexing, so the values are correct and the line is only\n');
|
||||||
|
fprintf(stderr, ' misleading. Here: bisection over [0,1] with max=200 kept %d rows.\n', nrows);
|
||||||
|
fprintf(stderr, ' * newtonr with f''(x0) == 0 announces "x1 is local min/max" through errordlg\n');
|
||||||
|
fprintf(stderr, ' and returns before assigning x1. A caller that asks for the output gets:\n');
|
||||||
|
try
|
||||||
|
[r, t] = newtonr(@(x) x.^2-1, 0, MTOL, MAXI, T);
|
||||||
|
fprintf(stderr, ' (no error; it returned %g)\n', r);
|
||||||
|
catch e
|
||||||
|
fprintf(stderr, ' %s\n', e.message);
|
||||||
|
end
|
||||||
|
|
||||||
|
if (failures > 0)
|
||||||
|
fprintf(stderr, '\nFAILED: %d of %d checks\n', failures, checks);
|
||||||
|
exit(1);
|
||||||
|
else
|
||||||
|
fprintf(stderr, '\nOK: %d checks, 0 failures\n', checks);
|
||||||
|
exit(0);
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user